Reset Vive Camera

Hello all,
I’m working on a driving game using VR, and I’m having problems always getting the VR camera in the right position. I want to make it so no matter where the user faces, they’ll be able to see out the front of the car. I have a non-VR camera in the position that the person should be looking, and I’d like the VR camera to be initialized in that same position

EDIT: apparently my whole post didn’t get posted. I used code found here, but it didn’t seem to work. I think it has to do with how I’m changing the position of the game object holder for the camera, since that code isn’t posted, but rather is a function that is called in this post OpenVR - How to reset camera properly? - Unity Engine - Unity Discussions

If it’s Vive, what you want to do is make sure the tracking space is “TrackingUniverseSeated”, and then call this to recenter the camera after the user is where they want to look:

Valve.VR.OpenVR.System.ResetSeatedZeroPose ();

What that does is set the direction/place the user is looking to be the 0,0,0 for the headset camera’s local position/orientation, so the view will match the position/orientation of whatever the camera’s parent is. Unity has its own built-in centering call which will probably work with other headsets, but I don’t remember what it is because last time I tried it it didn’t work with Vive.

My full post didn’t get posted! I tried using that, but it didn’t end up working exactly like I wanted. I think I might be doing something wrong, here’s what I have:

private void RecenterHeadsetForCarSitting()
{
        GameObject car = GameObject.Find("CarName");
        Quaternion temp = car.transform.rotation;
        car.transform.rotation = Quaternion.identity;

        Valve.VR.OpenVR.System.ResetSeatedZeroPose();
        Valve.VR.OpenVR.Compositor.SetTrackingSpace(Valve.VR.ETrackingUniverseOrigin.TrackingUniverseSeated);
        currentCamera.transform.position = NonVRCamera.transform.position;
        currentCamera.transform.position = new Vector3(currentCamera.transform.position.x, 0, currentCamera.transform.position.z);
}

I see two problems there: first, you are setting the tracking space to seated after trying to reset the space, rather than before. You should only need to do that once, at the start of your app, rather than every time you recenter the camera (and if you are using the VR prefab from the SDK where you can set the tracking space in the Inspector, you only have to have it set to seated there).

The other problem is you are changing the VR camera position after resetting it. Resetting the VR camera sets the direction it is facing to be local position (0,0,0) and local rotation (Quaternion.identity). After that the local values are going to be wherever the user’s head moves/looks as offsets from that, you cannot set those values yourself. You need the VR camera to be a child of something that is where you want the user’s virtual head to be; that is the thing you can move/rotate, and the VR camera’s local position/rotation will be offsets from that.

Sorry, I named some variables wrong when I pasted since I had changed some things while testing this. currentCamera reference the camera holder for the VR Camera. Which gets moved to the NonVR camera position which is where it should be. That’s what I’m supposed to be doing correct? Or should I not even be doing that (i.e, does doing
Valve.VR.OpenVR.System.ResetSeatedZeroPose();
Valve.VR.OpenVR.Compositor.SetTrackingSpace(Valve.VR.ETrackingUniverseOrigin.TrackingUniverseSeated);
reset everything?)

Well, again you need to reset zero pose after setting it to seated, not before. What that does is make it so wherever the user is looking is local position (0,0,0) and local rotation (Quaternion.identity) for all VR cameras (so looking straight is looking exactly in whatever direction the camera’s parent is pointed).

I swapped the lines of code to be

        Valve.VR.OpenVR.Compositor.SetTrackingSpace(Valve.VR.ETrackingUniverseOrigin.TrackingUniverseSeated);
        Valve.VR.OpenVR.System.ResetSeatedZeroPose();

but it doesn’t seem to do anything differently. I know that the correct way to face is right toward my monitors. But if I start the game with the headset facing the opposite direction, it will look out the back of the car. Which is obviously not what I want

Make sure you aren’t resetting the camera position until after everything has fully started - if you do it too soon the headset may not have gotten its current position initialized (meaning, bind that to a recentering key or something). And of course make sure the camera’s parent is exactly where you want the camera to be, rotated to where you want it to look.

Ok, this is what I have now,

private IEnumerator RecenterHeadsetForCarSitting()
{
         yield return new WaitForEndOfFrame();
        GameObject car = GameObject.Find("CarName");
        Quaternion temp = car.transform.rotation;
        car.transform.rotation = Quaternion.identity;

        currentCamera.transform.position = NonVRCamera.transform.position;
        currentCamera.transform.position = new Vector3(currentCamera.transform.position.x, 0, currentCamera.transform.position.z);
        currentCamera.transform.rotation = temp;
        Valve.VR.OpenVR.Compositor.SetTrackingSpace(Valve.VR.ETrackingUniverseOrigin.TrackingUniverseSeated);
        Valve.VR.OpenVR.System.ResetSeatedZeroPose();

         car.transform.rotation = temp;
}

I know that everything has started, because this code doesn’t execute until a certain condition has happened–in my case, the person goes into a sitting position (done using some fancy code to determine seated or standing positions). But it still puts it at a wrong position. The rotation seems to be off, which is a main problem I’ve always had