OVRDebugHeadController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /************************************************************************************
  2. Copyright : Copyright 2017 Oculus VR, LLC. All Rights reserved.
  3. Licensed under the Oculus VR Rift SDK License Version 3.4.1 (the "License");
  4. you may not use the Oculus VR Rift SDK except in compliance with the License,
  5. which is provided at the time of installation or download, or which
  6. otherwise accompanies this software in either electronic or hard copy form.
  7. You may obtain a copy of the License at
  8. https://developer.oculus.com/licenses/sdk-3.4.1
  9. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ************************************************************************************/
  15. using UnityEngine;
  16. using System.Collections;
  17. /// <summary>
  18. /// This is a simple behavior that can be attached to a parent of the CameraRig in order
  19. /// to provide movement via the gamepad. This is useful when testing an application in
  20. /// the Unity editor without the HMD.
  21. /// To use it, create a game object in your scene and drag your CameraRig to be a child
  22. /// of the game object. Then, add the OVRDebugHeadController behavior to the game object.
  23. /// Alternatively, this behavior can be placed directly on the OVRCameraRig object, but
  24. /// that is not guaranteed to work if OVRCameraRig functionality changes in the future.
  25. /// In the parent case, the object with OVRDebugHeadController can be thougt of as a
  26. /// platform that your camera is attached to. When the platform moves or rotates, the
  27. /// camera moves or rotates, but the camera can still move independently while "on" the
  28. /// platform.
  29. /// In general, this behavior should be disabled when not debugging.
  30. /// </summary>
  31. public class OVRDebugHeadController : MonoBehaviour
  32. {
  33. [SerializeField]
  34. public bool AllowPitchLook = false;
  35. [SerializeField]
  36. public bool AllowYawLook = true;
  37. [SerializeField]
  38. public bool InvertPitch = false;
  39. [SerializeField]
  40. public float GamePad_PitchDegreesPerSec = 90.0f;
  41. [SerializeField]
  42. public float GamePad_YawDegreesPerSec = 90.0f;
  43. [SerializeField]
  44. public bool AllowMovement = false;
  45. [SerializeField]
  46. public float ForwardSpeed = 2.0f;
  47. [SerializeField]
  48. public float StrafeSpeed = 2.0f;
  49. protected OVRCameraRig CameraRig = null;
  50. void Awake()
  51. {
  52. // locate the camera rig so we can use it to get the current camera transform each frame
  53. OVRCameraRig[] CameraRigs = gameObject.GetComponentsInChildren<OVRCameraRig>();
  54. if( CameraRigs.Length == 0 )
  55. Debug.LogWarning("OVRCamParent: No OVRCameraRig attached.");
  56. else if (CameraRigs.Length > 1)
  57. Debug.LogWarning("OVRCamParent: More then 1 OVRCameraRig attached.");
  58. else
  59. CameraRig = CameraRigs[0];
  60. }
  61. // Use this for initialization
  62. void Start ()
  63. {
  64. }
  65. // Update is called once per frame
  66. void Update ()
  67. {
  68. if ( AllowMovement )
  69. {
  70. float gamePad_FwdAxis = OVRInput.Get(OVRInput.RawAxis2D.LThumbstick).y;
  71. float gamePad_StrafeAxis = OVRInput.Get(OVRInput.RawAxis2D.LThumbstick).x;
  72. Vector3 fwdMove = ( CameraRig.centerEyeAnchor.rotation * Vector3.forward ) * gamePad_FwdAxis * Time.deltaTime * ForwardSpeed;
  73. Vector3 strafeMove = ( CameraRig.centerEyeAnchor.rotation * Vector3.right ) * gamePad_StrafeAxis * Time.deltaTime * StrafeSpeed;
  74. transform.position += fwdMove + strafeMove;
  75. }
  76. #if UNITY_2017_2_OR_NEWER
  77. if ( !UnityEngine.XR.XRDevice.isPresent && ( AllowYawLook || AllowPitchLook ) )
  78. #else
  79. if ( !UnityEngine.VR.VRDevice.isPresent && ( AllowYawLook || AllowPitchLook ) )
  80. #endif
  81. {
  82. Quaternion r = transform.rotation;
  83. if ( AllowYawLook )
  84. {
  85. float gamePadYaw = OVRInput.Get(OVRInput.RawAxis2D.RThumbstick).x;
  86. float yawAmount = gamePadYaw * Time.deltaTime * GamePad_YawDegreesPerSec;
  87. Quaternion yawRot = Quaternion.AngleAxis( yawAmount, Vector3.up );
  88. r = yawRot * r;
  89. }
  90. if ( AllowPitchLook )
  91. {
  92. float gamePadPitch = OVRInput.Get(OVRInput.RawAxis2D.RThumbstick).y;
  93. if ( Mathf.Abs( gamePadPitch ) > 0.0001f )
  94. {
  95. if ( InvertPitch )
  96. {
  97. gamePadPitch *= -1.0f;
  98. }
  99. float pitchAmount = gamePadPitch * Time.deltaTime * GamePad_PitchDegreesPerSec;
  100. Quaternion pitchRot = Quaternion.AngleAxis( pitchAmount, Vector3.left );
  101. r = r * pitchRot;
  102. }
  103. }
  104. transform.rotation = r;
  105. }
  106. }
  107. }