Learning Unity — Collisions Vs Triggers

Joshua Nielsen
3 min readJun 8, 2021

--

In the last article, we took a brief tour of Unity’s physics engine. This time we’ll be looking closer at the subjects of Collisions and Triggers, and when you would you want to use each of them.

Let’s start with the similarities. Both Collisions and Triggers require all objects involved to have a Collider component. At least one object involved needs to have a Rigidbody component. If these two requirements are not met, nothing will happen.

They also both have multiple messages that can inform you of when objects are interacting with each other, although the messages are different between Collisions and Triggers.

Now for the differences.

Triggers

A Trigger is for when you want to know when two objects have “touched”, but you don’t want them to interact in a physical way. When an object is a trigger other objects pass through it freely.

A physical object passing through a trigger object

As noted earlier, Triggers have certain messages that are called when another object interacts with them.

  1. OnTriggerEnter: This is called when another object touches the trigger object. Within a game, you might use this to trigger a certain event, such as a cutscene or enemy ambush. You might also use this to have the player “pick up” an in-game item such as a powerup or money.
  2. OnTriggerExit: This is called when another object stops touching the trigger object. Within a game, you might use this to trigger other events or as an alternative to OnTriggerEnter. For example, perhaps your game has a “safe zone” and the enemies resume being hostile after exiting.
  3. OnTriggerStay: This is called while another object is touching the trigger object. Within a game, you might use this for “zones” that have different effects. For example, you might have lava in your game that causes damage while the player is in it.

Collisions

A Collision is for when you want objects to… collide. Seriously though, Collisions are used when you want objects to interact in a physical way. For example, when you want a ball to bounce off of a wall you would use a Collision.

A physical object colliding with another physical object

Collisions have different messages than Triggers, but they heavily parallel each other.

  1. OnCollisionEnter: This message is called when two objects touch. A good example of when you might use this is when a player’s lasers hit an enemy (or vice versa).
  2. OnCollisionExit: This message is called when two objects stop touching. This could come into play if you were making a game similar to baseball, where strict contact with the base is an important element of the rules.
  3. OnCollisionStay: This message is called while two objects are touching. Perhaps similar to with triggers you might have a dangerous surface, such as an electrified floor or wall, that cause damage while in contact.

When should I use Collisions? Triggers?

This actually may be a trickier question than it first appears, but for right now I think a good rule of thumb is if you need an object to act in a “physical” manner (having gravity or other forces act upon it, blocking the movement of other objects, etc) then you should definitely use Collisions. Otherwise, Triggers may be sufficient to your needs.

In the next article, we will be looking at how to communicate with another object’s script. Which is very appropriate, because that is how we will be able to actually make best use of these Trigger and Collision messages. Until then, good luck and happy coding!

--

--