TransformControl.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Base class for controls that change the ZED's transform in the ZED MR Calibration scene. See TranslateControl and RotateControl.
  6. /// Mostly handles the audio sounds they both play since it's fairly complicated.
  7. /// </summary>
  8. public abstract class TransformControl : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// CameraAnchor object holding the ZED camera. Inheriting classes send transform updates to this.
  12. /// </summary>
  13. [Tooltip("CameraAnchor object holding the ZED camera. Inheriting classes send transform updates to this.")]
  14. public CameraAnchor anchor;
  15. /// <summary>
  16. /// Transform that holds all the visuals, like RotateRings and TransformArrows, and visual indicators of their movement.
  17. /// This should be a child of this script's transform, as it's moved/rotated with the visuals, but this object should
  18. /// not be moved separately from its parent as its used for calculating the controller's offset.
  19. /// </summary>
  20. [Tooltip("Transform that holds all the visuals, like RotateRings and TransformArrows, and visual indicators of their movement.\r\n" +
  21. "This should be a child of this script's transform, as it's moved/rotated with the visuals, but this object should " +
  22. "not be moved separately from its parent as its used for calculating the controller's offset.")]
  23. public Transform visualsParent;
  24. /// <summary>
  25. /// How many 'notches' (intervals) between a control being moved at 0 and 1 where should a tap sound play as it's crossed.
  26. /// </summary>
  27. [Tooltip("How many 'notches' (intervals) between a control being moved at 0 and 1 where should a tap sound play as it's crossed. ")]
  28. [Header("Sounds")]
  29. public int tapLevels = 5;
  30. /// <summary>
  31. /// Minimum time that must pass between playing tap sounds, so it doesn't sound caustic when you move the controller very quickly.
  32. /// </summary>
  33. [Tooltip("Minimum time that must pass between playing tap sounds, so it doesn't sound caustic when you move the controller very quickly. ")]
  34. public float secondsBetweenTaps = 0.05f;
  35. /// <summary>
  36. /// Pitch multiplier of the tap sounds when at 0. Taps in between 0 and 1 have the pitch lerped between the min and max.
  37. /// </summary>
  38. [Tooltip("Pitch multiplier of the tap sounds when at 0.\r\n" +
  39. "Taps in between 0 and 1 have the pitch lerped between the min and max. ")]
  40. public float minPitch = 1f;
  41. /// <summary>
  42. /// Pitch multiplier of the tap sounds when fully articulated to 1. Taps in between 0 and 1 have the pitch lerped between the min and max.
  43. /// </summary>
  44. [Tooltip("Pitch multiplier of the tap sounds when fully articulated to 1.\r\r" +
  45. "Taps in between 0 and 1 have the pitch lerped between the min and max. ")]
  46. public float maxPitch = 2f;
  47. private float tapTimer = 0f;
  48. private AudioSource audioSource;
  49. private float tapIncrement
  50. {
  51. get
  52. {
  53. return 1 / (float)tapLevels;
  54. }
  55. }
  56. protected virtual void Awake()
  57. {
  58. audioSource = GetComponent<AudioSource>();
  59. }
  60. protected virtual void Update()
  61. {
  62. if (tapTimer > 0f) tapTimer -= Time.deltaTime;
  63. }
  64. /// <summary>
  65. /// Determines if we need to play a tapping sound based on the latest change in articulation.
  66. /// Also makes sure there's an attached AudioSource and that enough time has passed since the last tap sound.
  67. /// </summary>
  68. /// <param name="oldvector">Amount of articulation of X, Y and Z (clamped at -1 to 1) before the latest change.</param>
  69. /// <param name="newvector">Amount of articulation of X, Y and Z (clamped at -1 to 1) as of the latest change.</param>
  70. protected void PlayTapSoundIfNeeded(Vector3 oldvector, Vector3 newvector)
  71. {
  72. if (!audioSource) return;
  73. if (tapTimer > 0f) return;
  74. float oldsumdirection = oldvector.x + oldvector.y + oldvector.z;
  75. float newsumdirection = newvector.x + newvector.y + newvector.z;
  76. int oldlevel = Mathf.RoundToInt(oldsumdirection / tapIncrement);
  77. int newlevel = Mathf.RoundToInt(newsumdirection / tapIncrement);
  78. if (newlevel != oldlevel) //We've passed a tapping point.
  79. {
  80. float newpitch = Mathf.Lerp(minPitch, maxPitch, (Mathf.Abs(newlevel) + 1) / (float)(tapLevels + 1));
  81. audioSource.pitch = newpitch;
  82. audioSource.Play();
  83. tapTimer = secondsBetweenTaps;
  84. }
  85. else if (newlevel == 0f)
  86. {
  87. if ((oldsumdirection >= 0 && newsumdirection < 0) || (oldsumdirection <= 0 && newsumdirection > 0))
  88. {
  89. audioSource.pitch = minPitch;
  90. audioSource.Play();
  91. tapTimer = secondsBetweenTaps;
  92. }
  93. }
  94. }
  95. }