Recenter or Reorient view with Cardboard SDK on Unity
Asked Answered
M

4

9

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.

Mccall answered 17/12, 2014 at 4:40 Comment(0)
S
6

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;
    }
}
Snipe answered 17/12, 2014 at 23:23 Comment(0)
T
6

Cardboard.SDK.Recenter (); should do the trick.

Recenter orientation Added Recenter() function to Cardboard.SDK, which resets the head tracker so the phone's current heading becomes the forward direction (+Z axis).

Couldn't find the docs for the API/SDK but it's in the release notes for the v0.4.5 Update.

Teofilateosinte answered 5/8, 2015 at 22:31 Comment(1)
This used to do the trick in v0.7.0 but since I've upgraded to 0.9.1 it doesn't work anymore. Very strange.Westlund
B
1

You can rotate the Cardboard Main to point in a certain direction.

This is what worked for me when I wanted the app to start up pointing a certain way. Since the CardboardHead points at Vector3.zero on startup if no target is assigned, I ran a function during Start() for the CardboardMain that would point in the direction I wanted.

Of course, if you're already rotating CardboardMain for some other reason, it may be possible to use this same method by creating a parent of the CardboardHead (child of CardboardMain) and doing the same thing.

Balcony answered 23/5, 2016 at 21:59 Comment(0)
D
1

This question is a bit old but for Google VR SDK 1.50+ you can do

transform.eulerAngles = new Vector3(newRot.x, newRot.y, newRot.z);
UnityEngine.VR.InputTracking.Recenter();

also, if you don't want to get confused you also need to catch the GvrEditorEmulator instance and Recenter it as well.

#if UNITY_EDITOR
        gvrEditorEmulator.Recenter();
#endif

Recentering GvrEditorEmulator though doesn't seem to work very well at the moment but if you disable it you'll see the recentering works for the main camera.

Directorial answered 10/7, 2017 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.