Set Local Rotation of VR Camera Rig

Hi Everybody,

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:

  1. The left stick strafes the player forward, backward, left, and right according to the player’s BODY orientation.
  2. The right stick left and right inputs rotate the player’s BODY left and right, respectively.
  3. The Gear VR controls the head movement of the player (we are assuming the player has owl-like head movement).
  4. There is an indicator attached to the player’s field of view that tells them which direction their body is facing.
  5. 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.

I don’t fully understand - there seems to be a contradiction in having the left stick strafe relative to the body, but lerping the body rotation to head direction when you strafe. Why not just have strafe relative to the head direction?

But if you did want to do it that way for some reason, perhaps you could just store a logical y-rotation as a float and have the camera not rotate the parent object.
The right stick will just add or subtract from this rotation, and the left stick move the camera along the orthogonal directions rotated by the logical rotation.
Then, when you strafe, you get the head rotation’s y component and lerp your rotation float to that value.