MatchPlayerPositionAndCalibrate.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System.Collections;
  2. using Display;
  3. using UnityEngine;
  4. using Valve.VR;
  5. namespace Tracking
  6. {
  7. public enum CalibrationMode
  8. {
  9. None,
  10. MatchOnly,
  11. AutoImmediate,
  12. AutoCountdown
  13. }
  14. public class MatchPlayerPositionAndCalibrate : MonoBehaviour
  15. {
  16. public Transform player;
  17. public CalibrationMode calibrationMode;
  18. public CountdownDisplay countdownHud;
  19. [Header("Front Wheel")] public Transform frontWheelDesiredPosition;
  20. public FrontWheelTracker frontWheelTracker;
  21. [Header("Trainer Leg")] public Transform legDesiredPosition;
  22. public KineticLegTracker legTracker;
  23. [Header("HMD")] public bool calibrateHmd = true;
  24. public LookStraightDisplay lookStraightHud;
  25. public CameraTracker hmdTracker;
  26. public Transform bikeTransform;
  27. private bool autoSet;
  28. private SteamVR_TrackedObject fwTrackedObject;
  29. private Transform fwtTransform;
  30. private SteamVR_TrackedObject legTrackedObject;
  31. private Transform legTransform;
  32. private int matching;
  33. private Transform playerTransform;
  34. private void Start()
  35. {
  36. fwtTransform = frontWheelTracker.transform;
  37. legTransform = legTracker.transform;
  38. playerTransform = player.transform;
  39. fwTrackedObject = legTracker.GetComponent<SteamVR_TrackedObject>();
  40. legTrackedObject = frontWheelTracker.GetComponent<SteamVR_TrackedObject>();
  41. }
  42. private void Update()
  43. {
  44. AutoCalibration();
  45. DoMatching();
  46. }
  47. public void Match()
  48. {
  49. matching = 1;
  50. }
  51. private void MatchFrontWheel()
  52. {
  53. var dif = frontWheelDesiredPosition.position - fwtTransform.position;
  54. playerTransform.position += dif;
  55. StartCoroutine(HideFrontWheelTracker());
  56. }
  57. private void MatchLeg()
  58. {
  59. //The only way to match it now is by rotating
  60. var fwtPosition = fwtTransform.position;
  61. var fwtToDesiredLeg = legDesiredPosition.position - fwtPosition;
  62. var fwtToRealLeg = legTransform.position - fwtPosition;
  63. var dl2d = new Vector2(fwtToDesiredLeg.x, fwtToDesiredLeg.z);
  64. var dr2d = new Vector2(fwtToRealLeg.x, fwtToRealLeg.z);
  65. var angle = Vector2.Angle(dl2d, dr2d);
  66. playerTransform.RotateAround(fwtPosition, playerTransform.up, -angle);
  67. StartCoroutine(HideLegTracker());
  68. }
  69. private IEnumerator HideFrontWheelTracker()
  70. {
  71. yield return new WaitForSeconds(1);
  72. frontWheelDesiredPosition.gameObject.SetActive(false);
  73. frontWheelTracker.GetComponent<MeshRenderer>().enabled = false;
  74. }
  75. private IEnumerator HideLegTracker()
  76. {
  77. yield return new WaitForSeconds(1);
  78. legDesiredPosition.gameObject.SetActive(false);
  79. legTracker.GetComponent<MeshRenderer>().enabled = false;
  80. }
  81. private void AutoCalibration()
  82. {
  83. if (calibrationMode == CalibrationMode.None || autoSet || !fwTrackedObject.isValid ||
  84. !legTrackedObject.isValid) return;
  85. if (calibrationMode == CalibrationMode.AutoCountdown)
  86. {
  87. var display = Instantiate(countdownHud);
  88. display.OnCountdownDone = () =>
  89. {
  90. Match();
  91. return "Calibrated!";
  92. };
  93. autoSet = true;
  94. }
  95. else
  96. {
  97. Match();
  98. autoSet = true;
  99. }
  100. }
  101. private void DoMatching()
  102. {
  103. if (matching == 1)
  104. {
  105. MatchFrontWheel();
  106. matching++;
  107. }
  108. else if (matching == 2)
  109. {
  110. MatchLeg();
  111. if (calibrationMode == CalibrationMode.MatchOnly || calibrationMode == CalibrationMode.None)
  112. matching = 0;
  113. else
  114. matching++;
  115. }
  116. else if (matching == 3)
  117. {
  118. legTracker.Calibrate();
  119. frontWheelTracker.Calibrate();
  120. matching = 0;
  121. DoLookStraightIfWanted();
  122. }
  123. }
  124. private void DoLookStraightIfWanted()
  125. {
  126. if (!calibrateHmd) return;
  127. var lookStraightDisplay = Instantiate(lookStraightHud);
  128. lookStraightDisplay.OnDone += OnLookStraightDone;
  129. var lTransform = lookStraightDisplay.transform;
  130. var newPos = bikeTransform.position + bikeTransform.forward * 1.4f;
  131. newPos.y = hmdTracker.transform.position.y;
  132. lTransform.position = newPos;
  133. }
  134. private void OnLookStraightDone()
  135. {
  136. hmdTracker.Calibrate();
  137. }
  138. }
  139. }