Udon has three ways to detect when a Player and an Object Collide - Triggers, Physics, and Particles.
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.
To use these events, add an object with a collider and check the 'Trigger' box on the collider. A Trigger Collider lets objects and players pass through it and calls events when those objects have colliders. You can learn more about Collision in the Unity Manual.
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.
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.
These events WILL NOT be called when a player 'walks into' a stationary object. If you want to handle that, use a Trigger Collider.
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.
The Udon example scene demonstrates how to use all three methods of detecting player collisions.