Collisions and Triggers in Unity
For every action, there is an equal and opposite reaction — Third Newton’s Law
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.
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, and it is called once per frame for every body that is touching the body that implements this method.
- 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…)
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, and it is called once per frame for every body that is overlapping with the body that implements this method.
- OnTriggerExit(Collider other): The trigger is no longer detecting another body.
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…).