How to Detect Collision in Unity: Simple Guide with Examples
In Unity, you detect collisions by using the
OnCollisionEnter or OnTriggerEnter methods inside a script attached to a GameObject with a Collider component. These methods run automatically when your object hits or overlaps another object with a Collider, allowing you to respond to the collision in code.Syntax
To detect collisions, you create a script with one of these methods:
void OnCollisionEnter(Collision collision): Called when this object's collider hits another collider and at least one has a Rigidbody component.void OnTriggerEnter(Collider other): Called when this object's collider marked as a trigger overlaps another collider.
Attach this script to a GameObject with a Collider component. For OnCollisionEnter, at least one object must have a Rigidbody. For OnTriggerEnter, the collider must be set as a trigger.
csharp
void OnCollisionEnter(Collision collision) { // Code to run when collision happens } void OnTriggerEnter(Collider other) { // Code to run when trigger overlap happens }
Example
This example shows how to detect when your player hits an obstacle and prints a message to the console.
csharp
using UnityEngine; public class CollisionDetector : MonoBehaviour { void OnCollisionEnter(Collision collision) { Debug.Log("Collided with " + collision.gameObject.name); } void OnTriggerEnter(Collider other) { Debug.Log("Triggered by " + other.gameObject.name); } }
Output
When the player hits another object with a collider, the console shows: "Collided with ObjectName" or "Triggered by ObjectName" if using triggers.
Common Pitfalls
- For
OnCollisionEnterto work, at least one object must have a Rigidbody component; otherwise, the method won't be called. - For
OnTriggerEnter, the collider must have "Is Trigger" checked in the Inspector. - Mixing triggers and collisions incorrectly can cause no detection.
- Not attaching the script to the correct GameObject will prevent detection.
csharp
/* Wrong: No Rigidbody on either object, so collision won't be detected */ // GameObjects have colliders but no Rigidbody /* Right: Add Rigidbody to one object */ // Add Rigidbody component to player or obstacle GameObject
Quick Reference
| Method | When to Use | Requirements |
|---|---|---|
| OnCollisionEnter(Collision collision) | Detect physical collisions | Both objects have Colliders; at least one has Rigidbody; colliders not set as triggers |
| OnTriggerEnter(Collider other) | Detect overlaps with trigger colliders | Collider set as trigger; at least one object has Rigidbody |
Key Takeaways
Use OnCollisionEnter to detect physical collisions between objects with Rigidbody and Collider components.
Use OnTriggerEnter to detect when an object enters a trigger collider area.
Ensure at least one object has a Rigidbody component for collision detection to work.
Set the collider's "Is Trigger" property correctly depending on the detection method.
Attach your collision detection script to the GameObject with the collider.