using System.Collections; using TMPro; using UnityEngine; using UnityEngine.Events; using UnityEngine.XR.Interaction.Toolkit; namespace Interactions { public class XRScroll : MonoBehaviour { private bool isHovering; [SerializeField] private Vector3 scrollAxis = Vector3.up; [SerializeField] private float scrollScale = 1000; public UnityEvent scrollUpdated; public void OnSelected(SelectEnterEventArgs args) { StartCoroutine(Hovering(args.interactorObject.transform)); } private IEnumerator Hovering(Transform mover) { if (isHovering) yield break; isHovering = true; Vector3 previousPos = mover.position; while (isHovering) { yield return null; Vector3 currentPos = mover.position; //Calculate the difference in position only along one axis Vector3 diff = (previousPos - currentPos); diff = new Vector3(diff.x * scrollAxis.x, diff.y * scrollAxis.y, diff.z * scrollAxis.z); float diffTotal = (diff.x + diff.y + diff.z) * scrollScale; previousPos = currentPos; scrollUpdated?.Invoke(diffTotal); } } public void OnUnSelected(SelectExitEventArgs args) { isHovering = false; } } }