72 lines
1.7 KiB
C#
Raw Normal View History

2023-11-14 11:51:12 -05:00
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;
public RotateTarget rotator;
[SerializeField] private MeshRenderer meshRenderer;
[SerializeField] private Material onMat, offMat;
public TextMeshProUGUI text;
public void OnHoverEntered(HoverEnterEventArgs args)
{
}
public void OnSelected(SelectEnterEventArgs args)
{
StartCoroutine(Hovering(args.interactorObject.transform));
}
private IEnumerator Hovering(Transform mover)
{
if (isHovering) yield break;
isHovering = true;
meshRenderer.material = onMat;
Vector3 previousPos = mover.position;
while (isHovering)
{
yield return null;
Vector3 currentPos = mover.position;
Vector3 diff = (previousPos - currentPos);
diff = new Vector3(diff.x * scrollAxis.x, diff.y * scrollAxis.y, diff.z * scrollAxis.z);
text.text = diff.ToString("0.00000");
previousPos = currentPos;
rotator.Rotate(diff);
}
meshRenderer.material = offMat;
}
public void OnHoverExited(HoverExitEventArgs args)
{
//isHovering = false;
}
public void OnUnSelected(SelectExitEventArgs args)
{
isHovering = false;
}
}
}