Unity: OnTriggerEnter vs OnCollisionEnter

Objective: Describe when to use OnTriggerEnter or OnCollisionEnter.

Notable rigidbody & collider events: 1) Event trigger on kinematic cube, turns yellow on trigger. 2) Event trigger on non-kinematic cube falls through floor. 3) No event trigger on kinematic static cube, turns red on collision . 4) No event trigger on non-kinematic cube is moved by physics, turns red on collision.

OnTriggerEnter and OnCollisionEnter are C# components of game objects that detect interactions between objects. A collider is added to a game object to define the boundaries for interactions with other objects. A rigidbody should be added to a game object that moves to detect interactions. If physics are not used check IsKinematic in the rigidbody component. There are two major differences between OnTriggerEnter and OnCollisionEnter:

1. The collider or rigidbody conditions that will cause the script to execute. OnTriggerEnter requires both objects to have a collider with one having IsTrigger checked, and one of the objects must have a rigidbody. OnCollisionEnter requires a rigidbody or collider on both objects, and one of the objects is nonkinematic, i.e. IsKinematic is not checked. Below is shown the possible outcomes with force being added to the sphere rigidbody.

Cube and sphere have collider and rigidbody with gravity. Falls indicates object falls through floor. Floating indicates sphere cannot move. Yellow indicates a trigger event and red is a collision event.

2. The component of the other colliding object that is accessed. OnTriggerEnter accesses the collider of the other colliding object. OnCollisionEnter accesses the collision of the other colliding object.

Use OnCollisionEnter if you need to access collision physics; otherwise, use OnTriggerEnter.

Software engineer