I have a first person environment that I am building a custom navigation for with Gear VR and a MOGA controller. I have built the navigation to do the following:
- The left stick strafes the player forward, backward, left, and right according to the player’s BODY orientation.
- The right stick left and right inputs rotate the player’s BODY left and right, respectively.
- The Gear VR controls the head movement of the player (we are assuming the player has owl-like head movement).
- There is an indicator attached to the player’s field of view that tells them which direction their body is facing.
- When the player begins to strafe forward or backward, the camera lerps to align with the body. This means that if they specifically want to go the direction they are facing, their body will cooperate and turn around.
The problem is, while I can use a lerp to set my normal main camera’s rotation to match my body’s rotation, I can’t seem to set my VR camera’s rotation to match my body’s rotation. When I attempt to rotate the VR camera using camera.main, or any reference to camera.main, nothing happens. When I attempt to use UnityEngine.VR.InputTracking I have the option to GetLocalRotation but not SetLocalRotation and I don’t seem to be able to use the two interchangeably.
If I manually make a parent to the camera, the camera’s parent’s local rotation doesn’t give me any data on where the VR camera is facing that I can use to start a lerp. If I manually apply the lerp to a child of the camera, it won’t rotate the parent.
How do I do the following to my VR camera?
_startHorizontalRotation = _playerTransform.rotation.eulerAngles.y; _endHorizontalRotation = _cameraTransform.rotation.eulerAngles.y; _startLocalHorizontalRotation = _cameraTransform.localRotation.eulerAngles.y; _endLocalHorizontalRotation = 0.0f; Vector3 _startLocalVector = new Vector3 (0, _startLocalHorizontalRotation,0); Vector3 _endLocalVector = new Vector3 (0, _endLocalHorizontalRotation, 0); Vector3 counterLerpCamera = Vector3.Lerp (_startLocalVector, _endLocalVector, percentageComplete); _cameraTransform.localRotation = Quaternion.Euler (counterLerpCamera);
In the editor, this spins the player’s body to their head direction while resetting their head direction to a local rotation of 0 on their body. Works perfectly, but I lose the ability to rotate the camera when switching to VR mode.
This is the important line that needs to change:
_cameraTransform.localRotation = Quaternion.Euler (counterLerpCamera);
Where _cameraTransform.localRotation needs to set the VR camera rig’s local rotation.