using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// Handles the visible, interactable ring controls in the ZED MR Calibration scene - the red, blue and green circles
/// that you can grab to rotate the ZED.
/// See parent class TransformGrabbable to see how visuals and enabling/disabling other objects works.
///
public class RotateRing : TransformGrabbable
{
///
/// RotateControl object that governs the actual ZED rotations. When RotateRing is moved, it sends movements to this object.
///
[Tooltip("RotateControl object that governs the actual ZED rotations. When RotateRing is moved, it sends movements to this object. ")]
public RotateControl rotControl;
///
/// Multiplies how the controller's movements move each axis. Set to 1 for axes this should control and 0 for the others.
///
[Tooltip("Multiplies how the controller's movements move each axis. Set to 1 for axes this should control and 0 for the others. ")]
public Vector3 axisFactor = Vector3.right;
private Vector3 grabStartDirection = Vector3.zero;
private Quaternion grabStartRotation = Quaternion.identity;
protected override void Awake()
{
base.Awake();
if(!rotControl)
{
rotControl = GetComponentInParent();
}
}
///
/// If being grabbed, calculates the angle offset of the controller relative to the start, and sends that angle to the RotateControl.
///
void Update()
{
if(isGrabbed)
{
Vector3 currentvec = rotControl.transform.InverseTransformDirection(grabbingTransform.position - rotControl.transform.position);
currentvec.Normalize();
float xangle = Vector3.SignedAngle(new Vector3(0, grabStartDirection.y, grabStartDirection.z), new Vector3(0, currentvec.y, currentvec.z), Vector3.right);
float yangle = Vector3.SignedAngle(new Vector3(grabStartDirection.x, 0, grabStartDirection.z), new Vector3(currentvec.x, 0, currentvec.z), Vector3.up);
float zangle = Vector3.SignedAngle(new Vector3(grabStartDirection.x, grabStartDirection.y, 0), new Vector3(currentvec.x, currentvec.y, 0), Vector3.forward);
Vector3 finalangle = new Vector3(xangle * axisFactor.x, yangle * axisFactor.y, zangle * axisFactor.z);
rotControl.Rotate(finalangle);
}
}
///
/// What happens when ZEDXRGrabber first grabs it. From IXRGrabbable. Stores the current positions for determining the change later.
///
public override void OnGrabStart(Transform grabtransform)
{
base.OnGrabStart(grabtransform);
grabStartRotation = rotControl.transform.localRotation;
grabStartDirection = rotControl.transform.InverseTransformDirection(grabbingTransform.position - rotControl.transform.position);
grabStartDirection.Normalize();
}
///
/// What happens when ZEDXRGrabber stops grabbing it. From IXRGrabbable.
///
public override void OnGrabEnd()
{
rotControl.Rotate(Vector3.zero);
base.OnGrabEnd();
}
}