MovePlayerPosition.cs 1.7 KB

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