Using Events to bring your VRChat world to life Edit
The VRChat Event system is a key component of creating interactive worlds in VRChat. The Event system provides access to a range of built in functionality including:
- triggering particle systems
- hiding and showing objects or making them active / inactive
- adding health and damage to players (in conjunction with VRC_AddDamage)
- triggering custom scripted methods
- triggering animation and setting animation parameters
- teleporting players
The primary component script for the event system is VRC_EventHandler. This is where you set up all your events and what they will do. You can then use a variety of other methods to trigger those events including timers, collision triggers, animation events, etc.
Here's an example of a quick setup to make an object visible when a user enters a trigger volume.
We'll start by just setting up the hierarchy of objects first.
- Create an empty game object with GameObject -> Create Empty.
- Rename the object to TriggerGroup.
- Select the TriggerGroup in the hierarchy panel.
- Use GameObject -> Create Empty Child to create an empty game object under the TriggerGroup.
- rename the new object to "trigger".
- Use GameObject -> 3D Object -> Sphere to create the Sphere object.
- In the hierarchy panel, drag the Sphere onto the TriggerGroup to make it a child of the TriggerGroup.
- Use GameObject -> 3D Object -> Cube to create the Cube object.
- In the hierarchy panel, drag the Cube onto the TriggerGroup to make it a child of the TriggerGroup.
- scale the Cube or move it down so it looks more like the platform in the example.
- You should now have an object hierarchy that looks like the example. You should probably add a plane as well so the player has something to stand on ...
Set up the TriggerGroup to handle the events.
- select the TriggerGroup in the hierarchy panel.
- add a VRC_EventHandler script to it.
- set the size of the events to 1 to open up the settings for the event.
- the name of the event can be whatever you want but try to use CamelCase to keep the naming conventions consistent. For the example we are calling the event "ShowBall". When this event is triggered it will do all the stuff defined in the event.
- the event type is MeshVisibility. It's important to note that this will turn on the MeshRenderer on the Sphere rather than enabling the whole object.
- The Parameter Bool is set to "true". If you wanted to hide the Sphere instead you could set this to "false". Note the that other parameters are not used for this type of event.
- The Parameter Object should be set to the object we want to make visible. You can just drag the Sphere from the hierarchy panel onto the Parameter Object field in the Inspector to create this reference.
Set up the trigger object that will detect when the player walks into it and trigger the event. The trigger has a BoxCollider and a VRC_TriggerColliderEventTrigger on it to pass the event up to the TriggerGroup.
- select the trigger object and use Component -> Physics -> Box Collider to add a collider to it. Ensure that the "IsTrigger" option is checked if you don't want players to collide with this object.
- move the trigger object around to so it is above the Cube. You can click the little "Edit Collider" button to adjust the collider's size and shape.
- add a VRC_TriggerColliderEventTrigger script to the trigger object.
- set the Enter Event Name parameter to "ShowBall" or whatever you called the event in the VRC_EventHandler.
Turn off the visibility of the Sphere by selecting the sphere and turning off the MeshRenderer component. You need to keep the Sphere object enabled for the MeshVisibility event to work.
That's it. You just need to add a VRCWorld prefab to your scene and you should be able to test it.
Here's a summary of how this works just to be sure this is clear.
- The player walks into the trigger collider on the trigger object.
- this triggers VRC_TriggerColliderEventTrigger to send the "ShowBall" event up the hierarchy.
- the "ShowBall" event is received by the VRC_EventHandler on the TriggerGroup object.
- the "ShowBall" event turns on the MeshRenderer on the Sphere object.
If you want to go a bit further with this you could add another event for when the player leaves the trigger.
Tip: If you want to do more than one thing with the same event you can just create additional events with the same name. When the event is triggered all events with the same name will fire off in order.