using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using UnityEngine.XR.Interaction.Toolkit.Interactors;
using UnityEngine.XR.Interaction.Toolkit.Interactors.Visuals;
using UnityEngine.XR.Interaction.Toolkit.Locomotion.Teleportation;
namespace jeanf.universalplayer
{
///
/// XRI-style stick teleportation (ships on the Player root, zero wiring):
/// pushing the right thumbstick forward summons the curved teleport ray (the
/// packaged Teleport Interactor with its valid/blocked gradients and directional
/// reticle), the curve reaches further the harder the stick is pushed, rotating
/// the stick aims the landing orientation, and releasing the stick teleports —
/// onto TeleportationArea/TeleportationAnchor surfaces only. The left stick keeps
/// driving continuous movement.
///
public class StickTeleport : MonoBehaviour
{
private const string LogPrefix = "[UniversalPlayer]";
[Tooltip("Stick deflection (forward) that summons the teleport ray.")]
[Range(0.3f, 0.95f)][SerializeField] private float activateThreshold = 0.65f;
[Tooltip("Deflection below which the stick counts as released (teleports if aiming at a valid target).")]
[Range(0.05f, 0.6f)][SerializeField] private float releaseThreshold = 0.3f;
[Tooltip("Curve velocity at the lightest push — how close the shortest teleport lands.")]
[SerializeField] private float minCurveVelocity = 6f;
[Tooltip("Curve velocity at full deflection — how far the longest teleport reaches.")]
[SerializeField] private float maxCurveVelocity = 13f;
[Tooltip("Legacy fallback for rigs whose teleport interactor has NO Ray Origin Transform: re-aims the interactor itself forward out of the controller while summoned. The packaged Player wires each Teleport Interactor to a 'Left/Right Teleport Ray Origin' child of the controller instead — edit THAT transform's position/rotation to place and aim the ray; this override is then ignored.")]
[SerializeField] private bool overrideAim = true;
[Tooltip("Legacy fallback only (see Override Aim): Euler offset applied on top of the controller's orientation. X tilts the launch DOWN. With an authored Teleport Ray Origin this is unused — rotate the origin transform instead.")]
[SerializeField] private Vector3 aimEulerOffset = new Vector3(35f, 0f, 0f);
/// Test seam: stick value per hand. Null = read the real XR device.
public Func StickProbe;
private sealed class TeleportHand
{
public XRNode Node;
public XRRayInteractor Ray;
public XRInteractorLineVisual LineVisual;
public Transform RayOrigin; // what the interactor casts from (stabilizer-driven)
public bool Aiming;
public float TargetYaw;
}
private readonly List hands = new List();
private Camera playerCamera;
private CharacterController characterController;
private PlayerMovement playerMovement;
private float nextScan;
private void OnEnable()
{
playerCamera = Camera.main;
characterController = GetComponentInChildren(true);
playerMovement = GetComponentInChildren(true);
}
private void OnDisable()
{
foreach (var hand in hands) StopAiming(hand);
hands.Clear();
}
private void Update()
{
if (BroadcastControlsStatus.controlScheme != BroadcastControlsStatus.ControlScheme.XR)
{
foreach (var hand in hands) StopAiming(hand);
return;
}
if (hands.Count == 0 && Time.unscaledTime >= nextScan) RefreshInteractors();
foreach (var hand in hands) DriveHand(hand);
}
// Aim ownership: the packaged Player gives each Teleport Interactor a Ray Origin
// Transform ("Left/Right Teleport Ray Origin", a child of the controller). Its LOCAL
// pose is the authored position/aim of the ray, so nothing here may touch it.
// Legacy rigs without an origin fall back to re-aiming the interactor itself — from
// the CONTROLLER's rotation, never from the interactor's own (that compounded the
// offset every frame and sent the arc spinning).
private void LateUpdate()
{
if (!overrideAim) return;
if (BroadcastControlsStatus.controlScheme != BroadcastControlsStatus.ControlScheme.XR) return;
foreach (var hand in hands)
{
if (!hand.Aiming || hand.Ray == null) continue;
if (hand.RayOrigin == null)
hand.RayOrigin = hand.Ray.rayOriginTransform != null ? hand.Ray.rayOriginTransform : hand.Ray.transform;
if (hand.RayOrigin != hand.Ray.transform) continue; // authored origin: its local pose IS the aim
var basis = hand.Ray.transform.parent != null ? hand.Ray.transform.parent.rotation : hand.Ray.transform.rotation;
hand.RayOrigin.rotation = basis * Quaternion.Euler(aimEulerOffset);
}
}
/// Finds the packaged teleport rays (projectile-curve interactors); also a test seam.
public void RefreshInteractors()
{
nextScan = Time.unscaledTime + 1f;
hands.Clear();
foreach (var ray in GetComponentsInChildren(true))
{
if (ray.lineType != XRRayInteractor.LineType.ProjectileCurve) continue;
var node = NodeFor(ray.transform);
hands.Add(new TeleportHand
{
Node = node,
Ray = ray,
LineVisual = ray.GetComponent(),
RayOrigin = ray.rayOriginTransform != null ? ray.rayOriginTransform : ray.transform,
});
// The ray only shows while aiming.
ray.gameObject.SetActive(false);
}
// Teleport lives on the RIGHT stick: the left stick drives continuous move.
hands.RemoveAll(h => h.Node != XRNode.RightHand);
}
private static XRNode NodeFor(Transform t)
{
for (var current = t; current != null; current = current.parent)
{
var n = current.name;
if (n.Contains("Left")) return XRNode.LeftHand;
if (n.Contains("Right")) return XRNode.RightHand;
}
return XRNode.RightHand;
}
private void DriveHand(TeleportHand hand)
{
if (hand.Ray == null) return;
var stick = ReadStick(hand.Node);
if (stick == null)
{
StopAiming(hand);
return;
}
var value = stick.Value;
var deflection = Mathf.Clamp01(value.magnitude);
if (!hand.Aiming)
{
// Forward push summons the ray (sideways stays free for snap turn).
if (value.y >= activateThreshold && value.y >= Mathf.Abs(value.x))
{
hand.Aiming = true;
hand.Ray.gameObject.SetActive(true);
}
return;
}
if (deflection > releaseThreshold)
{
// Curve length follows the push; stick rotation aims the landing yaw.
hand.Ray.velocity = Mathf.Lerp(minCurveVelocity, maxCurveVelocity,
Mathf.InverseLerp(releaseThreshold, 1f, deflection));
var headYaw = playerCamera != null ? playerCamera.transform.eulerAngles.y : 0f;
hand.TargetYaw = headYaw + Mathf.Atan2(value.x, value.y) * Mathf.Rad2Deg;
RotateReticle(hand);
return;
}
// Released: teleport when the curve rests on a teleport surface.
TryTeleport(hand);
StopAiming(hand);
}
private void RotateReticle(TeleportHand hand)
{
var reticle = hand.LineVisual != null ? hand.LineVisual.reticle : null;
if (reticle != null && reticle.activeInHierarchy)
reticle.transform.rotation = Quaternion.Euler(0f, hand.TargetYaw, 0f);
}
private void TryTeleport(TeleportHand hand)
{
if (!hand.Ray.TryGetCurrent3DRaycastHit(out var hit)) return;
var interactable = hit.collider.GetComponentInParent();
if (interactable == null) return;
var destination = hit.point;
var yaw = hand.TargetYaw;
if (interactable is TeleportationAnchor anchor && anchor.teleportAnchorTransform != null)
{
// Anchors are authored landings: their transform wins.
destination = anchor.teleportAnchorTransform.position;
yaw = anchor.teleportAnchorTransform.eulerAngles.y;
}
TeleportRootTo(destination, yaw);
}
/// Moves the player root so the HEAD lands on the destination facing the given yaw.
public void TeleportRootTo(Vector3 destination, float yaw)
{
if (playerCamera == null) playerCamera = Camera.main;
var head = playerCamera != null ? playerCamera.transform : transform;
var ccWasEnabled = characterController != null && characterController.enabled;
if (ccWasEnabled) characterController.enabled = false;
// Rotate the rig around the head so the view faces the aimed yaw...
var deltaYaw = Mathf.DeltaAngle(head.eulerAngles.y, yaw);
transform.RotateAround(new Vector3(head.position.x, transform.position.y, head.position.z), Vector3.up, deltaYaw);
// ...then land the head (not the rig origin) on the destination.
var headOffset = head.position - transform.position;
transform.position = new Vector3(destination.x - headOffset.x, destination.y, destination.z - headOffset.z);
if (ccWasEnabled) characterController.enabled = true;
if (playerMovement != null) playerMovement.OnExternalTeleport();
}
private void StopAiming(TeleportHand hand)
{
if (!hand.Aiming) return;
hand.Aiming = false;
if (hand.Ray != null) hand.Ray.gameObject.SetActive(false);
}
private Vector2? ReadStick(XRNode node)
{
if (StickProbe != null) return StickProbe(node);
var device = InputDevices.GetDeviceAtXRNode(node);
if (!device.isValid) return null;
device.TryGetFeatureValue(CommonUsages.primary2DAxis, out var value);
return value;
}
}
}