Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
prestonmediaspi's avatar
prestonmediaspi
Honored Guest
11 years ago

Mouse rotation (decoupled from OVRPlayerController) script

For those of you making anything other than an FPS, I thought this might be a helpful script.

I decoupled the mouse rotation out of the OVRPlayerController script (removing the CharacterController and movement aspects).

/************************************************************************************

Copyright : Copyright 2014 Oculus VR, LLC. All Rights reserved.

Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

http://www.oculusvr.com/licenses/LICENSE-3.2

Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

THIS RIFT SDK AND ANY COMPONENT THEREOF IS PROVIDED BY OCULUS VR AND
ITS CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OCULUS VR AS THE
COPYRIGHT OWNER OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS RIFT
SDK OR THE RIFT SDK DERIVATIVES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Modified by Preston Cowley

************************************************************************************/

using UnityEngine;
using System.Collections.Generic;

/// <summary>
/// Mouse input --> camera rig rotation
/// </summary>
public class OculusInputController : MonoBehaviour
{
/// <summary>
/// The rate of rotation when using a gamepad.
/// </summary>
public float RotationAmount = 1.5f;

/// <summary>
/// The rate of rotation when using the keyboard.
/// </summary>
public float RotationRatchet = 45.0f;

/// <summary>
/// The player's current rotation about the Y axis.
/// </summary>
private float YRotation = 0.0f;

/// <summary>
/// If true, tracking data from a child OVRCameraRig will update the direction of movement.
/// </summary>
public bool HmdRotatesY = true;

protected OVRCameraRig CameraController = null;
protected Transform DirXform = null;

private float RotationScaleMultiplier = 1.0f;
private bool SkipMouseRotation = false;
private bool HaltUpdateMovement = false;
private bool prevHatLeft = false;
private bool prevHatRight = false;
private float SimulationRate = 60f;

void Awake()
{
// We use OVRCameraRig to set rotations to cameras,
// and to be influenced by rotation
OVRCameraRig[] CameraControllers;
CameraControllers = gameObject.GetComponentsInChildren<OVRCameraRig>();

if(CameraControllers.Length == 0)
Debug.LogError("OVRPlayerController: No OVRCameraRig attached.");
else if (CameraControllers.Length > 1)
Debug.LogError("OVRPlayerController: More then 1 OVRCameraRig attached.");
else
CameraController = CameraControllers[0];

DirXform = transform.Find("ForwardDirection");

if(DirXform == null)
Debug.LogError("OVRPlayerController: ForwardDirection game object not found. Do not use. (this gameobject needs to have a child named 'ForwardDirection' with the identity transform");

#if UNITY_ANDROID && !UNITY_EDITOR
OVRManager.display.RecenteredPose += ResetOrientation;
#endif
}

protected virtual void Update()
{
UpdateMovement();
}

public virtual void UpdateMovement()
{
if (HaltUpdateMovement)
return;

bool curHatLeft = OVRGamepadController.GPC_GetButton(OVRGamepadController.Button.LeftShoulder);

if (curHatLeft && !prevHatLeft)
YRotation -= RotationRatchet;

prevHatLeft = curHatLeft;

bool curHatRight = OVRGamepadController.GPC_GetButton(OVRGamepadController.Button.RightShoulder);

if(curHatRight && !prevHatRight)
YRotation += RotationRatchet;

prevHatRight = curHatRight;

//Use keys to ratchet rotation
if (Input.GetKeyDown(KeyCode.Q))
YRotation -= RotationRatchet;

if (Input.GetKeyDown(KeyCode.E))
YRotation += RotationRatchet;

float rotateInfluence = SimulationRate * Time.deltaTime * RotationAmount * RotationScaleMultiplier;

if (!SkipMouseRotation)
YRotation += Input.GetAxis("Mouse X") * rotateInfluence * 3.25f;

float rightAxisX = OVRGamepadController.GPC_GetAxis(OVRGamepadController.Axis.RightXAxis);

YRotation += rightAxisX * rotateInfluence;

DirXform.rotation = Quaternion.Euler(0.0f, YRotation, 0.0f);
transform.rotation = DirXform.rotation;

if (HmdRotatesY)
{
float hmdY = CameraController.centerEyeAnchor.localRotation.eulerAngles.y;
DirXform.rotation *= Quaternion.Euler(0.0f, hmdY, 0.0f);
}
}

/// <summary>
/// Gets the rotation scale multiplier.
/// </summary>
/// <param name="rotationScaleMultiplier">Rotation scale multiplier.</param>
public void GetRotationScaleMultiplier(ref float rotationScaleMultiplier)
{
rotationScaleMultiplier = RotationScaleMultiplier;
}

/// <summary>
/// Sets the rotation scale multiplier.
/// </summary>
/// <param name="rotationScaleMultiplier">Rotation scale multiplier.</param>
public void SetRotationScaleMultiplier(float rotationScaleMultiplier)
{
RotationScaleMultiplier = rotationScaleMultiplier;
}

/// <summary>
/// Gets the allow mouse rotation.
/// </summary>
/// <param name="skipMouseRotation">Allow mouse rotation.</param>
public void GetSkipMouseRotation(ref bool skipMouseRotation)
{
skipMouseRotation = SkipMouseRotation;
}

/// <summary>
/// Sets the allow mouse rotation.
/// </summary>
/// <param name="skipMouseRotation">If set to <c>true</c> allow mouse rotation.</param>
public void SetSkipMouseRotation(bool skipMouseRotation)
{
SkipMouseRotation = skipMouseRotation;
}

/// <summary>
/// Gets the halt update movement.
/// </summary>
/// <param name="haltUpdateMovement">Halt update movement.</param>
public void GetHaltUpdateMovement(ref bool haltUpdateMovement)
{
haltUpdateMovement = HaltUpdateMovement;
}

/// <summary>
/// Sets the halt update movement.
/// </summary>
/// <param name="haltUpdateMovement">If set to <c>true</c> halt update movement.</param>
public void SetHaltUpdateMovement(bool haltUpdateMovement)
{
HaltUpdateMovement = haltUpdateMovement;
}

/// <summary>
/// Resets the player look rotation when the device orientation is reset.
/// </summary>
public void ResetOrientation()
{
YRotation = 0.0f;
}
}
Replies have been turned off for this discussion