S&box Wiki

Revision Difference

Triggers#561151

<cat>Code.Physics</cat>⤶ <title>Triggers</title>⤶ ⤶ # Detecting collisions⤶ ⤶ 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`⤶ ⤶ ```c#⤶ ⤶ 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";⤶ }⤶ ⤶ }⤶ ```⤶ ⤶ <note>NameTagPanel is a custom razor component that displays text on a **World Panel**</note>⤶ ⤶ <upload src="b4aa4/8dc3b149e74a703.mp4" size="4036001" name="sbox-dev_bMHJ0Vp4ne.mp4" />