Unity3D: OnCollisionEnter vs. OnTriggerEnter
What is the difference?
First, let us take a look at what Unity’s scripting API says about them two. Then we can try to understand what they mean.
OnCollisionEnter: is called when a game object’s collider or rigidbody has begun touching another game object’s collider or rigidbody.
In the example below, the sphere and the cube have a default collider attached for collision events. When the sphere collided with the cube, the OnCollisionEnter() is called and runs the code to change colors for both objects.
OnTriggerEnter: is called when a game object with a collider passes through a game object with “Is Trigger” checked collider.
The cube object has a default collider. The sphere has a trigger collider. When the sphere passes through the cube, the OnTriggerEnter() is called and runs the code to change the material of the sphere and destroy the cube.
As we can see, there is a difference and its uses.
If we want to create a game about destruction derby, basically cars ramming against each other for destruction, then we would use a default collider with “Is Trigger” unchecked and call OnCollisionEnter through scripting. When two cars collide, it will call OnCollisionEnter on both of the game objects. Collisions also enable physics to the game object.
If we want to collect items or perhaps trigger a door or a cut scene event, we use a “Is Trigger” check collider and call OnTriggerEnter through scripting. When a player walks through a box collider trigger, a cut scene can start, or a door would open. Physics is disabled for trigger colliders.
They are both equally important and used for different mechanics in a video game. Use them for their intent and you’ll bet you won’t have any issues!