Overview
When creating interactive games in Unity, detecting "something touching something else" is essential. Picking up items when the player touches them, dealing damage when bullets hit enemies, triggering events when entering specific areas—many game interactions begin with "contact."
Unity provides special event functions like OnCollisionEnter and OnTriggerEnter for detecting contact events. These functions are automatically called when specific conditions are met, forming part of Unity's lifecycle. This article explains the differences between these physics event functions and how to use each correctly.
Physics Events vs Trigger Events
Unity's contact events fall into two main categories:
- Physics Events (Collision): Occur when objects physically "collide." Like real-world collisions, objects bounce off or push against each other. Handled by
OnCollisionfunctions. - Trigger Events: Occur when objects enter or exit a specific "zone." Objects pass through each other without physical interaction. Handled by
OnTriggerfunctions.
Which event type to use depends on the behavior you want:
- For physical rebounds like wall/floor collisions or billiard ball impacts, use Collision.
- For zone detection without physical interaction—item pickup zones, event areas, warp portals—use Trigger.
Using Collision Events
There are three Collision event functions: OnCollisionEnter, OnCollisionStay, and OnCollisionExit.
| Event Function | When Called |
|---|---|
OnCollisionEnter(Collision other) | Once, the moment collision with another Collider begins. |
OnCollisionStay(Collision other) | Every frame while in contact with another Collider. |
OnCollisionExit(Collision other) | Once, the moment separation from another Collider occurs. |
These functions receive a Collision class instance as an argument. This other object contains various collision information—the colliding object, contact point, normal vector, etc.
using UnityEngine;
public class CollisionDetector : MonoBehaviour
{
// Called the moment we collide with another object
void OnCollisionEnter(Collision collision)
{
// Access the colliding GameObject via collision.gameObject
Debug.Log("Collided with " + collision.gameObject.name + "!");
// If the other object has "Enemy" tag, destroy ourselves
if (collision.gameObject.CompareTag("Enemy"))
{
Destroy(gameObject);
}
}
}
Collision Event Requirements
- Both colliding objects must have
Collidercomponents. - At least one of the colliding objects must have a
Rigidbodycomponent.
Using Trigger Events
To use Trigger events, check the Is Trigger property on the Collider component. This makes the Collider function as an "area" for triggering events rather than a physical shape.
There are also three Trigger event types:
| Event Function | When Called |
|---|---|
OnTriggerEnter(Collider other) | Once, the moment another Collider enters the trigger zone. |
OnTriggerStay(Collider other) | Every frame while another Collider remains in the trigger zone. |
OnTriggerExit(Collider other) | Once, the moment another Collider exits the trigger zone. |
These functions receive a Collider class instance as an argument. The other parameter contains the Collider component that entered the zone.
using UnityEngine;
public class ItemCollector : MonoBehaviour
{
// Called the moment another Collider enters the trigger zone
void OnTriggerEnter(Collider other)
{
// Access the entering GameObject via other.gameObject
Debug.Log(other.gameObject.name + " entered the item zone.");
// If it has the "Player" tag, hide the item (ourselves)
if (other.CompareTag("Player"))
{
Debug.Log("Item collected!");
gameObject.SetActive(false);
}
}
}
Trigger Event Requirements
- Both objects must have
Collidercomponents. - At least one object must have a
Rigidbodycomponent. - At least one object's Collider must have
Is Triggerenabled.
Other Event Functions
Unity provides various event functions beyond physics events. For example, mouse events detect when the cursor is over an object's Collider:
OnMouseEnter(): When the cursor enters the Collider area.OnMouseExit(): When the cursor leaves the Collider area.OnMouseDown(): When a mouse button is pressed while over the Collider.
These are useful for UI element interactions or selecting 3D objects by clicking.
Summary
Contact events are fundamental for adding interactivity to games. Understand the difference between OnCollision and OnTrigger and choose appropriately:
- Need physical rebounds → Collision (
Is Triggeroff). - Only need zone entry/exit detection (objects pass through) → Trigger (
Is Triggeron). - Both event types require at least one object to have a
Rigidbody.
Master these event functions to create vibrant game worlds that richly respond to player actions.