2025-01-07 02:40:37 +02:00

60 lines
1.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.XR.CoreUtils;
using UnityEngine;
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.");
}
}
}
void Update()
{
}
private void TeleportPlayer()
{
Debug.Log("Teleport button clicked");
if (target != null && Player != null && Player.Camera != null)
{
// Teleport the XR Origin to the specified coordinates.
Player.transform.position = target.transform.position;
Player.Camera.transform.rotation = target.transform.rotation;
}
}
}