Triggers and collisions help your game know when objects touch or overlap. They let your game react to these events, like opening a door or stopping a character.
Trigger vs collision detection in Unity
Start learning this pattern below
Jump into concepts and practice - no test required
public class Example : MonoBehaviour { void OnTriggerEnter(Collider other) { // Code runs when another object enters this trigger } void OnCollisionEnter(Collision collision) { // Code runs when this object collides with another } }
OnTriggerEnter requires the collider to be set as a Is Trigger in Unity.
OnCollisionEnter requires both objects to have colliders and at least one to have a Rigidbody.
void OnTriggerEnter(Collider other) {
Debug.Log("Player entered the trigger area.");
}void OnCollisionEnter(Collision collision) {
Debug.Log("Player hit a wall.");
}void OnTriggerEnter(Collider other) {
if (other.CompareTag("Collectible")) {
Debug.Log("Collected an item!");
}
}void OnCollisionEnter(Collision collision) {
if (collision.gameObject.CompareTag("Enemy")) {
Debug.Log("Player bumped into an enemy!");
}
}This script prints messages when the object either triggers or collides with another object. Attach it to a GameObject with a Collider. Set the Collider's Is Trigger on to test triggers, or off to test collisions.
using UnityEngine; public class TriggerVsCollisionExample : MonoBehaviour { void OnTriggerEnter(Collider other) { Debug.Log("Trigger detected with " + other.name); } void OnCollisionEnter(Collision collision) { Debug.Log("Collision detected with " + collision.gameObject.name); } }
Time complexity: Both trigger and collision detection run efficiently within Unity's physics engine.
Common mistake: Forgetting to set Is Trigger for triggers or missing Rigidbody on objects for collisions.
Use triggers when you want to detect overlap without blocking movement. Use collisions when you want physical interaction and blocking.
Triggers detect when objects overlap without physical blocking.
Collisions detect physical contact and block movement.
Set Is Trigger for triggers; use Rigidbody and colliders for collisions.
Practice
Solution
Step 1: Understand trigger behavior
Triggers detect when objects overlap but do not physically block each other.Step 2: Understand collision behavior
Collisions detect physical contact and prevent objects from passing through each other.Final Answer:
Triggers detect overlaps without blocking movement, collisions detect physical contact and block movement. -> Option AQuick Check:
Trigger = overlap, Collision = block [OK]
- Confusing triggers with collisions as both blocking movement
- Thinking triggers require Rigidbody always
- Assuming collisions do not block movement
Solution
Step 1: Identify trigger setup
In Unity, to make a collider a trigger, you must enable its Is Trigger checkbox.Step 2: Verify other options
Adding Rigidbody or disabling collider does not make it a trigger; setting material to None affects physics but not trigger behavior.Final Answer:
Set the collider's Is Trigger property to true. -> Option CQuick Check:
Is Trigger = true for triggers [OK]
- Adding Rigidbody alone to make trigger
- Disabling collider thinking it triggers events
- Changing material instead of Is Trigger
void OnTriggerEnter(Collider other) {
Debug.Log("Triggered by " + other.name);
}
void OnCollisionEnter(Collision collision) {
Debug.Log("Collided with " + collision.gameObject.name);
}
What will be printed if an object with a collider marked as trigger overlaps another object with a collider and Rigidbody?Solution
Step 1: Analyze trigger event
When a collider marked as trigger overlaps another collider with Rigidbody, OnTriggerEnter is called.Step 2: Analyze collision event
OnCollisionEnter is called only when colliders physically collide (not triggers).Final Answer:
Only "Triggered by [object name]" will print. -> Option DQuick Check:
Trigger collider calls OnTriggerEnter only [OK]
- Expecting both trigger and collision messages
- Thinking OnCollisionEnter triggers on overlap
- Confusing collider types for events
void OnTriggerEnter(Collider other) {
Debug.Log("Triggered");
}
What is the most likely reason?Solution
Step 1: Check trigger setup
OnTriggerEnter only fires if the collider has Is Trigger enabled.Step 2: Verify other conditions
Rigidbody is needed on one object but missing Rigidbody alone won't prevent OnTriggerEnter if Is Trigger is off; method name is correct; collider disabled would prevent all events.Final Answer:
The collider's Is Trigger property is not enabled. -> Option BQuick Check:
Is Trigger must be true for OnTriggerEnter [OK]
- Forgetting to enable Is Trigger
- Assuming Rigidbody absence blocks trigger
- Misspelling method name
- Not enabling collider component
Solution
Step 1: Configure zone for overlap detection
Set the zone collider's Is Trigger to true so it detects player entering without blocking movement.Step 2: Configure wall for physical collision
Set the wall collider as non-trigger to physically block the player.Step 3: Rigidbody requirement
Add Rigidbody to the player so physics and trigger events work properly.Final Answer:
Set the zone collider as trigger (Is Trigger = true), the wall collider as non-trigger, and add Rigidbody to the player. -> Option AQuick Check:
Trigger zone + Rigidbody player + solid wall = correct setup [OK]
- Making wall a trigger so player passes through
- Not adding Rigidbody to player
- Setting zone collider as non-trigger blocking player
