OnCollisionEnter vs. OnTrigger Enter — When to Use Them?

Ricardo Miranda
3 min readMay 22, 2022

--

Trigger vs. Collision Events

In game development, there are times when we want objects to display rigidbody physics while at other times, we don’t. The behavior of the cube objects on the left illustrate non-physics based behavior while the spheres on the right display a type of physics-based collision. Game objects can detect these types of interactions by using Unity’s built-in MonoBehaviour Messages, OnTriggerEnter( )¹ and OnCollisionEnter( )².

OnTriggerEnter(Collider other)

OnTriggerEnter( )¹ is used to detect the behavior of the cubes in the top illustration, such as when one Game Object is overlapping with another Game Object. This is used when the colliding objects can pass through each other or when we want to detect the collision, without having to simulate the physics of a collision.

In order for the OnTriggerEnter( ) message to be activated, both objects must have Collider components attached. Also, the object that is doing the detecting must have the Collider.isTrigger enabled and that detecting object must also have a Rigidbody component attached. Since the detecting object will have the Collider.isTrigger enabled, that means that the collision event will simply trigger an OnTriggerEnter( ) message, WITHOUT a physics-based interaction with the other object. This is used simply for collision detection, not for the simulation of physics interactions.

This is useful when we want to detect, for example, the collision between a laser projectile and an enemy object. We don’t necessarily want a rigidbody collision behavior between the laser projectile and the enemy. Instead, we simply want to detect the collision so that a Destroy( ) command can be run on the enemy. Figure 1 bellow illustrates an earlier implementation of OnTriggerEnter( ), where Destroty(other) (Destroy enemy) is declared, inside of the OnTriggerEnter( ) method.

Figure 1: Destroying Enemy Object Inside of OnTriggerEnter( ) Method

The illustration below shows the behavior that is enabled by the method outlined in Figure 1.

Note that both the enemy and the laser projectile are both destroyed. That means that the OnTriggerEnter( ) method is applied on both object scripts.

OnCollisionEnter(Collision collision)

This method is used to detect collisions for objects that have non-kinematic Rigdbody components. You may notice that both OnTriggerEnter( ) and OnCollisionEnter( ) detection both require Rigidbody components on the game objects. So, what’s the difference here? The main difference here is that OnCollisionEnter( ) can be used to detect collisions for objects whose Collider component is set to Collider.isTrigger (is false). That means that the game object IS NOT a trigger. The Collider.isTrigger components of objects displaying physics behaviors must be turned off. The Collider.isTrigger components of the objects we DON’T want displaying physics, on the other hand, must be set to true.

OnCollisionEnter( ) can be useful when wanting to detect collision (or contact) between say, a player and a platform. This becomes necessary to know before determining whether or not the player should be allowed to jump. For a game like Meat Boy, it’s useful to know whether the player is making contact with the walls, in order to apply a sticky wall or slipping effect.

Super Meat Boy: Platform & Wall Collision Detection

That wraps up OnTriggerEnter( ) vs. OnCollisionEnter( ) and when to use them.

Unity Documentation References

[1] OnTriggerEnter(Collider other)
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html

[2] OnCollisionEnter(Collision collision)
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter.html

Check Out My Portfolio Here:
https://ricardoemiranda.com/
(under construction)

Check Out My Other Articles Here:
https://medium.com/@ricardoemiranda

--

--