OVRDebugHeadController.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.VR;
  4. public class OVRDebugHeadController : MonoBehaviour
  5. {
  6. private void Awake()
  7. {
  8. OVRCameraRig[] componentsInChildren = base.gameObject.GetComponentsInChildren<OVRCameraRig>();
  9. if (componentsInChildren.Length == 0)
  10. {
  11. Debug.LogWarning("OVRCamParent: No OVRCameraRig attached.");
  12. }
  13. else if (componentsInChildren.Length > 1)
  14. {
  15. Debug.LogWarning("OVRCamParent: More then 1 OVRCameraRig attached.");
  16. }
  17. else
  18. {
  19. this.CameraRig = componentsInChildren[0];
  20. }
  21. }
  22. private void Start()
  23. {
  24. }
  25. private void Update()
  26. {
  27. if (this.AllowMovement)
  28. {
  29. float y = OVRInput.Get(OVRInput.RawAxis2D.LThumbstick, OVRInput.Controller.Active).y;
  30. float x = OVRInput.Get(OVRInput.RawAxis2D.LThumbstick, OVRInput.Controller.Active).x;
  31. Vector3 a = this.CameraRig.centerEyeAnchor.rotation * Vector3.forward * y * Time.deltaTime * this.ForwardSpeed;
  32. Vector3 b = this.CameraRig.centerEyeAnchor.rotation * Vector3.right * x * Time.deltaTime * this.StrafeSpeed;
  33. base.transform.position += a + b;
  34. }
  35. if (!VRDevice.isPresent && (this.AllowYawLook || this.AllowPitchLook))
  36. {
  37. Quaternion quaternion = base.transform.rotation;
  38. if (this.AllowYawLook)
  39. {
  40. float x2 = OVRInput.Get(OVRInput.RawAxis2D.RThumbstick, OVRInput.Controller.Active).x;
  41. float angle = x2 * Time.deltaTime * this.GamePad_YawDegreesPerSec;
  42. Quaternion lhs = Quaternion.AngleAxis(angle, Vector3.up);
  43. quaternion = lhs * quaternion;
  44. }
  45. if (this.AllowPitchLook)
  46. {
  47. float num = OVRInput.Get(OVRInput.RawAxis2D.RThumbstick, OVRInput.Controller.Active).y;
  48. if (Mathf.Abs(num) > 0.0001f)
  49. {
  50. if (this.InvertPitch)
  51. {
  52. num *= -1f;
  53. }
  54. float angle2 = num * Time.deltaTime * this.GamePad_PitchDegreesPerSec;
  55. Quaternion rhs = Quaternion.AngleAxis(angle2, Vector3.left);
  56. quaternion *= rhs;
  57. }
  58. }
  59. base.transform.rotation = quaternion;
  60. }
  61. }
  62. [SerializeField]
  63. public bool AllowPitchLook;
  64. [SerializeField]
  65. public bool AllowYawLook = true;
  66. [SerializeField]
  67. public bool InvertPitch;
  68. [SerializeField]
  69. public float GamePad_PitchDegreesPerSec = 90f;
  70. [SerializeField]
  71. public float GamePad_YawDegreesPerSec = 90f;
  72. [SerializeField]
  73. public bool AllowMovement;
  74. [SerializeField]
  75. public float ForwardSpeed = 2f;
  76. [SerializeField]
  77. public float StrafeSpeed = 2f;
  78. protected OVRCameraRig CameraRig;
  79. }