- Home /
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:
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.
Hi @Ereptor. I've been trying to integrate $$anonymous$$oga controller in Unity 5. The sample asset is throwing null reference and null ptr errors. Did you face the same ? There is hardly any support from $$anonymous$$oga for their sdk.
Answer by mptp · Apr 15, 2016 at 01:48 AM
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.
I've been demonstrating movement styles in waves. I figured the most elegant solution was to have forward be the camera facing direction, but this wasn't what the user had in $$anonymous$$d. Aligning strafe to a constant position also wasn't the intended result, so we added a body rotation. This solved all of the client's needs except the fact that they still got confused when they were looking in a new direction and forgot to realign their body before moving forward, so I'm trying to solve that by just doing it for them when they are turned around.
It's convoluted, but it works for our purposes. For now what I'm planning to do is implement a mid-level parent (calling it a 'neck' informally) that will actually serve as my grabbable rotation handle on the camera. The only problem with this solution is that the neck will always be out of alignment with the head and body, and I'll have to test the camera and body's rotation separately ins$$anonymous$$d of getting a relative rotation.
I'm still greatly struggling with manipulating a mid-level parent in a simple way. I'll see if I can do the y-rotation float route.
The problem remains, though, that rotating the camera's parent also rotates the camera; however, I can't rotate the camera itself. In order to make the camera "stand still", I need to be able to grab something related to the camera and rotate it opposite of the body.
There needs to be a rotation and a counter-rotation in order for the camera to stay in the same place.
Your answer
Follow this Question
Related Questions
Unity app not running in Android phone. 0 Answers
GearVR wont initialize the scene (working fine in Editor) 0 Answers
How can i run a Unity Build on my PC, but use my phone as second screen, (Cheapskate Oculus-Like) 2 Answers
Speech Recognition for Android (For Beginner) 0 Answers
VR app spins uncontrollably on iOS 14 (Unity 2019.4.11) 0 Answers