A reliable zone system for scripts to detect local and or remote players within specified areas.
Works with players in stations seamlessly.
Then head to my VCC Listing and follow the instructions there.
Areas are defined using colliders (triggers or not). The local player detects which zones they are in using physics overlap checks rather than trigger enter and exit events. This makes the system reliable.
PlayerZone scripts define one zone, however said zone can be expanded using PlayerZoneExpansions in its children.
Player Zones and their colliders can be toggled at runtime, and if a player is inside of said zone an enter or exit event will get raised respectively. This is not instant however, can take up to 0.2s for the system to react.
When moving from one zone to another without a moment of being outside of both of them in between, the enter event for the new zone is guaranteed to always be raised before the exit event for the previous zone. Even when both happen within the same frame.
For remote players, when a script requires knowing about them, which zones players are in is synced through lockstep networking by this system.
Listening to events works simply by creating a script, such as the examples below, and then adding said script to a PlayerZone or its children.
using JanSharp;
using UdonSharp;
using UnityEngine;
namespace MySystem
{
[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public class MyLocalZoneListener : UdonSharpBehaviour
{
[PlayerZoneEvent(PlayerZoneEventType.OnLocalPlayerZoneEnter)]
public void OnLocalPlayerZoneEnter()
{
Debug.Log($"Local player entered {name}");
}
[PlayerZoneEvent(PlayerZoneEventType.OnLocalPlayerZoneExit)]
public void OnLocalPlayerZoneExit()
{
Debug.Log($"Local player exited {name}");
}
}
}
using JanSharp;
using UdonSharp;
using UnityEngine;
namespace MySystem
{
[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public class MyLocalAndRemoteZoneListener : UdonSharpBehaviour
{
[HideInInspector][SerializeField][FindInParent] private PlayerZoneAPI zone;
// These local events get raised immediately when the local player enters/exits.
[PlayerZoneEvent(PlayerZoneEventType.OnLocalPlayerZoneEnter)]
public void OnLocalPlayerZoneEnter()
{
Debug.Log($"Local player entered {name}");
}
[PlayerZoneEvent(PlayerZoneEventType.OnLocalPlayerZoneExit)]
public void OnLocalPlayerZoneExit()
{
Debug.Log($"Local player exited {name}");
}
// The enter and exit events below get raised for the local player too,
// however delayed by input action latency, as well as delayed until
// lockstep is initialized in the case of having just loaded into the world.
// For late joiners, to get events for players which are
// already in a zone at the time of loading into the world.
[PlayerZoneEvent(PlayerZoneEventType.OnPlayerAlreadyInZone)]
public void OnPlayerAlreadyInZone() => OnPlayerZoneEnter();
[PlayerZoneEvent(PlayerZoneEventType.OnPlayerZoneEnter)]
public void OnPlayerZoneEnter()
{
CorePlayerData player = zone.PlayerForEvent.core;
if (player.isLocal)
return;
Debug.Log($"{player.displayName} entered {name}");
}
[PlayerZoneEvent(PlayerZoneEventType.OnPlayerZoneExit)]
public void OnPlayerZoneExit()
{
CorePlayerData player = zone.PlayerForEvent.core;
if (player.isLocal)
return;
Debug.Log($"{player.displayName} exited {name}");
}
}
}
The PlayerZoneTriggerEventsLocal and PlayerZoneTriggerEventsLocalAndRemote scripts are bridges from PlayerZones to other scripts which were written using VRChat’s player trigger enter and exit events.
The Player Zones system, by default, configures a layer which collides with nothing. Which means they also do not collide with local nor remote players. Colliders on this layer will not raise any VRChat nor Unity trigger events.
The PlayerZone script will force the object it is on to the layer as defined by the PlayerZonesManager.
With all that in mind, in a lot of cases migrating an existing script setup can often be as simple as:
PlayerZone script to the same objectPlayerZoneTriggerEventsLocal script also to the same object
PlayerZoneTriggerEventsLocalAndRemote if the script requires remote player events)And that’s it. Only times this does not work are:
PlayerZonesManager (which gets created automatically on build or entering play mode)PlayerZoneTriggerEvents-Local/LocalAndRemote target said new objectThis project uses custom git filters to reduce the amount of noise generated by Udon and UdonSharp. These filters are not required in order to contribute, though without them all UdonSharp asset files will show as modified and those “changes” should not be committed.
com.jansharp.player-zones
Undefined
1.0.0 (Changelog)
2022.3 or later
No dependencies
No legacy packages