OnTriggerEnter VS OnCollisionEnter

Andrew Crippen
5 min readApr 22, 2021

In Unity and game development in general you need to be able to detect when two different Gameobjects collide. In Unity this is done through the use of colliders, Rigidbodies and the Unity API.

Depending on how you want the collision of gameobjects to be detected and what you want to be done when they collide is what decides whether using the OnTriggerEnter() or OnCollisionEnter() is best for your use case.

OnTriggerEnter

According to the Unity Scripting API, for OnTriggerEnter to work 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.

OnTriggerEnter is best used when you don’t need a physics reaction from 2 GameObjects colliding. Something like collecting a powerup where you simply make the collected powerup disappear upon collision. Also very useful for detecting when a GameObject enters an area, like a player making it to an area triggers a cutscene or enemies appearing.

The gif below demonstrates what happens when isTrigger is enabled but no code is within the OnTriggerEnter method yet.

OnTriggerEnter is called as soon as a gameobject with a collider enters the collider of the gameobject with isTrigger enabled. There is also OnTriggerStay which is called while a GameObject is within the collider and OnTriggerExit which is called when the GameObject exits the collider. I will only be covering OnTriggerEnter but it’s a similar concept for the others.

To check which GameObject has collided you want to compare tags in the OnTriggerEnter method like I have done below. On my Player I have the tag set to Player, Enemy set to Enemy etc.

This checks/compares the Collider other’s tag to see if it is the Laser, if it is then it destroys the Laser and the Enemy which is the red cube I have the Enemy script with this OnTriggerEnter method attached to.

I also have it comparing tags to check if it’s the Player, if it is then it checks to see if the Player has an IDamageable interface using TryGetComponent, it’s a bool so if it returns true it runs the code and damages the player.

(Interfaces are a bit more advanced topic which I will cover in a future article, you could do the same by just getting the Player class component with Player player = other.transform.GetComponent<Player>(); or the same way I got the interface with other.TryGetComponent(out Player player) and then accessing the Damage method through player.Damage(1); the TryGetComponent way is supposed to be a little bit more performant)

Now when the Enemy collides with the Player it is destroyed and damages the player.

The Damage method within my Player script which is called from the OnTriggerEnter method within my Enemy script.

As you can see when the Enemy collides with the Player it deducts 1 Health

Here is what it looks like when a Laser hits the Enemy

OnCollisionEnter

OnCollisionEnter is used when you want to have two or more GameObjects collide and have physics interactions or not have them pass through each other.

According to the Unity Scripting API, OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.

In contrast to OnTriggerEnter, OnCollisionEnter is passed the Collision class and not a Collider. The Collision class contains information, for example, about contact points and impact velocity.

There are also OnCollisionStay and OnCollisionExit methods available.

For this example I will show what it looks like when isTrigger is not enabled on the Enemy and Player, if you have an OnCollisionEnter method in either script attached to the gameobjects you could have it play a sound or make particle effects when they hit each other or various other things. You can also have it only instantiate the particle effects at the actual points of contact too, great example of that in the Unity Scripting API Documentation.

This is how I achieved the explosion from the grenade in my VR test scene….

Nothing stops this dancing alien

I have a collider on my floor and it is tagged as Floor.

I have a collider and a Rigidbody attached to my grenade with a Grenade script that has the OnCollisionEnter method. I’m using Unity’s XR Interaction toolkit’s XR Grab Interactable component here but it would be the same setup otherwise with Oculus Integration Package aka OVR.

Here is how that script looks…

So as you can see OnTriggerEnter, OnCollisionEnter and their Stay and Exit variations open up nearly limitless possibilities.

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Andrew Crippen
Andrew Crippen

Written by Andrew Crippen

Unity Developer/C# Programmer for VR, 2d,3d Games and other Immersive Interactive Experiences

Responses (1)

Write a response

The dancing alien is pretty cool!! Good job!!