OnCollisionEnter VS OnTriggerEnter - When To Use Them.

Austin Hall
2 min readSep 7, 2022

--

In Unity we primarily deal with two types of collisions. We have hard surface collisions for things like balls bouncing off of walls or car crashes. And we have trigger collisions for instances like gathering a coin when we run over it, or collecting a power up as we move through it.

Snippet from the Unity Scripting API

OnCollisionEnter() is called when a collider/rigid body has begun touching another rigid body/collider. this method passes the Collision class, which contains information about contact points, impact velocity and other attributes.

On the other hand we have trigger collisions, which are called using the OnTriggerEnter() method.

Snippet from Enemy Script using Trigger Collision to decrement player life

In contrast to OnCollisionEnter(), the OnTriggerEnter() method calls the Collider class, which is a base class of all colliders. For trigger collisions to function properly, both game objects must have a collider component. At least one of the objects must have Collider.isTrigger enabled and contain a rigid body.

Enemy Prefab selected, shown with collider and rigid body components

If we select our Enemy Prefab, we can see that it has a collider and a rigid body component. Is Trigger box is checked on the Box Collider. This object meets the requirements for the OnTriggerEnter() method.

Laser Prefab selected, shown with collider and rigid body components

We can see that the Laser Prefab also contains the required components for the OnTriggerEnter() method.

Enemy Script, OnTriggerEnter() method, If statement for collision with Laser

If we implement this if statement inside our OnTriggerEnter() method, we expect both the enemy and the laser to be destroyed when they collide.

Trigger Collision with Enemy and Laser

--

--

No responses yet