2
0

MovePlayerPosition.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 Transform playerTransform;
  15. private bool moveModeForwards = false;
  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; var z = moveModeForwards ? forward - backward : 0f;
  30. playerTransform.localPosition += new Vector3(x, y, z) * (Time.deltaTime * movementSpeed);
  31. }
  32. private void OnResetActionUp(SteamVR_Action_Boolean fomAction, SteamVR_Input_Sources fromSource)
  33. {
  34. OpenVR.Chaperone.ResetZeroPose(ETrackingUniverseOrigin.TrackingUniverseStanding);
  35. }
  36. private void OnModeSwitchUp(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
  37. {
  38. moveModeForwards = !moveModeForwards;
  39. }
  40. }
  41. }