RemoteController.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. public class RemoteController : MonoBehaviour
  8. {
  9. public Transform modelJoystick;
  10. public float joystickRot = 20;
  11. public Transform modelTrigger;
  12. public float triggerRot = 20;
  13. public Transform MenuButton;
  14. //ui stuff
  15. public Canvas ui_Canvas;
  16. public Image ui_rpm;
  17. public Image ui_speed;
  18. public RectTransform ui_steer;
  19. public float ui_steerangle;
  20. public Vector2 ui_fillAngles;
  21. SteamVR_Action_Vector2 touchpad = SteamVR_Input.GetVector2Action("read_touchpad");
  22. SteamVR_Action_Single squezz = SteamVR_Input.GetSingleAction("Squeeze");
  23. SteamVR_Action_Boolean modeMenu = SteamVR_Input.GetBooleanAction("modemenu");
  24. public float angle;
  25. public float velocity;
  26. private float usteer;
  27. private Interactable interactable;
  28. private Quaternion trigSRot;
  29. private Quaternion joySRot;
  30. private Coroutine resettingRoutine;
  31. private Vector3 initialScale;
  32. private Remote remote;
  33. private void Start()
  34. {
  35. joySRot = modelJoystick.localRotation;
  36. trigSRot = modelTrigger.localRotation;
  37. remote = GameObject.Find("Modes/Remote").GetComponent<Remote>();
  38. remote.remoteController = this;
  39. interactable = GetComponent<Interactable>();
  40. }
  41. private void Update()
  42. {
  43. Vector2 steer = Vector2.zero;
  44. bool b_modemenu = false;
  45. if (interactable.attachedToHand)
  46. {
  47. SteamVR_Input_Sources hand = interactable.attachedToHand.handType;
  48. // caculate the angle and the velocity
  49. velocity = squezz.GetAxis(hand);
  50. angle = touchpad.GetAxis(hand).x;
  51. steer = touchpad.GetAxis(hand);
  52. b_modemenu = modeMenu.GetStateDown(hand);
  53. //interactable.attachedToHand.TriggerHapticPulse(0.1f, velocity*50f, velocity);
  54. }
  55. else{
  56. velocity = 0;
  57. angle = 0;
  58. }
  59. if (ui_Canvas != null)
  60. {
  61. ui_Canvas.gameObject.SetActive(interactable.attachedToHand);
  62. usteer = Mathf.Lerp(usteer, steer.x, Time.deltaTime * 9);
  63. ui_steer.localEulerAngles = Vector3.forward * usteer * -ui_steerangle;
  64. ui_rpm.fillAmount = Mathf.Lerp(ui_rpm.fillAmount, Mathf.Lerp(ui_fillAngles.x, ui_fillAngles.y, velocity), Time.deltaTime * 4);
  65. float speedLim = 1;
  66. ui_speed.fillAmount = Mathf.Lerp(ui_fillAngles.x, ui_fillAngles.y, 1 - (Mathf.Exp(- velocity/ speedLim)));
  67. }
  68. modelJoystick.localRotation = joySRot;
  69. modelJoystick.Rotate(steer.y * -joystickRot, steer.x * -joystickRot, 0, Space.Self);
  70. modelTrigger.localRotation = trigSRot;
  71. modelTrigger.Rotate(velocity * -triggerRot, 0, 0, Space.Self);
  72. MenuButton.localScale = new Vector3(1, 1, b_modemenu ? 0.4f : 1.0f);
  73. }
  74. private void OnDestroy() {
  75. remote.remoteController = null;
  76. }
  77. }