Two Colliders Enter One Collider Leaves: OnCollisionEnter vs OnTriggerEnter

As you build games and simulations in Unity you’ll encounter situations where you have to decide between using collisions and triggers to control game logic.

Something as simple as opening an automatic door, pressing a button in VR, or know when a player has reached a important panel can be done with either OnCollisionEnter() or OnTriggerEnter(). However it can be a little tricky to figure when and where to use either one especially if you’ve never used them before. Let’s take a look at each.

Bonk: Using OnCollisionEnter()

The first one we’ll look at is OnCollisionEnter(), let’s start by looking at the Unity Scripting API (here) to get an idea on how it works. “OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.” Seems pretty straight forward but what does that really mean?

An example would be something like a bowling ball smashing into a bunch of pins or a projectile like a bullet hitting a can. You would use this if you needed the collision data to affect something else like in the bowling ball example. How fast and what angle the ball is moving would determine how to pins knock over.

As you can see when the pins hit one another they change green and then when the player later bumps into one, it changes red all with few lines of code.

Zap: Using OnTriggerEnter()

Next is OnTriggerEnter() like before we’ll look to the Unity Scripting API (here) for clues on it’s implementation. “When a GameObject collides with another GameObject, Unity calls OnTriggerEnter.” Well that sounds exactly like before but there is a key difference that is noted a little later.

Note: Both GameObjects must contain a Collider component. One must have Collider.isTrigger enabled, and contain a Rigidbody. If both GameObjects have Collider.isTrigger enabled, no collision happens. The same applies when both GameObjects do not have a Rigidbody component”.

Unlike OnCollisionEnter() one of the objects in the interaction needs to be checked as IsTrigger enabled, specifically the object that’s passing through must be unchecked. It’s best to think of a collider with IsTrigger enabled as a zone.

When a collider passes through it essentially sends a signal that something has entered, is inside the zone, and once again does it when the object leaves the zone.

This can be useful for something like a teleporter, enemy detection zone, or a finish line in a race are but some of the simpler examples of using OnTriggerEnter(). It is important to note like it mentions above that the other object must have a collider and rigidbody and not be a trigger itself otherwise no detection occurs. In this example above we have a ball that changes color when it enters the zone and then again when it leaves the zone .You can see how simple this code is down below attached to the cube in the scene.

There you have it, a simple to understand and easy to break down examples of what OnCollisionEnter()and OnTriggerEnter() are, how to use them, and most importantly when. These methods are simple but have so much verstaility and really provide ample opportunity to create interesting and immersive game environments using the built in physics engine. Until next time, happy coding.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store