Skip to main content

Player Collisions

Udon has three ways to detect when a Player and an Object Collide - Triggers, Physics, and Particles.

Triggers

If you want to detect when a player has entered or exited an area, your best bet will be to use the **OnPlayerTrigger **events. There are three of these:

  • OnPlayerTriggerEnter is called when a player's capsule enters a Trigger Collider
  • OnPlayerTriggerStay is called on frames while a player's capsule is inside a Trigger Collider
  • OnPlayerTriggerExit is called when a player's capsule exits a Trigger Collider.

A simple Box Collider with 'Is Trigger' checked.

To use these events, add an object with a collider component and enable the 'Is Trigger' box on the collider. A trigger collider allows objects and players pass through it and calls events when those objects have colliders. You can learn more about Collision in Unity's documentation.

Edge Cases

There are some edge cases where one of these events could be skipped, like when a player teleports out of a collider, or is moving VERY fast. We'll add in handling in the future to catch these.

Physics

There is another set of events you can use when you've got objects like bouncing balls or bullets that you're moving around with physics. These objects have Colliders with IsTrigger turned off so that they'll interact with the environment and each other.

To detect events on these Colliders, you can use:

  • OnPlayerCollisionEnter is called when a player's capsule enters a Collider.
  • OnPlayerCollisionStay is called on frames while a player's capsule is inside a Collider.
  • OnPlayerCollisionExit is called when a player's capsule exits a Collider.
  • OnControllerColliderHitPlayer is called when a CharacterController hits a Player.
OnPlayerCollision Events are for Moving Objects

These events WILL NOT be called when a player 'walks into' a stationary object. If you want to handle that, use a Trigger Collider.

Particles

Finally, you can use OnPlayerParticleCollision to detect when a Particle colliders with a player, assuming that Particle System has Collision and Send Collision Messages turned on.

This Particle System has the Collision module turned on, is set to 'World' and '3D' modes, with 'Send Collision Messages' turned on.

Examples

The Udon example scene demonstrates how to use all three methods of detecting player collisions.

Check out the Udon Example Scene to see how these events can be used.