S&box Wiki


Detecting Triggers

To detect when an object enters or exits a trigger, both GameObjects must have a collider component attached, with the Is Trigger box checked on the trigger GameObject.

Then create a component on the same GameObject as the collider marked as a trigger, and inherit from Component, Component.ITriggerListener

public sealed class TriggerDebug : Component, Component.ITriggerListener { [Property] public NameTagPanel NameTag { get; set; } int iTouching; void ITriggerListener.OnTriggerEnter( Collider other ) { iTouching++; NameTag.Name = $"{iTouching} touching\n{other.GameObject.Name} entered"; } void ITriggerListener.OnTriggerExit( Collider other ) { iTouching--; NameTag.Name = $"{iTouching} touching\n{other.GameObject.Name} left"; } }
NameTagPanel is a custom razor component that displays text on a World Panel