RotateRing.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Handles the visible, interactable ring controls in the ZED MR Calibration scene - the red, blue and green circles
  6. /// that you can grab to rotate the ZED.
  7. /// See parent class TransformGrabbable to see how visuals and enabling/disabling other objects works.
  8. /// </summary>
  9. public class RotateRing : TransformGrabbable
  10. {
  11. /// <summary>
  12. /// RotateControl object that governs the actual ZED rotations. When RotateRing is moved, it sends movements to this object.
  13. /// </summary>
  14. [Tooltip("RotateControl object that governs the actual ZED rotations. When RotateRing is moved, it sends movements to this object. ")]
  15. public RotateControl rotControl;
  16. /// <summary>
  17. /// Multiplies how the controller's movements move each axis. Set to 1 for axes this should control and 0 for the others.
  18. /// </summary>
  19. [Tooltip("Multiplies how the controller's movements move each axis. Set to 1 for axes this should control and 0 for the others. ")]
  20. public Vector3 axisFactor = Vector3.right;
  21. private Vector3 grabStartDirection = Vector3.zero;
  22. private Quaternion grabStartRotation = Quaternion.identity;
  23. protected override void Awake()
  24. {
  25. base.Awake();
  26. if(!rotControl)
  27. {
  28. rotControl = GetComponentInParent<RotateControl>();
  29. }
  30. }
  31. /// <summary>
  32. /// If being grabbed, calculates the angle offset of the controller relative to the start, and sends that angle to the RotateControl.
  33. /// </summary>
  34. void Update()
  35. {
  36. if(isGrabbed)
  37. {
  38. Vector3 currentvec = rotControl.transform.InverseTransformDirection(grabbingTransform.position - rotControl.transform.position);
  39. currentvec.Normalize();
  40. float xangle = Vector3.SignedAngle(new Vector3(0, grabStartDirection.y, grabStartDirection.z), new Vector3(0, currentvec.y, currentvec.z), Vector3.right);
  41. float yangle = Vector3.SignedAngle(new Vector3(grabStartDirection.x, 0, grabStartDirection.z), new Vector3(currentvec.x, 0, currentvec.z), Vector3.up);
  42. float zangle = Vector3.SignedAngle(new Vector3(grabStartDirection.x, grabStartDirection.y, 0), new Vector3(currentvec.x, currentvec.y, 0), Vector3.forward);
  43. Vector3 finalangle = new Vector3(xangle * axisFactor.x, yangle * axisFactor.y, zangle * axisFactor.z);
  44. rotControl.Rotate(finalangle);
  45. }
  46. }
  47. /// <summary>
  48. /// What happens when ZEDXRGrabber first grabs it. From IXRGrabbable. Stores the current positions for determining the change later.
  49. /// </summary>
  50. public override void OnGrabStart(Transform grabtransform)
  51. {
  52. base.OnGrabStart(grabtransform);
  53. grabStartRotation = rotControl.transform.localRotation;
  54. grabStartDirection = rotControl.transform.InverseTransformDirection(grabbingTransform.position - rotControl.transform.position);
  55. grabStartDirection.Normalize();
  56. }
  57. /// <summary>
  58. /// What happens when ZEDXRGrabber stops grabbing it. From IXRGrabbable.
  59. /// </summary>
  60. public override void OnGrabEnd()
  61. {
  62. rotControl.Rotate(Vector3.zero);
  63. base.OnGrabEnd();
  64. }
  65. }