Collisions and Triggers in Unity

Learning more about colliders

Pablo Gómez Platón
4 min readApr 10, 2021

For every action, there is an equal and opposite reactionThird Newton’s Law

When the ball collides, it emits dust. When the ball goes through the red square, it completes the level!

In the previous article, I reviewed Rigidbodies in Unity. If you haven’t read it, please go ahead and check that first. Rigidbodies are necessary for collisions or triggers to work.

Today, it’s all about colliders, and the two types of collision! Put on your seatbelt because we’re rushing through a highway full of colliders!

Knowing the basics… again

How to generate a collision or an overlap

You need at least two GameObjects, both with Colliders, and at least one of them has a Rigidbody component that is NOT set to Kinematic.

Kinematic bodies don’t react to collisions because that mode lets developers handle each interaction. Though, other Dynamic Rigidbodies can react to collisions made with Kinematic Bodies.

The Unity manual has a Collision chart to help you understand what kind of collisions or Rigidbodies can interact with each other.

How to access the Collision / Trigger events?

Your script must inherit from the MonoBehaviour class. That’s pretty much it.

Collision

A collision occurs when two or more physical bodies collide with each other.

There are three callback methods related to Collisions, and these are called when:

  • OnCollisionEnter(Collision collision): The body collides for the first time with another body.
  • OnCollisionStay(Collision collision): The body is still colliding with another bodies. Remember that physics use a Fixed Timestep to update all colliders, so don’t expect this to be called every frame like an Update function.
  • OnCollisionExit(Collision collision): The body is no longer colliding with another body.

If you’re using 2D colliders / Rigidbodies, you’ll have to add the “2D” to those methods (OnCollisionEnter2D…)

There, I leaked my code just for you ;)
Just using OnCollisionEnter, I can emit particles, play sounds, or do whatever I define for the ball.

Trigger

A trigger, instead of a physical body, is a volume that detects if a body is currently overlapping with it.

To make a collider a trigger, you must mark it as such in the Inspector. Beware, if you’re working with a GameObject with a collider marked as trigger and a Dynamic Rigidbody, your object will pass through everything (you’re applying physics to a volume that can’t actually physically collide with other colliders).

Like the Collision methods, the three previous methods are also available for triggers, and these are called when:

  • OnTriggerEnter(Collider other): The trigger detects for the first time another body that has entered the volume.
  • OnTriggerStay(Collider other): The trigger is still detecting another bodies inside the volume. Remember that physics use a Fixed Timestep to update all colliders, so don’t expect this to be called every frame like an Update function.
  • OnTriggerExit(Collider other): The trigger is no longer detecting another body.
I use a trigger to detect if the ball has entered the hole, and send a event to the GameManager
In code…
…In Action!

Conclusion: When to use Physical Colliders and Triggers?

Use physical colliders if you want your body to still retain a physic interaction between bodies. (Impacts, platforms with collision interactions…).

Use triggers when you only want to detect objects through a volume, rather than a physical contact (coins, volumes to start cutscenes…).

--

--