Currently in Unity 5.2 VR HMD position tracking is auto-magically applied to your camera. So as you move your HMD’s position in the real world, it is also applied to the Game Object that has the camera on it.
So, what happens when you have a more complex hierarchy? Does the parent get moved or the camera game object?
Here’s a hierarchy with a body at the top, a child head (and a child face just so we can see which direction the head is facing more clearly).
Now, when we run this scene and move the HMD around in space, we get this:
The answer is the Game Object with the camera only moves (and rotates).
Now, with that knowledge, if we want to implement a weapon that stays pointing forwards relative to the body, we can create this by making a child of the body named “Forward Direction”, and copying the Y rotation from the head (VR Camera) and keeping the other X,Z as 0. The important part is the Forward Direction transform is centered with the body pivot by making sure it’s X,Z position is 0.
The Forward direction simply needs this code in it’s Update (where CopyTransform is the head camera):
transform.localEulerAngles = new Vector3(0f, CopyTransform.localEulerAngles.y, 0f);
Now, what happens if you Recenter tracking?
UnityEngine.VR.InputTracking.Recenter();
The head will reset the Y to 0 to face down the Z-axis and the forward direction will match it. Nice!
What about position? Well the local position X and Z are reset to to 0. (eg: if you walk backwards from the tracking sensor z goes negative). And the Y (for me) is set to 0.82. Which I expect is based on the user height profile.
Here’s what the transform looks like just after called Recenter:
Leave a Reply