Advertisement
Guest User

VRMouseLook.cs

a guest
Feb 27th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.VR;
  3.  
  4. public class VRMouseLook : MonoBehaviour {
  5.    
  6. #if UNITY_EDITOR
  7.  
  8.     public bool enableYaw = true;
  9.     public bool autoRecenterPitch = true;
  10.     public bool autoRecenterRoll = true;
  11.     public KeyCode HorizontalAndVerticalKey = KeyCode.LeftAlt;
  12.     public KeyCode RollKey = KeyCode.LeftControl;
  13.  
  14.     Transform vrCameraTransform;
  15.     Transform rotationTransform;
  16.     Transform forwardTransform;
  17.  
  18.     private float mouseX = 0;
  19.     private float mouseY = 0;
  20.     private float mouseZ = 0;
  21.  
  22.     void Awake() {
  23.         // get the vr camera so we can align our forward with it
  24.         Camera vrCamera = gameObject.GetComponentInChildren<Camera>();
  25.         vrCameraTransform = vrCamera.transform;
  26.  
  27.         // create a hierarchy to enable us to additionally rotate the vr camera
  28.         rotationTransform = new GameObject("VR Mouse Look (Rotation)").GetComponent<Transform>();
  29.         forwardTransform = new GameObject("VR Mouse Look (Forward)").GetComponent<Transform>();
  30.         rotationTransform.SetParent(transform.parent, false);
  31.         forwardTransform.SetParent(rotationTransform, false);
  32.         transform.SetParent(forwardTransform, false);
  33.     }
  34.  
  35.     void Update () {
  36.         bool rolled = false;
  37.         bool pitched = false;
  38.         if (Input.GetKey(HorizontalAndVerticalKey)) {
  39.             pitched = true;
  40.             if (enableYaw) {
  41.                 mouseX += Input.GetAxis("Mouse X") * 5;
  42.                 if (mouseX <= -180) {
  43.                     mouseX += 360;
  44.                 } else if (mouseX > 180) {
  45.                     mouseX -= 360;
  46.                 }
  47.             }
  48.             mouseY -= Input.GetAxis("Mouse Y") * 2.4f;
  49.             mouseY = Mathf.Clamp(mouseY, -85, 85);
  50.         } else if (Input.GetKey(RollKey)) {
  51.             rolled = true;
  52.             mouseZ += Input.GetAxis("Mouse X") * 5;
  53.             mouseZ = Mathf.Clamp(mouseZ, -85, 85);
  54.         }
  55.         if (!rolled && autoRecenterRoll) {
  56.             // People don't usually leave their heads tilted to one side for long.
  57.             mouseZ = Mathf.Lerp(mouseZ, 0, Time.deltaTime / (Time.deltaTime + 0.1f));
  58.         }
  59.         if (!pitched && autoRecenterPitch) {
  60.             // People don't usually leave their heads tilted to one side for long.
  61.             mouseY = Mathf.Lerp(mouseY, 0, Time.deltaTime / (Time.deltaTime + 0.1f));
  62.         }
  63.  
  64.         forwardTransform.localRotation = Quaternion.Inverse(Quaternion.Euler(0.0f, vrCameraTransform.localRotation.eulerAngles.y, 0.0f));
  65.         rotationTransform.localRotation = Quaternion.Euler(0, vrCameraTransform.localRotation.eulerAngles.y, 0.0f) * Quaternion.Euler(mouseY, mouseX, mouseZ);
  66.     }
  67.  
  68.  
  69. #endif
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement