S&box Wiki

Revision Difference

Collisions#563003

<cat>Code.Physics</cat> <title>Collisions</title> ⤶ <note>This page is for detecting collisions between non-triggers, to detect collision with triggers go to https://wiki.facepunch.com/sbox/Triggers</note>⤶ # Detecting Collisions To detect when 2 colliders touch, both objects must have some kind of collider component attached. Then create a component on the `same GameObject` as one of these colliders and inherit from `Component, Component.ICollisionListener`⤶ ⤶ <note>No code example for now, still waiting on a fix for ragdolls to work properly with this :/</note>⤶ To detect when an object touches, or stops touching another, both GameObjects must have a collider component attached(box, sphere, etc), with the `Is Trigger` box unchecked on both of them.⤶ For a component to detect triggers, it must be on the `same GameObject` as one of the collider components.⤶ ⤶ <upload src="b4aa4/8dd11796d7ad41a.png" size="14526" name="image.png" />⤶ ⤶ First inherit from `Component, Component.ICollisionListener`, then implement `public void OnCollisionStart()` or `public void OnCollisionStop`, `public void` is necessary. In the parenthesis, add `Collision collision`. This sets a variable "collision" to the collision event so you can use it in code.⤶ ⤶ ```cs⤶ public sealed class ColliderExample : Component, Component.ICollisionListener⤶ {⤶ public void OnCollisionStart( Collision collision )⤶ {⤶ Log.Info( $"{collision.Self} Started Touching {collision.Other}" );⤶ }⤶ public void OnCollisionStop( CollisionStop collision )⤶ {⤶ Log.Info( $"{collision.Self} Stopped Touching {collision.Other}" );⤶ }⤶ }⤶ ```⤶