How do you reset view with Google Cardboard SDK?

I have a 3D Google Cardboard first person controller set up with a reticle, but the stereo game camera is a child of this controller with it’s own independent rotation based on the Cardboard SDK head tracking. So when I move the stick on my controller, my whole view moves with the reticle in the center, but I can also look around and away from my reticle.

Right now I’m setting up a parent object that rotates based on controller input, then the Cardboard head is a child of that which rotates based on Cardboard.SDK.HeadRotation. This is working just like I need it too, but I’m trying to set up a button to reset the Cardboard head rotation back to match the parent (basically set it back to 0 rotation).

I’ve tried this answer but it’s not working, and I don’t think it’s doing what I want. I don’t want to move the reference. The closest I’ve gotten is the code example below, but what is happening now is when I look 90 degrees to the right or left, then press the reset button to snap my view back to the reticle it goes back and centers the reticle like I want. However, at this point if I try to look up, instead of the view going up away from the reticle, it rotates sideways as if I was still looking 90 degrees the other direction and trying to look up. I think this has to do with the Cardboard.SDK.HeadRotation being fixed in space so it’s still getting the rotation as if I was looking the other direction and I need to do something other than the inverse to reset the value, but I don’t know what to do.

private Quaternion resetModifier;
// if you aren't using a parent to control the reticle you can set a target value
// public Transform target;

void update () {
	resetModifer = Quaternion.identity;
}

void update () {
	if (Input.GetButton ("Fire3")) {
		resetModifier = Quaternion.Inverse(Cardbaord.SDK.HeadRotation);
	}
	var rot = Cardboard.SDK.HeadRotation * resetModifier;
	transform.rotation = rot;
	// if using target for transfrom use the following
	// transform.rotation = rot * target.rotation;
}

Cardboard SDK v0.4.5 added a Recenter() function.

I figured out a way that is working for me. I added this answer to another question that I didn’t see before asking it.

My biggest problem was that Cardbaord.SDK.HeadRotation is read-only and it’s a Quaternion that is stuck in 3D space and can’t be manipulated like you want. So instead of using the Cardboard.SDK.HeadRotation to directly control your camera game object’s position, I calculated the change in rotation between updates and applied that rotation to my camera game object.

I’m very new to this and I’m writing this example code off the top of my head, so there may be errors, but it’s close to what I’ve implemented that is working.

//used to set a game-object, such as a parent, as a point reference for resetting view
public Transform target;
private Quaternion prevRotation;

void Start () {
	//set your first rotation as starting head rotation
	prevRotation = Cardboard.SDK.HeadRotation;

}

void Update () {
	//on button push
	if (Input.GetButton ("Reset")) {
		//doesn't have to reset to target, can reset to anything you need
		//such as Quaternion.identity
		transform.rotation = target.rotation;

	} else {
	
		//here you set the the rotation to the difference between the current head
		//rotation and your previous head rotation
		var rot = Quaternion.Euler (Cardboard.SDK.HeadRotation.eulerAngles
						- prevRotation.eulerAngles);

		//add this difference to current rotation by using the Quaternion operator '*'
		transform.rotation *= rot;

	}

	//save the current head rotation as prevRotation to be used next update
	prevRotation = Cardboard.SDK.HeadRotation;

}

Please note I’m new to unity and programming in general, but this is currently working for the game I made, so I hope it helps!

A easy way to reset the view.

using UnityEngine;
using System.Collections;
public class ReiniciaGCSDK : MonoBehaviour {
	private GvrViewer gr;
	void Start () {
		gr = new GvrViewer ();
	}

	void Update () {
		if (Input.GetKey ("joystick button 6")) {
			gr.Recenter ();
			print ("Restart :D")
		}

	}
	}