TimedTeleport.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using UdonSharp;
  2. using UnityEngine;
  3. using VRC.SDKBase;
  4. [UdonBehaviourSyncMode(BehaviourSyncMode.NoVariableSync)]
  5. [AddComponentMenu("Artyom Scripting System/Timed Teleport")]
  6. [DisallowMultipleComponent]
  7. public class TimedTeleport : UdonSharpBehaviour
  8. {
  9. [Header("Teleportation Settings")]
  10. public Transform teleportTarget; // The transform to teleport the player to
  11. public float delay = 1f; // The delay in seconds before teleportation
  12. [Tooltip("Leaving the trigger before the delay is up cancels the teleport.")]
  13. public bool cancelOnExit = false;
  14. private bool pending; // A teleport is waiting: entering again doesn't queue another
  15. public override void OnPlayerTriggerEnter(VRCPlayerApi player)
  16. {
  17. if (!player.isLocal || pending) return;
  18. if (teleportTarget == null)
  19. {
  20. Debug.LogError("[AUMSS] Timed Teleport: Teleport Target isn't assigned.");
  21. return;
  22. }
  23. pending = true;
  24. SendCustomEventDelayedSeconds(nameof(TeleportPlayer), delay);
  25. }
  26. public override void OnPlayerTriggerExit(VRCPlayerApi player)
  27. {
  28. if (cancelOnExit && player.isLocal) pending = false;
  29. }
  30. public void TeleportPlayer()
  31. {
  32. if (!pending) return; // Cancelled by leaving the trigger
  33. pending = false;
  34. Networking.LocalPlayer.TeleportTo(teleportTarget.position,
  35. teleportTarget.rotation,
  36. VRC_SceneDescriptor.SpawnOrientation.Default,
  37. false
  38. );
  39. }
  40. }