Unity Collision Detection

by Michael Sacco Published May 18, 2023

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.

  1. Unity Physics
  2. Unity Collision Detection
  3. Character Controller and Input Systems
  4. Rigidbody Mass in Unity
  5. How to add Friction to a Rigidbody
  6. Cylinder Collider in Unity
  7. Box Collider in Unity
  8. Box Cast in Unity
  9. Sorting Layers
  10. Get the distance between two objects
  11. 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.

What does “Is Trigger” mean on a Collider?

The “Is Trigger” flag on a Collider determines whether it is marked as a trigger collider. When the flag is enabled, the collider functions as a Unity trigger collider. Trigger colliders do not register collisions with other rigidbodies but instead generate trigger events.

Trigger Colliders and Rigidbody Trigger Colliders

Trigger colliders register the appropriate OnTrigger events when they collide with any rigidbody collider. On the other hand, rigidbody trigger colliders generate the OnTrigger events when they collide with any collider, regardless of whether it has a rigidbody component or not.

Symmetry in OnTrigger and OnCollision Events

It is important to understand that the OnTrigger or OnCollision message is sent to both objects that participate in the event.

In other words, if Object A collides with Object B and triggers the OnCollisionEnter method for Object B, then Object B simultaneously collides with Object A and triggers the OnCollisionEnter method for Object A. This symmetry ensures that both objects involved in the interaction can respond accordingly.

The mental model you can use to understand this is just imagine that Unity simply checks to see if any physics objects overlap this frame. If they didn’t overlap in the last frame, Unity triggers OnCollisionEnter for both objects.

How Trigger Colliders Interact with Other Colliders

Let’s consider a scenario where you have one trigger collider and one rigidbody collider. When these two objects collide, Unity calls the OnTrigger[Enter/Stay/Exit] messages on both colliders. However, Unity does not trigger the OnCollision[Enter/Stay/Exit] methods on either collider since triggers do not produce physical collisions.

Rigidbody Component and Collisions

It’s worth noting that not every collider needs to have a Rigidbody. Colliders without a Rigidbody are known as static colliders.

Some examples of common use cases for static colliders include walls, floors, and ramps.

Colliders that do have a rigidbody will be treated as dynamic colliders. Some examples of dynamic colliders include soccer balls, moveable crates, and player characters.

Dynamic colliders can interact with static colliders, but the static colliders themselves won’t move as a result of the interaction. For example, if you throw a dynamic collider like a crate into a static collider like a wall, Unity will evaluate the collision and adjust the dynamic collider’s position and movement based on the collision, while the wall remains stationary.

All Unity Physics Messages, Broken Down

OnCollisionEnter

Unity calls OnCollisionEnter in the first frame that a given non-kinematic rigidbody collider collides with a non-trigger variant of a collider, a non-kinematic rigidbody collider, or a kinematic rigidbody collider.

OnCollisionStay

Unity calls OnCollisionStay for every frame that a given non-kinematic rigidbody collider is colliding with a non-trigger variant of a collider, a rigidbody collider, or a kinematic rigidbody collider.

OnCollisionExit

Unity calls OnCollisionExit in the first frame that a given non-kinematic rigidbody collider stops colliding with a non-trigger variant of a collider, a rigidbody collider, or a kinematic rigidbody collider.

OnTriggerEnter

Unity calls OnTriggerEnter in the first frame that a given trigger collider collides with trigger- or non-trigger variant of a rigidbody collider or a kinematic rigidbody collider.

Unity will also call OnTriggerEnter in the first frame that a given trigger rigidbody collider or trigger kinematic rigidbody collider collides with any other physics object.

OnTriggerStay

Unity calls OnTriggerStay for every frame that a given trigger collider is colliding with trigger- or non-trigger variant of a rigidbody collider or a kinematic rigidbody collider.

Unity will also call OnTriggerStay for every frame that a given trigger rigidbody collider or trigger kinematic rigidbody collider is colliding with any other physics object.

OnTriggerExit

Unity calls OnTriggerEnter in the first frame that a given trigger collider stops colliding with trigger- or non-trigger variant of a rigidbody collider or a kinematic rigidbody collider.

Unity will also call OnTriggerEnter in the first frame that a given trigger rigidbody collider or trigger kinematic rigidbody collider stops colliding with any other physics object.

Glossary of Unity Physics Terms

The following terms and their definitions will provide a foundation for understanding the different collider and rigidbody types in Unity. You can use this understanding to use these features in physics simulations and gameplay mechanics.

Collider

A component in Unity that defines the shape and physical boundaries of an object. It is used for collision detection and interaction with other colliders in the scene.

Rigidbody Collider

A collider component that is associated with a Rigidbody. It allows the object to be affected by physics forces such as gravity, collisions, and forces applied programmatically.

Kinematic Rigidbody Collider

A specific type of Rigidbody Collider that is marked as “kinematic.” Kinematic rigidbodies are not affected by external physics forces but can be moved programmatically. They can still interact with other non-kinematic rigidbody colliders in the scene as well as all types of trigger colliders.

Trigger Collider

A collider component with the “Is Trigger” flag enabled. Trigger colliders do not physically collide with other colliders but generate trigger events when they come into contact with other rigidbody colliders. They are commonly used for implementing gameplay mechanics or detecting specific events.

Trigger Rigidbody Collider

A trigger collider component associated with a Rigidbody. When this collider encounters other colliders, trigger events are generated without causing physical collisions.

Trigger Kinematic Rigidbody Collider

A trigger collider component associated with a kinematic rigidbody. Similar to the trigger rigidbody collider, this collider generates trigger events without causing physical collisions.

Box Collider

A Box collider is a collider in the shape of a rectangular cuboid.

Cylinder Collider

A Cylinder collider is a collider in the shape of a cylinder.

FAQs

Can two triggers collide?

Two colliders with Is Trigger enabled do not collide using the physics system.

These colliders do not receive OnTrigger[Enter/Stay/Exit] method calls.

These colliders do not receive OnCollider[Enter/Stay/Exit] method calls.

OnCollisionEnter vs. OnTriggerEnter?

OnCollisionEnter is called when two colliders with rigidbodies collide, generating a physical collision. OnTriggerEnter is called when a collider marked as a trigger (Is Trigger enabled) comes into contact with another collider, generating a trigger event. Trigger events do not produce physical collisions.

How do I detect collisions between objects in Unity?

To detect collisions between objects in Unity, you need to attach colliders to the objects involved in the interaction. You can then implement collision detection and response logic by utilizing the OnCollisionEnter, OnCollisionStay, and OnCollisionExit methods (or their 2D counterparts) in your MonoBehaviour scripts. Remember that one of the two objects will need a Rigidbody in order to register OnCollision events.

What is a collision in Unity?

In Unity, a collision occurs when two objects with colliders come into contact and interact physically. It can involve objects with rigidbodies or static colliders, and it triggers events and physics calculations based on the collision.

What is a trigger in Unity?

In Unity, a trigger is a special type of collider component that generates trigger events when it comes into contact with other colliders. Unlike regular colliders, triggers do not produce physical collisions but are used for detecting specific events or implementing gameplay mechanics.

What is the difference between a trigger and a regular collider?

The main difference between a trigger and a regular collider is that triggers do not generate physical collisions. Instead, they generate trigger events when they come into contact with other colliders, allowing you to implement custom logic or detect specific interactions without affecting the physical simulation.

Can a trigger interact with multiple colliders at the same time?

Yes, a trigger collider can interact with multiple other colliders at the same time. When a trigger collider overlaps with multiple colliders, Unity generates trigger events for each overlapping collider, allowing you to handle the interactions individually.

Can triggers be used for detecting proximity or area-based events?

Yes. By defining trigger colliders in specific areas of your scene, you can detect when other objects enter, stay within, or exit those areas. This can be useful for implementing area-of-effect effects, proximity-based interactions, or triggering specific actions based on the player’s position.

Recommended Reading

Free download: Indie Game Marketing Checklist

Download now

Category

unity basics

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    A lightweight procedural skybox ideal for semi-stylized projects.
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Drag-and-drop ready-to-use ambient, impact, and spell particle effects.
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.

Free Indie Game Marketing Checklist

Learn how to make your game successful with this handy checklist.

Download for free