Unity Guide

OnCollisionEnter vs. OnTriggerEnter | When to use them in Unity?

A quick guide about when to use different collision detection methods in Unity

Fernando Alcantara Santana

--

Objective: Analyze in which cases use OnCollisionEnter and OnTriggerEnter methods to detect collisions between gameobjects in Unity.

In the last post I started to implement the Physics in Unity for the space shooter prototype. Now it’s time to choose and code the function behavior that will allow to detect the collisions between the player and the enemies like in a space shooter game.

OnCollisionEnter vs. OnTriggerEnter

First of all, we need to determine when to use the Unity built methods to detect the different types of collisions that were listed in the last post:

  • Hard surface collisions
  • Trigger collisions

OnCollisionEnter

This method is used to handle the hard surface collisions. When a collider or rigidbody has begun touching another rigidbody or collider the function is executed in the script:

In difference with OnTriggerEnter, OnCollisionEnter contains a parameter of type Collision, which describes information about the collision detected. It’s important to notice that this will only work if one of the gameobjects that collide contains a non-kinematic rigidbody component attached.

For more information about this method, visit the Unity documentation:

OnTriggerEnter

This method is used to handle the trigger collisions. When a gameobject (with IsTrigger enabled and a rigidbody component) collides with another gameobject with a collider component attached this function is executed in the script:

Note: If neither gameobjects contain a rigidbody component or both have enabled the IsTrigger option, the collision won’t be detected.

For more information about this method visit the Unity documentation:

Using OnTriggerEnter

Now, in order to implement a correct collision behavior between the space enemies and the player of my space shooter prototype, I’ll use the OnTriggerEnter method to detect whenever the spaceships collide.

The behavior I want to implement consists in disappearing the enemy spaceships when they collide with the player to inflict damage until it collides with a certain number and loses. So, that’s why I’m not using the OnCollisionEnter method, I don’t want the enemy spaceships and the player to collide and bounce back into the cold and infinite space.

To begin, we need to open the enemy script and write the OnTriggerEnter method:

Then, we need to assign tags to the gameobjects that will collide in the scene, so that we trigger the respective feedback on each. There are some tags by default in the Unity inspector, like in the case of the player, but for the laser and the enemies we’ll need to add a new tag:

Assign the Player tag to the player gameobject.
Create a new tag for the enemy and the laser gameobjects.

Once we assign the tags to the gameobjects, we need to handle them in the code by comparing the tag from the parameter of the method.

It’s important to trigger the desired action before destroying the enemy (which contains the trigger) so that it gets executed. For example: if the enemy detects a laser collision, the laser should be destroyed first and then the enemy.

If the enemy collides with a player or with a laser, it gets destroyed.

As you can see above, I left a comment to apply damage to the player before destroying the enemy in the next line. We’ll see how to do that using script communication in the next post.

But for now, if I execute the game in Unity, the player and the lasers fired destroy the enemies as expected:

Now the player can destroy the enemies by colliding or shooting them.

And that’s it, you can handle the different types of collisions in Unity! :D. I’ll see you in the next post, where I’ll be showing how to communicate between scripts in Unity using the GetComponent method.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

Fernando Alcantara Santana

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).