TranslateArrow.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Handles the visible, interactable arrow controls in the ZED MR Calibration scene - the red, blue and green arrows
  6. /// that you can grab to translate the ZED.
  7. /// See parent class TransformGrabbable to see how visuals and enabling/disabling other objects works.
  8. /// </summary>
  9. public class TranslateArrow : TransformGrabbable
  10. {
  11. /// <summary>
  12. /// TranslateControl object that governs the actual ZED rotations. When TranslateArrow is moved, it sends movements to this object.
  13. /// </summary>
  14. public TranslateControl transControl;
  15. /// <summary>
  16. /// Multiplies how the controller's movements move each axis. Set to 1 for axes this should control and 0 for the others.
  17. /// </summary>
  18. public Vector3 axisFactor = Vector3.right;
  19. private Vector3 grabStartOffset = Vector3.zero;
  20. private Vector3 grabStartControlLocalPos = Vector3.zero;
  21. protected override void Awake()
  22. {
  23. base.Awake();
  24. if (!transControl)
  25. {
  26. transControl = GetComponentInParent<TranslateControl>();
  27. }
  28. }
  29. /// <summary>
  30. /// If being grabbed, sends its current positional offset from the center to the TranslateControl, which applies the translation.
  31. /// </summary>
  32. private void Update()
  33. {
  34. if (isGrabbed)
  35. {
  36. Vector3 currentlocal = grabbingTransform.position;
  37. Vector3 currentoffset = transControl.transform.InverseTransformPoint(grabbingTransform.position);
  38. currentoffset += (transControl.transform.localPosition - grabStartControlLocalPos);
  39. Vector3 dist = currentoffset - grabStartOffset;
  40. Vector3 moddist = new Vector3(dist.x * axisFactor.x, dist.y * axisFactor.y, dist.z * axisFactor.z);
  41. transControl.Translate(moddist);
  42. }
  43. }
  44. /// <summary>
  45. /// What happens when ZEDXRGrabber first grabs it. From IXRGrabbable. Stores the current positions for determining the change later.
  46. /// </summary>
  47. public override void OnGrabStart(Transform grabtransform)
  48. {
  49. base.OnGrabStart(grabtransform);
  50. grabStartControlLocalPos = transControl.transform.localPosition;
  51. grabStartOffset = transControl.transform.InverseTransformPoint(grabbingTransform.position);
  52. }
  53. /// <summary>
  54. /// What happens when ZEDXRGrabber stops grabbing it. From IXRGrabbable.
  55. /// </summary>
  56. public override void OnGrabEnd()
  57. {
  58. transControl.Translate(Vector3.zero);
  59. base.OnGrabEnd();
  60. }
  61. }