0
0
Unityframework~5 mins

Trigger vs collision detection in Unity

Choose your learning style9 modes available
Introduction

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.

When you want to detect if a player enters an area without blocking their movement.
When you want to know if two objects hit each other and should bounce or stop.
When you want to collect items by touching them without physical impact.
When you want to trigger animations or sounds when objects collide.
When you want to prevent objects from passing through walls by detecting collisions.
Syntax
Unity
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.

Examples
This runs when an object enters a trigger zone. The player can pass through without stopping.
Unity
void OnTriggerEnter(Collider other) {
    Debug.Log("Player entered the trigger area.");
}
This runs when the player physically hits a wall or object and cannot pass through.
Unity
void OnCollisionEnter(Collision collision) {
    Debug.Log("Player hit a wall.");
}
Detects when the player touches a collectible item without blocking movement.
Unity
void OnTriggerEnter(Collider other) {
    if (other.CompareTag("Collectible")) {
        Debug.Log("Collected an item!");
    }
}
Detects a physical collision with an enemy object.
Unity
void OnCollisionEnter(Collision collision) {
    if (collision.gameObject.CompareTag("Enemy")) {
        Debug.Log("Player bumped into an enemy!");
    }
}
Sample Program

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.

Unity
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);
    }
}
OutputSuccess
Important Notes

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.

Summary

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.