Trigger events
- Game Development course
- Beginner
- Lesson 53/83
- 4 min
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.
Practice Print trigger state
Assign "OnTriggerEnter" to triggerType and "Checkpoint" to zoneName. Print Trigger: OnTriggerEnter | Zone: Checkpoint to the console.
Starter code
using System;
class Program
{
static void Main()
{
// triggerType ve zoneName değerlerini tanımlayıp yazdır
}
}Sample solution
using System;
class Program
{
static void Main()
{
string triggerType = "OnTriggerEnter";
string zoneName = "Checkpoint";
Console.WriteLine("Trigger: " + triggerType + " | Zone: " + zoneName);
}
}Expected output
Trigger: OnTriggerEnter | Zone: Checkpoint
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 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 Correct answer
- 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
Explanation: Is Trigger allows objects to pass through without physical bounce while still signaling contact events.
2 Set IsTrigger property
collider.____ = true;
collider.isTrigger = true;
Explanation: To turn a Collider into a trigger, the isTrigger property is assigned true.
3 Trigger terms
| Term | Meaning |
|---|---|
Is Trigger |
Turns collider into trigger zone |
OnTriggerEnter |
Called when entering trigger |
OnTriggerExit |
Triggered when leaving trigger |
Collider other |
Reference to entering collider |