Unity Collision Detection
Introduction
Collider interactions in Unity can be quite complex, but understanding them is crucial for developing physics-based interactions in your game or simulation. In this article, we will explore various aspects of Unity collision detection, Unity triggers, and rigidbody components.
You will learn everything you need to know about Unity collisions. You will understand how Unity collision detection works. You will be familiar with the on trigger enter, on trigger exit, on collision enter, and on collision exit methods. And, you will be familiar with Unity rigidbody interactions.
This is a series on Unity Physics. We recommend reading the series in order.
- Unity Physics
- Unity Collision Detection
- Character Controller and Input Systems
- Rigidbody Mass in Unity
- How to add Friction to a Rigidbody
- Cylinder Collider in Unity
- Box Collider in Unity
- Box Cast in Unity
- Sorting Layers
- Get the distance between two objects
- How to normalize a vector
Physics Interactions Matrix
To make it easy to understand how physics objects interact with each other, we can create a physics interaction matrix. This matrix provides an overview of the various collider states and their relationships. It helps us determine whether Unity will handle the interaction as a collision or a trigger based on the object types involved.
If you’re confused about how to interpret this matrix, let’s delve deeper into the different collider states and how they relate to each other. Once we have a clear understanding, we can refer back to the matrix for a comprehensive overview.
Unity Physics Messages
Unity monitors interactions between scene objects that can collide. Unity has a set of physics messages that it calls based on these interactions. These messages are known as physics callbacks. You can include methods for these message calls in your MonoBehaviour in order to trigger code when these events occur.
There are six basic physics messages available in MonoBehaviour:
- OnCollisionEnter
- OnCollisionStay
- OnCollisionExit
- OnTriggerEnter
- OnTriggerStay
- OnTriggerExit
Unity also has corresponding versions of these methods applicable to 2D Collision and Trigger tests:
- OnCollisionEnter2D
- OnCollisionStay2D
- OnCollisionExit2D
- OnTriggerEnter2D
- OnTriggerStay2D
- OnTriggerExit2D
Differences between Collisions and Triggers
Let’s examine the differences between collisions and triggers in more detail:
- When two physics objects collide, Unity calls the On[Trigger/Collision]Enter method once for each object.
- For each frame that two physics objects remain in contact, Unity calls the On[Trigger/Collision]Stay method once for each object.
- When the two physics objects separate contact, Unity calls the On[Trigger/Collision]Exit method once for each object.
The same applies to triggers, but with the Unity On Trigger Enter, On Trigger Stay, and On Trigger Exit methods. Trigger colliders are special in that they have the “Is Trigger” flag enabled, which means they no longer register physical collisions with other rigidbody colliders. Instead, they register trigger events when they collide with any rigidbody collider. This enables you to create interactions that are based on triggers rather than physical collisions.
Unity has provided more information about each of these methods along with usage examples on their API docs.
Understanding Physics Update and Render Updates
Collision detection occurs during physics fixed update frames. The OnCollisionStay and OnTriggerStay events are called during the physics update. Since the physics update doesn’t occur every frame, OnCollisionStay and OnTriggerStay may not necessarily be called every frame.
Create a free account, or log in.
Gain access to free articles, game development tools, and game assets.