Simple Door Toggle (Synced)
This script opens and closes a door for everyone by rotating a door transform between two angles.
Category: Networking Systems
UdonSharp Script
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
public class SimpleDoorToggleSynced : UdonSharpBehaviour
{
[Header("Door pivot transform")]
public Transform doorPivot;
[Header("Closed/Open Y rotation")]
public float closedY = 0f;
public float openY = 90f;
[UdonSynced] private bool isOpen;
private void Start()
{
ApplyDoorRotation();
}
public override void Interact()
{
VRCPlayerApi localPlayer = Networking.LocalPlayer;
if (localPlayer == null || doorPivot == null)
{
return;
}
Networking.SetOwner(localPlayer, gameObject);
isOpen = !isOpen;
ApplyDoorRotation();
RequestSerialization();
}
public override void OnDeserialization()
{
ApplyDoorRotation();
}
private void ApplyDoorRotation()
{
if (doorPivot == null)
{
return;
}
float targetY = isOpen ? openY : closedY;
Vector3 euler = doorPivot.localEulerAngles;
euler.y = targetY;
doorPivot.localEulerAngles = euler;
}
}
Unity Setup
- Create a door object with a correct hinge pivot.
- Create a handle/button object with
VRC_Interactable. - Add this script to the interactable object.
- Assign the door transform to
doorPivot. - Set
closedYandopenY.
Notes
- This basic version snaps instantly between states.
- For smooth animation, lerp rotation over time in
Update.
Extra Tips and Troubleshooting
Tips and Tricks
- Keep pivot placement correct before scripting.
- Add sound and indicator lights for better feedback.
Troubleshooting
- If door rotates oddly, verify local pivot axis and angle direction.
Related Content
Prefab Setup Notes
Import the prefab or script into a throwaway test scene before adding it to a live world. Confirm inspector references, ownership behavior, sync, triggers, UI hooks, and audio or object links before moving it into the production scene.
Testing Checklist
- Test once as a local player and again with a second client or late joiner if the behavior can affect more than one player.
- Confirm ownership, sync, trigger zones, UI references, and audio or object references are assigned intentionally.
- Check desktop and VR interaction distance so players can actually use the feature in context.
- Keep a backup of the scene before changing prefabs, UdonBehaviours, or serialized references.
