Game Development
Trigger events
Learn OnTriggerEnter with IsTrigger and trigger volumes.
Trigger events
When the Is Trigger checkbox on a Collider is checked, the object loses its solid barrier trait. Other objects pass through this volume unhindered, but the contact is detected programmatically.
// Converting Collider into a trigger
Collider col = GetComponent<Collider>();
col.isTrigger = true;
Collecting coins, checkpoints, or door-opening proximity detectors are typically designed as Triggers.
OnTriggerEnter and Parameter Difference
When entering a trigger boundary, OnTriggerEnter(Collider other) is invoked. Unlike OnCollisionEnter, this method accepts the entering object's Collider reference directly instead of a Collision object.
void OnTriggerEnter(Collider other) {
if (other.CompareTag("Player")) {
Debug.Log("Reward collected!");
}
}
When leaving the boundary, the OnTriggerExit(Collider other) callback fires.
Exercise Print trigger state
Assign "OnTriggerEnter" to triggerType and "Checkpoint" to zoneName. Print Trigger: OnTriggerEnter | Zone: Checkpoint to the console.
Show example solution
using System;
class Program
{
static void Main()
{
string triggerType = "OnTriggerEnter";
string zoneName = "Checkpoint";
Console.WriteLine("Trigger: " + triggerType + " | Zone: " + zoneName);
}
}
Test yourself
-
Why is a Collider configured with 'Is Trigger' when creating a collectible object (e.g. coin)?
- So the player can pass through the object while contact is detected
- To double gravity and nail the object permanently to ground
- To stop all audio sources in the scene and freeze gameplay
- To completely disable material and shadow calculation for the object
Show answer
So the player can pass through the object while contact is detected — Is Trigger allows objects to pass through without physical bounce while still signaling contact events.
-
Set IsTrigger property
collider.____ = true;
Show answer
collider.isTrigger = true;
-
Trigger terms
- Is Trigger
- Turns collider into trigger zone
- OnTriggerEnter
- Called when entering trigger
- OnTriggerExit
- Triggered when leaving trigger
- Collider other
- Reference to entering collider
Next lesson: Summary - Physics