OnCollisionEnter vs. OnTriggerEnter: What’s the Difference & When to Use Them?
Learn the key differences between OnCollisionEnter
and OnTriggerEnter
in Unity, and discover when to use each for physics-based interactions, obstacles, and trigger zones!
Introduction to OnCollisionEnter
vs. OnTriggerEnter
in Unity
In Unity, OnCollisionEnter
and OnTriggerEnter
are two key methods used for detecting interactions between GameObjects. While both handle collisions, they work differently and serve distinct purposes.
OnCollisionEnter
is used when objects physically collide and should react with real-world physics, like a ball hitting a wall or a character bumping into an obstacle.OnTriggerEnter
, on the other hand, is used for non-physical interactions, such as detecting when a player enters a checkpoint, picking up a power-up, or passing through an invisible zone.
Understanding when to use collisions vs. triggers is essential for creating smooth gameplay interactions. In this article, we’ll break down how they work, when to use each one, and common use cases to help you build better physics-based mechanics in Unity.
What is OnTriggerEnter
?
OnTriggerEnter
is a Unity method that detects when a GameObject enters a trigger collider. Unlike OnCollisionEnter
, it does not require physical collisions—instead, it detects when an object passes through a trigger zone.
Key Differences from OnCollisionEnter
:
- No physical collision — The object passes through.
- Used for interactive areas — Like power-ups, doors, checkpoints, or triggers.
- Requires “Is Trigger” enabled — The collider must be marked as a trigger.
How to Use OnTriggerEnter
in Unity
To use OnTriggerEnter
, follow these steps:
1️. Create an object with a Collider
2. Check “Is Trigger” in the Inspector (so it acts as a trigger, not a solid object).
3. Write a script that detects when something enters the trigger.
Example Code: Basic OnTriggerEnter
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
Debug.Log("Player entered the trigger!");
}
}
This detects when the Player enters the trigger and logs a message.
To better understand how OnTriggerEnter
and trigger-based interactions work in Unity, I have created a small project that visually demonstrates these functions in action.
In this project, the player controls a rolling ball that can move left and right. As the player explores, they will interact with three different trigger-based objects — each showcasing how OnTriggerEnter
can be used for gameplay mechanics such as power-ups and environmental triggers.
Now, let’s take a look at the three interactive power-ups and how they work:
How OnTriggerEnter
Works in My Project
In my project, OnTriggerEnter
is used for three different trigger-based interactions:
Speed Boost Power-Up
Jump Boost Power-Up
Sound Zone
What is OnCollisionEnter
?
OnCollisionEnter
is a Unity method that detects when a GameObject physically collides with another object. Unlike OnTriggerEnter
, which only detects when an object passes through a trigger, OnCollisionEnter
requires both objects to have colliders and at least one to have a Rigidbody.
This method is used when you want objects to physically interact, such as stopping movement, bouncing, or breaking on impact.
Key Differences from OnTriggerEnter
:
- Requires physical collision — Objects must physically collide with each other.
- Used for solid interactions — Like walls, obstacles, breakable objects, and bouncing surfaces.
- Does NOT require “Is Trigger” enabled — The colliders must remain normal (not triggers) to register collisions.
How to Use OnCollisionEnter
in Unity
To use OnCollisionEnter
, follow these steps:
1️. Create an object with a Collider
- Ensure the object you want to detect collisions with has a BoxCollider, SphereCollider, or MeshCollider.
2. Attach a Rigidbody (if needed)
- At least one of the objects involved in the collision must have a Rigidbody for Unity’s physics engine to detect the collision.
3 ️. Write a script that detects when a collision happens.
Example Code: Basic OnCollisionEnter
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Wall"))
{
Debug.Log("Player collided with the wall!");
}
}
This script detects when the player collides with a “Wall” object and logs a message in the console.
How OnCollisionEnter
Works in My Project
In my project, OnCollisionEnter
is used for three different physical collision-based interactions:
Wall Collision
Bouncy Surface Collision
Breakable Object Collision
Why Collisions and Triggers Are Important in Games
Collisions and triggers are essential mechanics in game development, shaping how objects and characters interact within the game world. OnCollisionEnter
ensures that physical objects behave realistically, stopping movement, bouncing, or breaking on impact. Meanwhile, OnTriggerEnter
allows for non-physical interactions, like activating power-ups, playing sounds, or opening doors. Understanding when to use each function is key to creating engaging gameplay, whether it's triggering events or enforcing realistic physics-based mechanics.
But What About 2D Projects?
If you’re working on a 2D game, the process remains the same, but instead of OnCollisionEnter
and OnTriggerEnter
, you’ll use OnCollisionEnter2D
and OnTriggerEnter2D
.
These 2D-specific methods work the same way as their 3D counterparts:
✅ OnCollisionEnter2D
– Detects physical collisions between 2D objects with Rigidbodies and Colliders.
✅ OnTriggerEnter2D
– Detects when an object enters a trigger area without requiring physical contact.
In 2D platformers, OnCollisionEnter2D
is commonly used for detecting ground to enable jumping, while OnTriggerEnter2D
is used for coins, power-ups, and hazards.
So whether you’re working in 2D or 3D, the same logic applies — just switch to the 2D
versions of these methods!
Try It Yourself on Itch.io!
You can test this project on Itch.io to see OnCollisionEnter
and OnTriggerEnter
in action. Roll into objects to experience physical collisions, interact with power-ups, and trigger different events. It’s a simple but effective way to understand when to use collisions versus triggers in Unity.