This set of scripts allows world creators to restrict access to specific areas in their VRChat worlds and provide an interactive mechanism for players to gain access via button press-toggle.
What does the script do? #
Imagine you have a VIP room in a virtual club, and you want only certain people to enter. This script acts like a bouncer at the door of that VIP room. If someone who isn’t on the VIP list tries to walk in, the bouncer (our script) teleports them out to a designated spot, say, the club’s entrance.
Now, there’s also a special button in the club. When someone presses this button, they get added to the VIP list and can then enter the VIP room without being teleported out.
How does it work? #
- The VIP Room’s Door (Restricted Area):
- The script first sets up an invisible boundary using a box collider (like an invisible wall or door). This creates the VIP area.
- If anyone tries to walk into this invisible boundary without VIP access, they get teleported out.
- The VIP List:
- Only people on this list can enter the VIP room.
- The Toggle Button:
- There’s a button in the club. When someone presses this button, their name gets added to the VIP list.
- Once they’re on the list, they can walk into the VIP room without any issues.
- Constant Checking:
- The script is always watching. It’s like a security camera that’s always on.
- If it sees someone in the VIP room who isn’t on the VIP list, it immediately teleports them out.
Introduction #
The VRChat UdonSharp Permissions Scripts are designed to enhance world interactivity and security. By using these scripts, world creators can:
- Designate restricted areas in their worlds.
- Provide interactive objects (like buttons) that players can use to gain access to these areas.
Getting Started #
Prerequisites:
- Unity with VRChat SDK3 – Worlds.
- UdonSharp compiler installed in your Unity project.
Installation:
- Download the scripts from the repository.
- Import the scripts into your Unity project.
Script Descriptions #
1. ColliderPermissionCheck:
- Purpose: Manages player permissions and ensures unauthorized players are teleported out of a restricted area.
- Key Features:
- Designate restricted areas using colliders.
- Teleport unauthorized players to a specified location.
- Maintain a list of players with access permissions.
2. BoxButtonPermission:
- Purpose: Turns a GameObject into an interactive button that grants players permission to enter the restricted area.
- Key Features:
- Easily link to the
ColliderPermissionCheck
script. - Grant players access permissions upon interaction.
- Easily link to the
Integration Guide #
Setting up the Restricted Area:
- Attach the
ColliderPermissionCheck
script to a GameObject. - Assign a collider to the
restrictedCollider
field. This collider represents the restricted area. - Set a teleport location using the
teleportLocation
field. Unauthorized players will be teleported to this location.
Setting up the Permission Button:
- Create a GameObject (e.g., a cube) to act as the button.
- Attach the
BoxButtonPermission
script to this GameObject. - Link the
ColliderPermissionCheck
script to thepermissionManager
field in theBoxButtonPermission
script. - Ensure the GameObject has a collider set as a trigger.
FAQs #
Q: Can I use multiple restricted areas in one world? A: Yes, you can attach the ColliderPermissionCheck
script to multiple GameObjects, each with its own restricted collider.
Q: Can players lose their permissions? A: The current scripts provide a mechanism to grant permissions. If you wish to revoke permissions, additional scripting would be required.
ColliderPermissionChecks.cs #
/*
* ColliderPermissionChecks
* Author: AussieGuy92
* License: Open Source (MIT License)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
public class ColliderPermissionCheck : UdonSharpBehaviour
{
public Collider restrictedCollider;
public Transform teleportLocation;
public VRCPlayerApi[] playersWithPermission = new VRCPlayerApi[50]; // Assuming a max of 50 players with permission
private VRCPlayerApi[] allPlayers = new VRCPlayerApi[80]; // Assuming a max of 80 players in the instance
private void Start()
{
// Add the local player to the array when the script starts
AddPlayerToArray(Networking.LocalPlayer, allPlayers);
}
public override void OnPlayerJoined(VRCPlayerApi player)
{
AddPlayerToArray(player, allPlayers);
}
public override void OnPlayerLeft(VRCPlayerApi player)
{
RemovePlayerFromArray(player, allPlayers);
}
private void OnTriggerEnter(Collider other)
{
VRCPlayerApi player = other.GetComponent<VRCPlayerApi>();
if (player == null) return;
if (!HasPermission(player))
{
player.TeleportTo(teleportLocation.position, teleportLocation.rotation);
}
}
private void Update()
{
// Check all players inside the collider every frame
foreach (VRCPlayerApi player in allPlayers)
{
if (player == null) continue;
if (restrictedCollider.bounds.Contains(player.GetPosition()) && !HasPermission(player))
{
player.TeleportTo(teleportLocation.position, teleportLocation.rotation);
}
}
}
public void GrantPermission(VRCPlayerApi player)
{
if (!HasPermission(player))
{
AddPlayerToArray(player, playersWithPermission);
}
}
private bool HasPermission(VRCPlayerApi player)
{
foreach (VRCPlayerApi permittedPlayer in playersWithPermission)
{
if (permittedPlayer == player)
{
return true;
}
}
return false;
}
private void AddPlayerToArray(VRCPlayerApi player, VRCPlayerApi[] array)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] == null)
{
array[i] = player;
break;
}
}
}
private void RemovePlayerFromArray(VRCPlayerApi player, VRCPlayerApi[] array)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] == player)
{
array[i] = null;
break;
}
}
}
}
BoxButtonPermission.cs #
/*
* BoxButtonPermission
* Author: AussieGuy92
* License: Open Source (MIT License)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
public class BoxButtonPermission : UdonSharpBehaviour
{
public ColliderPermissionCheck permissionManager;
public override void Interact()
{
VRCPlayerApi localPlayer = Networking.LocalPlayer;
if (localPlayer != null)
{
permissionManager.GrantPermission(localPlayer);
}
}
}
Usage #
ColliderPermissionCheck #
This script restricts players from entering a specific collider unless they have been granted permission.
Setup:
- Attach the
ColliderPermissionCheck
script to a GameObject. - Assign the restricted collider to the
restrictedCollider
field. - Set a teleport location using the
teleportLocation
field. Unauthorized players will be teleported to this location. - (Optional) Pre-assign players with permission using the
playersWithPermission
array.
BoxButtonPermission #
This script allows players to interact with a box (or any other object) to gain permission to enter the restricted collider.
Setup:
- Attach the
BoxButtonPermission
script to a GameObject (e.g., a cube). - Ensure the GameObject has a collider set as a trigger.
- Link the
ColliderPermissionCheck
script to thepermissionManager
field in theBoxButtonPermission
script.
Usage:
- Players can interact with the GameObject to gain permission to enter the restricted collider.