RotateControl.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Receives rotations from separate RotateRing objects, which should be child objects,
  6. /// and applies them to the ZED via CameraAnchor. Also rotates the ring holder object accordingly.
  7. /// When this is held at an angle, the size of the angle affects how quickly the ZED moves.
  8. /// See parent class TransformControl to see how visual elements work.
  9. /// </summary>
  10. public class RotateControl : TransformControl
  11. {
  12. /// <summary>
  13. /// Full range that you can turn the control on any one axis.
  14. /// </summary>
  15. [Tooltip("Full range that you can turn the control on any one axis.")]
  16. public float maxUIRotateDegrees = 90f;
  17. private Quaternion startRot;
  18. protected override void Awake ()
  19. {
  20. base.Awake();
  21. if (!anchor) anchor = FindObjectOfType<CameraAnchor>();
  22. CameraAnchor.OnCameraAnchorCreated += SetNewAnchor;
  23. startRot = visualsParent.localRotation;
  24. }
  25. /// <summary>
  26. /// Takes how many degrees a given ring has been turned, and translates it to a call to
  27. /// CameraAnchor to move the ZED gradually.
  28. /// </summary>
  29. public void Rotate(Vector3 degrees)
  30. {
  31. Vector3 oldeulers = StandardizeEulers(visualsParent.localEulerAngles);
  32. //Reflect rotation in UI element (this thing).
  33. Vector3 clampdegrees;
  34. clampdegrees.x = Mathf.Clamp(degrees.x, -maxUIRotateDegrees, maxUIRotateDegrees);
  35. clampdegrees.y = Mathf.Clamp(degrees.y, -maxUIRotateDegrees, maxUIRotateDegrees);
  36. clampdegrees.z = Mathf.Clamp(degrees.z, -maxUIRotateDegrees, maxUIRotateDegrees);
  37. visualsParent.localRotation = startRot * Quaternion.Euler(clampdegrees);
  38. //Get clamped value and send to anchor to move the actual ZED.
  39. if (anchor)
  40. {
  41. Vector3 finaldegrees = clampdegrees / maxUIRotateDegrees;
  42. anchor.RotateZEDIncrementally(finaldegrees);
  43. }
  44. PlayTapSoundIfNeeded(oldeulers / maxUIRotateDegrees, StandardizeEulers(visualsParent.localEulerAngles) / maxUIRotateDegrees);
  45. }
  46. /// <summary>
  47. /// Changes the anchor object.
  48. /// </summary>
  49. private void SetNewAnchor(CameraAnchor newanchor)
  50. {
  51. anchor = newanchor;
  52. }
  53. /// <summary>
  54. /// Since Unity provides Euler angles inconsistently (ex. 270 = -90 = -450) this function adds or subtracts 360
  55. /// as needed to ensure the angle is between -180 and 180, allowing us to use that range for logic.
  56. /// </summary>
  57. private Vector3 StandardizeEulers(Vector3 input)
  58. {
  59. Vector3 standardized;
  60. standardized.x = StandardizeAngle(input.x);
  61. standardized.y = StandardizeAngle(input.y);
  62. standardized.z = StandardizeAngle(input.z);
  63. return standardized;
  64. }
  65. /// <summary>
  66. /// Makes a single angle (in degrees) be represented as between -180 and 180.
  67. /// </summary>
  68. private float StandardizeAngle(float input)
  69. {
  70. float standardized = input - Mathf.Floor(input / 360f); //Puts it to within -360 and 360.
  71. if (standardized > 180) standardized -= 360;
  72. else if (standardized < -180) standardized += 360;
  73. return standardized;
  74. }
  75. }