← Back to unity
Unity: OnTriggerEnter vs. OnCollisionEnter
11 Aug 26 (1mo ago)
The rule of thumb: Use OnCollisionEnter for physical impacts (bouncing, stopping, pushing) and OnTriggerEnter for spatial overlap (passing through).
The Golden Rule for Both
To fire either event:
- Both objects must have a
Collider. - At least one object must have a
Rigidbody.
| Feature | OnCollisionEnter | OnTriggerEnter |
|---|---|---|
Is Trigger Setting | OFF on both colliders. | ON on at least one collider. |
| Physical Reaction | Yes (objects block, bounce, or slide). | No (objects pass directly through). |
| Performance Cost | Higher (calculates forces and contacts). | Lower (only checks bounding box overlap). |
| Best For | Walls, bouncing projectiles, pushing crates. | Collectibles, checkpoints, aggro/dead zones. |
Quick Code Reference
// 1. Physical Impacts (Solid Colliders)
private void OnCollisionEnter(Collision collision)
{
// Access impact force and exact contact points
float force = collision.relativeVelocity.magnitude;
Debug.Log($"Hit {collision.gameObject.name} with force: {force}");
}
// 2. Overlap Detection (Trigger Colliders)
private void OnTriggerEnter(Collider other)
{
// 'other' is the specific collider that passed through the zone
if (other.CompareTag("Player"))
{
Debug.Log("Player entered the zone!");
}
}