using System; using System.Collections; using System.Collections.Generic; using Unity.XR.CoreUtils; using UnityEngine; using UnityEngine.Rendering; using UnityEngine.UI; using UnityEngine.XR.Interaction.Toolkit; public class MenuTeleportButton : MonoBehaviour { public Sprite NormalSprite; public Sprite HoverSprite; public String TargetName; public XRBaseControllerInteractor LeftXRInteractor; // Reference to XR controller interactor (e.g., Ray Interactor) public XRBaseControllerInteractor RightXRInteractor; public XROrigin Player; private Button button; private TeleportLocation target; // Target teleport position void Start() { button = GetComponent<Button>(); // Subscribe to button events button.onClick.AddListener(TeleportPlayer); TeleportLocation[] locations = FindObjectsOfType<TeleportLocation>(); // Fetches all teleport locations from scene. //Debug.Log("The amount of teleport locations is " + locations.Length); foreach (TeleportLocation location in locations) // Finds the target. { if (location.Name.Equals(TargetName)) { target = location; //Debug.Log("Teleport target of " + target.Name + " found."); } } if (target == null) Debug.Log("Teleport target of " + TargetName + " not found."); } public void SetStateSelected() { if (button != null) { if (HoverSprite != null) { button.targetGraphic.GetComponent<Image>().sprite = HoverSprite; } } } public void SetStateDefault() { // Refresh the button state. button.interactable = false; button.interactable = true; if (NormalSprite != null) { button.targetGraphic.GetComponent<Image>().sprite = NormalSprite; } } private void TeleportPlayer() { //Debug.Log("Teleport button clicked"); if (target != null && Player != null && Player.Camera != null) { Player.transform.position = target.transform.position; // Calculate the rotation offset needed for the player Vector3 targetEulerAngles = target.transform.rotation.eulerAngles; Vector3 currentCameraEulerAngles = Player.Camera.transform.rotation.eulerAngles; // Determine the rotation delta around the Y-axis float rotationDeltaY = targetEulerAngles.y - currentCameraEulerAngles.y; // Apply the rotation delta to the XR Origin Player.transform.Rotate(0, rotationDeltaY, 0, Space.World); // Refresh the button state. button.interactable = false; button.interactable = true; } } }