MovePlayerPosition.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using UnityEngine;
  2. using Valve.VR;
  3. namespace Calibration
  4. {
  5. public class MovePlayerPosition : MonoBehaviour
  6. {
  7. public SteamVR_Action_Boolean resetAction;
  8. public SteamVR_Action_Boolean modeAction;
  9. public SteamVR_Action_Boolean moveForwards;
  10. public SteamVR_Action_Boolean moveBackwards;
  11. public SteamVR_Action_Boolean moveRight;
  12. public SteamVR_Action_Boolean moveLeft;
  13. public float movementSpeed = 1f;
  14. private bool moveModeForwards;
  15. private Transform playerTransform;
  16. private void Start()
  17. {
  18. playerTransform = GetComponent<Transform>();
  19. resetAction.AddOnStateUpListener(OnResetActionUp, SteamVR_Input_Sources.Any);
  20. modeAction.AddOnStateUpListener(OnModeSwitchUp, SteamVR_Input_Sources.Any);
  21. }
  22. private void Update()
  23. {
  24. var left = moveLeft.GetState(SteamVR_Input_Sources.Any) ? 1 : 0;
  25. var right = moveRight.GetState(SteamVR_Input_Sources.Any) ? 1 : 0;
  26. var forward = moveForwards.GetState(SteamVR_Input_Sources.Any) ? 1 : 0;
  27. var backward = moveBackwards.GetState(SteamVR_Input_Sources.Any) ? 1 : 0;
  28. var x = right - left;
  29. var y = moveModeForwards ? 0f : forward - backward;
  30. var z = moveModeForwards ? forward - backward : 0f;
  31. playerTransform.localPosition += new Vector3(x, y, z) * (Time.deltaTime * movementSpeed);
  32. }
  33. private void OnResetActionUp(SteamVR_Action_Boolean fomAction, SteamVR_Input_Sources fromSource)
  34. {
  35. OpenVR.Chaperone.ResetZeroPose(ETrackingUniverseOrigin.TrackingUniverseStanding);
  36. }
  37. private void OnModeSwitchUp(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  38. {
  39. moveModeForwards = !moveModeForwards;
  40. }
  41. }
  42. }