OVRDebugHeadController.cs 4.4 KB

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