BuggyController.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Valve.VR;
  6. using Valve.VR.InteractionSystem;
  7. namespace Valve.VR.InteractionSystem.Sample
  8. {
  9. public class BuggyController : MonoBehaviour
  10. {
  11. public Transform modelJoystick;
  12. public float joystickRot = 20;
  13. public Transform modelTrigger;
  14. public float triggerRot = 20;
  15. public BuggyBuddy buggy;
  16. public Transform buttonBrake;
  17. public Transform buttonReset;
  18. //ui stuff
  19. public Canvas ui_Canvas;
  20. public Image ui_rpm;
  21. public Image ui_speed;
  22. public RectTransform ui_steer;
  23. public float ui_steerangle;
  24. public Vector2 ui_fillAngles;
  25. public Transform resetToPoint;
  26. public SteamVR_Action_Vector2 actionSteering = SteamVR_Input.GetAction<SteamVR_Action_Vector2>("buggy", "Steering");
  27. public SteamVR_Action_Single actionThrottle = SteamVR_Input.GetAction<SteamVR_Action_Single>("buggy", "Throttle");
  28. public SteamVR_Action_Boolean actionBrake = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("buggy", "Brake");
  29. public SteamVR_Action_Boolean actionReset = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("buggy", "Reset");
  30. private float usteer;
  31. private Interactable interactable;
  32. private Quaternion trigSRot;
  33. private Quaternion joySRot;
  34. private Coroutine resettingRoutine;
  35. private Vector3 initialScale;
  36. private void Start()
  37. {
  38. joySRot = modelJoystick.localRotation;
  39. trigSRot = modelTrigger.localRotation;
  40. interactable = GetComponent<Interactable>();
  41. StartCoroutine(DoBuzz());
  42. buggy.controllerReference = transform;
  43. initialScale = buggy.transform.localScale;
  44. }
  45. private void Update()
  46. {
  47. Vector2 steer = Vector2.zero;
  48. float throttle = 0;
  49. float brake = 0;
  50. bool reset = false;
  51. bool b_brake = false;
  52. bool b_reset = false;
  53. if (interactable.attachedToHand)
  54. {
  55. SteamVR_Input_Sources hand = interactable.attachedToHand.handType;
  56. steer = actionSteering.GetAxis(hand);
  57. throttle = actionThrottle.GetAxis(hand);
  58. b_brake = actionBrake.GetState(hand);
  59. b_reset = actionReset.GetState(hand);
  60. brake = b_brake ? 1 : 0;
  61. reset = actionReset.GetStateDown(hand);
  62. }
  63. if (reset && resettingRoutine == null)
  64. {
  65. resettingRoutine = StartCoroutine(DoReset());
  66. }
  67. if (ui_Canvas != null)
  68. {
  69. ui_Canvas.gameObject.SetActive(interactable.attachedToHand);
  70. usteer = Mathf.Lerp(usteer, steer.x, Time.deltaTime * 9);
  71. ui_steer.localEulerAngles = Vector3.forward * usteer * -ui_steerangle;
  72. ui_rpm.fillAmount = Mathf.Lerp(ui_rpm.fillAmount, Mathf.Lerp(ui_fillAngles.x, ui_fillAngles.y, throttle), Time.deltaTime * 4);
  73. float speedLim = 40;
  74. ui_speed.fillAmount = Mathf.Lerp(ui_fillAngles.x, ui_fillAngles.y, 1 - (Mathf.Exp(-buggy.speed / speedLim)));
  75. }
  76. modelJoystick.localRotation = joySRot;
  77. /*if (input.AttachedHand != null && input.AttachedHand.IsLeft)
  78. {
  79. Joystick.Rotate(steer.y * -joystickRot, steer.x * -joystickRot, 0, Space.Self);
  80. }
  81. else if (input.AttachedHand != null && input.AttachedHand.IsRight)
  82. {
  83. Joystick.Rotate(steer.y * -joystickRot, steer.x * joystickRot, 0, Space.Self);
  84. }
  85. else*/
  86. //{
  87. modelJoystick.Rotate(steer.y * -joystickRot, steer.x * -joystickRot, 0, Space.Self);
  88. //}
  89. modelTrigger.localRotation = trigSRot;
  90. modelTrigger.Rotate(throttle * -triggerRot, 0, 0, Space.Self);
  91. buttonBrake.localScale = new Vector3(1, 1, b_brake ? 0.4f : 1.0f);
  92. buttonReset.localScale = new Vector3(1, 1, b_reset ? 0.4f : 1.0f);
  93. buggy.steer = steer;
  94. buggy.throttle = throttle;
  95. buggy.handBrake = brake;
  96. buggy.controllerReference = transform;
  97. }
  98. private IEnumerator DoReset()
  99. {
  100. float startTime = Time.time;
  101. float overTime = 1f;
  102. float endTime = startTime + overTime;
  103. buggy.transform.position = resetToPoint.transform.position;
  104. buggy.transform.rotation = resetToPoint.transform.rotation;
  105. buggy.transform.localScale = initialScale * 0.1f;
  106. while (Time.time < endTime)
  107. {
  108. buggy.transform.localScale = Vector3.Lerp(buggy.transform.localScale, initialScale, Time.deltaTime * 5f);
  109. yield return null;
  110. }
  111. buggy.transform.localScale = initialScale;
  112. resettingRoutine = null;
  113. }
  114. private float buzztimer;
  115. private IEnumerator DoBuzz()
  116. {
  117. while (true)
  118. {
  119. while (buzztimer < 1)
  120. {
  121. buzztimer += Time.deltaTime * buggy.mvol * 70;
  122. yield return null;
  123. }
  124. buzztimer = 0;
  125. if (interactable.attachedToHand)
  126. {
  127. interactable.attachedToHand.TriggerHapticPulse((ushort)Mathf.RoundToInt(300 * Mathf.Lerp(1.0f, 0.6f, buggy.mvol)));
  128. }
  129. }
  130. }
  131. }
  132. }