Search code examples
unity-game-enginevirtual-reality

I'm unable to move the cursor in play mode in the Unity VR example scene


I'm using the VR Samples from Unity https://www.assetstore.unity3d.com/en/#!/content/51519 with the scene 'Shooter 360' (though I've tried the other scenes).

I'm on a Mac and in XR settings I have both 'Mock HMD - HTC' and 'Oculus SDK' supported.

When I press Play, I'm unable to move the cursor or interact with the GUI at all. Any recommendations?

screenshot


Solution

  • By default it is the SDK you use that will rotate the camera at runtime (Cardboard, Oculus, HTC Vive, ...).

    You can use a script that will rotate the camera while in edit mode. Here is an example, just assign this script to your camera and press Left Ctrl while moving the mouse in the editor window:

    public class EditorCameraController : MonoBehaviour 
    {    
        public float sensitivityX = 15F;
        public float sensitivityY = 15F;
        public float minimumX = -360F;
        public float maximumX = 360F;
        public float minimumY = -60F;
        public float maximumY = 60F;
        private float rotationX = 0F;
        private float rotationY = 0F;
        private Quaternion originalRotation;
    
        void Start()
        {
            originalRotation = transform.localRotation;
        }
    
        void Update () 
        {
            if (Input.GetKey(KeyCode.LeftControl))
            {
                rotationX += Input.GetAxis("Mouse X") * sensitivityX;
                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                rotationX = ClampAngle(rotationX, minimumX, maximumX);
                rotationY = ClampAngle(rotationY, minimumY, maximumY);
                Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
                Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, -Vector3.right);
                transform.localRotation = originalRotation * xQuaternion * yQuaternion;
            }
        }
    
        public static float ClampAngle(float angle, float min, float max)
        {
            if (angle < -360F)
             angle += 360F;
            if (angle > 360F)
             angle -= 360F;
            return Mathf.Clamp(angle, min, max);
        }
    }