0
0
Unityframework~7 mins

OnCollisionEnter2D and OnTriggerEnter2D in Unity

Choose your learning style9 modes available
Introduction

These functions help your game know when two objects touch or overlap. This lets you make things happen, like scoring points or playing sounds.

When you want to detect if a player hits a wall or enemy.
When you want to know if a player picks up an item by touching it.
When you want to trigger a door to open when the player enters a zone.
When you want to stop a character from passing through solid objects.
When you want to detect if a projectile hits a target.
Syntax
Unity
using UnityEngine;

public class CollisionExample : MonoBehaviour
{
    // Called when this object collides with another with a Rigidbody2D and Collider2D
    private void OnCollisionEnter2D(Collision2D collision)
    {
        // Your code here
    }

    // Called when this object's trigger collider overlaps another collider
    private void OnTriggerEnter2D(Collider2D other)
    {
        // Your code here
    }
}

OnCollisionEnter2D is called when two solid objects with Rigidbody2D and Collider2D touch and collide.

OnTriggerEnter2D is called when an object with a Collider2D set as a trigger overlaps another collider.

Examples
This runs when your object bumps into another solid object.
Unity
private void OnCollisionEnter2D(Collision2D collision)
{
    Debug.Log("Collided with " + collision.gameObject.name);
}
This runs when your object enters a trigger zone.
Unity
private void OnTriggerEnter2D(Collider2D other)
{
    Debug.Log("Entered trigger of " + other.gameObject.name);
}
Make sure your objects have Rigidbody2D components for these methods to work.
Unity
// Edge case: What if the object has no Rigidbody2D?
// Neither method will be called because physics needs Rigidbody2D to detect collisions or triggers.
Set the collider's 'Is Trigger' checkbox to switch between collision and trigger behavior.
Unity
// Edge case: What if the collider is not set as trigger?
// OnTriggerEnter2D won't run; instead OnCollisionEnter2D will run if colliders are solid.
Sample Program

This script prints messages when the object collides with another solid object or enters a trigger zone. Attach it to a GameObject with Rigidbody2D and Collider2D components. Make sure some colliders are triggers and some are not to see both messages.

Unity
using UnityEngine;

public class CollisionTriggerDemo : MonoBehaviour
{
    private void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log($"Collision detected with {collision.gameObject.name}");
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log($"Trigger entered by {other.gameObject.name}");
    }
}
OutputSuccess
Important Notes

Time complexity: These methods run only when collisions or triggers happen, so they do not slow down your game continuously.

Space complexity: Minimal, only storing collision info temporarily.

Common mistake: Forgetting to add Rigidbody2D to objects, so these methods never get called.

Use OnCollisionEnter2D for solid object hits and OnTriggerEnter2D for zones or pickups that don't block movement.

Summary

OnCollisionEnter2D detects solid collisions between objects with Rigidbody2D and Collider2D.

OnTriggerEnter2D detects when an object enters a trigger collider (a collider set as a trigger).

Both let you run code when objects touch or overlap, helping your game respond to player actions and events.