Skip to main content

Detect Controller Collide

Detect Controller Collide

The Unity event OnControllerColliderHit is useful for knowing when a CharacterController has collided with another object. VRChat adds the event OnControllerColliderHitPlayer to detect when a CharacterController collides with another Player. This event includes a ControllerColliderPlayerHit struct with a reference to the VRCPlayerApi object for the player that was collided with.

Visit the Detect Controller Collide Example World to try it for yourself!

Using the Example

Start the scene in the Unity Editor or visit the world in VRChat. Look at the canvas in the world, and observe that the empty space below the "Last hit:" label changes to "Wall" when the capsule "character" collides with it.

Stand in-between the spawn point for the capsule and the wall so that it runs into you on the way to the wall, and you should see the label change to your display name when it collides with you! It will reset to its spawn point and continue running into you until you move aside. At that point, it will collide with the wall again and change the label back to "Wall".

Importing the Example

Follow the steps below to add this example to your Unity project:

  1. Open the Example Central Window from the window from the Unity Editor Menu under "VRChat SDK > 🏠 Example Central"
  2. Find this prefab in the list or search for it by title (same as the title of this page).
  3. Press the "Import" button to import the Unitypackage into your project.

Technical Breakdown

The scene has a DetectControllerCollide GameObject which contains the main prefabs and logic for the example.

The first child object, CharacterController, has a CharacterController component on it as well as an UdonBehaviour with a Graph Program with the logic for detecting collisions.

OnCharacterControllerHitExampleGraph

Variables

This graph has the following public variables:

NameDescription
CharacterControllerReference to the CharacterController, included to run the Move() method during Update().
float moveSpeedThe speed at which the characterController will move, multiplied internally by Time.deltaTime.
TextMeshProUGUI hitNameTextA reference to the target textField to update when a collision is detected.
Transform characterControllerStartPosThe position from which the characterController will start its journey each time.

Events

The graph has four events:

  • Update() runs continuously, calling Move() on the characterController to move it forward.
  • OnControllerColliderHit() runs whenever the characterController collides with a non-player object. The name of the object is extracted from the ControllerColliderHit struct passed through by the event, and set on the hitNameText field.
  • OnControllerColliderHitPlayer() runs whenever the characterController collides with a Player. The name of the Player is extracted from the ControllerColliderPlayerHit struct passed through by the event, and set on the hitNameText field.
  • __ResetCharacterController() is a custom event which is called from the two collision events to reset the characterController back to its starting position.