menu_bookDocumentation
Triggers: Non-Blocking Collision Detection with ITriggerListener
A trigger is a collider that detects when other physics objects enter or exit it without blocking their movement. Useful for pickup zones, death planes, checkpoints, and area detection.
Setting Up a Trigger
Any collider can become a trigger by ticking Is Trigger in the inspector, or in code:
CSHARP
var box = AddComponent<BoxCollider>();
box.Scale = new Vector3( 200, 200, 100 );
box.IsTrigger = true;Reacting to Trigger Events
Implement Component.ITriggerListener on any component on the same GameObject as the trigger collider:
CSHARP
public sealed class PickupZone : Component, Component.ITriggerListener
{
public void OnTriggerEnter( Collider other )
{
Log.Info( $"{other.GameObject.Name} entered the pickup zone" );
}
public void OnTriggerExit( Collider other )
{
Log.Info( $"{other.GameObject.Name} left the pickup zone" );
}
}Overloads provide both colliders (useful with multiple triggers on one GameObject):
CSHARP
public void OnTriggerEnter( Collider self, Collider other ) { }GameObject variants if you only care about the entering object:
CSHARP
public void OnTriggerEnter( GameObject other ) { }
public void OnTriggerExit( GameObject other ) { }Polling with Collider.Touching
If you prefer polling over events, Collider.Touching gives all colliders currently overlapping:
CSHARP
[Property] public BoxCollider Box { get; set; }
protected override void OnUpdate()
{
foreach ( var collider in Box.Touching )
{
Log.Info( $"{collider.GameObject.Name} is inside the box" );
}
}
Was this helpful?