Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 36 ยท Physics and Collisions

Collision and Trigger Events

React to physics contacts in Unity: OnCollisionEnter, Stay and Exit, OnTriggerEnter, the collision matrix of which events fire, the Collision data, pickups and damage zones.

Physics is useful for movement, but games care about what happens when things touch: a bullet hits an enemy, the player picks up a coin, a car crosses the finish line, a spike hurts. Unity tells your scripts about these moments through collision and trigger messages. This lesson explains the difference between them, which combinations of components produce which events, and how to write the common handlers.

The Unity scripts are read-only. The stand-ins model the event rules and a damage/pickup system in plain C#.

Collisions versus triggers

  • A collision is a physical contact: the objects bounce, slide or push each other, and Unity calls OnCollision... methods.
  • A trigger is a collider with Is Trigger ticked. Other objects pass through it, and Unity calls OnTrigger... methods when they enter, stay and leave. Triggers are invisible sensors: pickup zones, checkpoints, damage areas, doors that open when you approach.
C#
using UnityEngine;

public class CollisionLogger : MonoBehaviour
{
    // Physical contact (neither collider is a trigger)
    void OnCollisionEnter(Collision collision)
    {
        Debug.Log($"Hit {collision.gameObject.name} with speed {collision.relativeVelocity.magnitude:F1}");
    }
    void OnCollisionStay(Collision collision)  { /* every physics step while touching */ }
    void OnCollisionExit(Collision collision)  { /* stopped touching */ }

    // Sensor overlap (at least one collider is a trigger)
    void OnTriggerEnter(Collider other)
    {
        Debug.Log($"{other.name} entered the zone");
    }
    void OnTriggerStay(Collider other)  { /* every physics step while inside */ }
    void OnTriggerExit(Collider other)  { /* left the zone */ }
}

Note the parameter types: collision methods get a Collision object (with contact points, impact velocity and the other object), while trigger methods get the other Collider directly.

When are events sent?

Messages are sent to the scripts on both objects, but only when the physics rules are satisfied:

  • At least one of the two objects must have a Rigidbody (kinematic is fine).
  • Both objects need a Collider.
  • Their layers must be allowed to collide in the Layer Collision Matrix (Edit > Project Settings > Physics).
  • For OnTrigger..., at least one collider must be a trigger. For OnCollision..., neither is a trigger.

The 3D rules in a table (S = static collider, no Rigidbody; R = Rigidbody collider; K = kinematic Rigidbody collider; T = trigger):

Pair Collision events Trigger events
S with S no no
S with R yes no
R with R yes no
K with R yes no
K with S no no
S with T-R no yes
R with T-R no yes
K with T-R no yes
S with T-S no no

The pattern: any event requires a moving, physics-aware participant. Two static objects never produce messages, and a kinematic body hitting a static collider does not either. The usual setup for a pickup is a static trigger collider (the coin) that the player (with a Rigidbody or a CharacterController) enters. A CharacterController also triggers events, but it uses OnControllerColliderHit for solid contacts.

Modeling the event rules

This stand-in encodes the rule table so you can test combinations. It decides what kind of event two objects produce.

C# Which event fires? (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Notice Wall + Spawn zone: two objects without Rigidbodies produce nothing, which is why a trigger zone appears "dead" until the thing entering has physics.

The Collision object

OnCollisionEnter(Collision c) carries useful data:

C#
void OnCollisionEnter(Collision c)
{
    GameObject other = c.gameObject;                 // what we hit
    float impact = c.relativeVelocity.magnitude;      // how hard
    ContactPoint contact = c.GetContact(0);
    Vector3 point = contact.point;                    // where
    Vector3 normal = contact.normal;                  // the surface direction (points away from the other collider)

    if (impact > 8f) Debug.Log("Ouch, hard landing at " + point);
}

collision.gameObject is the other object, and collision.collider its specific collider. In a trigger handler, other.gameObject and other.attachedRigidbody do the same job.

Common patterns

Pickup

C#
using UnityEngine;

[RequireComponent(typeof(Collider))]
public class Coin : MonoBehaviour
{
    [SerializeField] int value = 10;

    void Reset() { GetComponent<Collider>().isTrigger = true; }   // called when the script is first added

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            GameManager.Instance.score += value;
            Destroy(gameObject);
        }
    }
}

Damage on impact

C#
using UnityEngine;

public class Bullet : MonoBehaviour
{
    [SerializeField] int damage = 10;

    void OnCollisionEnter(Collision c)
    {
        if (c.gameObject.TryGetComponent<Health>(out var health))
            health.TakeDamage(damage);
        Destroy(gameObject);           // bullets vanish on any hit
    }
}

Damage zone, while standing in it

C#
void OnTriggerStay(Collider other)
{
    if (other.TryGetComponent<Health>(out var h))
        h.TakeDamage(5 * Time.fixedDeltaTime);     // 5 damage per second
}

OnTriggerStay runs every physics step, so scale by fixedDeltaTime to make the rate per second.

Simulating a pickup and damage system

This stand-in runs a player through a sequence of overlaps, applying the pickup and damage logic with the same decisions as the scripts above.

C# Pickups and damage zones (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Removing the coin after pickup is the same idea as Destroy(gameObject); the second touch of Coin A finds nothing. And the lava loop shows why a per-step damage value must be scaled by the step time: 75 steps of 0.02 s is 1.5 s, so 7.5 damage.

Pitfalls

  • Nothing fires: check the table: no Rigidbody on either object, a layer that ignores the other, or Is Trigger set wrongly.
  • Event on the wrong object: when colliders are children of an object with a Rigidbody, the messages go to the scripts on the Rigidbody's GameObject, not to scripts on the child collider. Put your handler on the object that holds the Rigidbody.
  • Fast objects may skip the contact (see tunneling in the last lesson).
  • Destroying in OnTriggerEnter is fine; the rest of the method still runs.
  • Multiple colliders: a compound object triggers one event per collider pair.
  • Stay events are per physics step; avoid expensive work in them.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Two static colliders (no Rigidbody on either) overlap. What events fire?

    At least one participant needs a Rigidbody for the physics engine to report events.
  2. 2. Which method is called when a Rigidbody enters a trigger collider?

    Triggers use OnTriggerEnter with the other Collider as the parameter.
  3. 3. What does 'Is Trigger' do?

    A trigger collider detects overlaps without physically blocking.
  4. 4. A damage zone uses OnTriggerStay and deals 5 damage per call. What is the problem?

    OnTriggerStay runs every fixed step, so 5 per call is 250 per second at 50 Hz.

Summary

  • Collisions are physical contacts (OnCollision...); triggers are overlap sensors (OnTrigger...).
  • Events need colliders on both objects, a Rigidbody on at least one, and layers that may collide.
  • Collision carries the other object, impact speed and contact points; trigger methods get the other collider.
  • Use CompareTag and TryGetComponent to identify what you touched.
  • Scale per-step effects by Time.fixedDeltaTime.

Next: Raycasting and Layers