Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recenter or Reorient view with Cardboard SDK on Unity

With Unity, the CardboardHead script is added to the main camera and that handles everything quite nicely, but I need to be able to "recenter" the view on demand and the only option I see so far is to rorate the entire scene and it seems like this is something the would address first-hand and I can't find anything in the docs.

With Oculus Mobile SDK (GearVR), it would be OVRCamera.ResetCameraPositionOrientation(Vector3.one, Vector3.zero, Vector3.up, Vector3.zero); though they handle it nicely each time the viewer is put on so it's rarely needed there.

like image 262
rainabba Avatar asked Dec 17 '14 04:12

rainabba


1 Answers

There's a "target" parameter on the CardboardHead that lets you use to another gameobject as a reference for rotation. Or you can use a dummy parent gameobject. Either way, when you want to recenter, you set this reference object's rotation so that the CardboardHead is now pointing forward. Add this function to an script on the CardboardHead (or just add it into that script):

public void Recenter() {
    Transform reference = target != null ? target : transform.parent;
    if (reference != null) {
        reference.rotation = Quaternion.Inverse(transform.rotation) * reference.rotation;
        // next line is optional -- try it with and without
        reference.rotation = Quaternion.FromToRotation(reference.up, Vector3.up) * reference.rotation;
    }
}
like image 56
smd Avatar answered Oct 17 '22 06:10

smd