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