Reputation: 953
I am making a google cardboard game using unity3d and google cardboard sdk. I would like to find out if the best way to disable/enable head tracking so that I can stop the game when it ends or before it starts.
Upvotes: 1
Views: 2370
Reputation: 3987
On September the 6th 2017, the GvrHead.cs does not exist anymore. I have been able to disable head tracking with the following code:
VRDevice.DisableAutoVRCameraTracking(Camera.main, true);
Reference page: https://docs.unity3d.com/ScriptReference/VR.VRDevice.DisableAutoVRCameraTracking.html
Notice it only works in the smartphone after building it. It does not work in the Unity Game preview, and it has other issues. I have asked about these issues here: https://github.com/googlevr/gvr-unity-sdk/issues/716
So the long best solution is probably:
private void TrackPositionAndRotation (bool track){
if (track) {
#if UNITY_EDITOR
FindObjectOfType<GvrEditorEmulator>().enabled = true;
#endif // UNITY_EDITOR
#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
VRDevice.DisableAutoVRCameraTracking(Camera.main, false);
#endif // (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
//cardboardHead.GetComponent<GvrHead>().trackPosition = true;
//cardboardHead.GetComponent<GvrHead>().trackRotation = true;
} else {
#if UNITY_EDITOR
FindObjectOfType<GvrEditorEmulator>().enabled = false;
#endif // UNITY_EDITOR
#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
VRDevice.DisableAutoVRCameraTracking(Camera.main, true);
#endif // (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
//cardboardHead.GetComponent<GvrHead>().trackPosition = false;
//cardboardHead.GetComponent<GvrHead>().trackRotation = false;
}
}
UPDATE 2017.11.29 The above code won't work because VR has changed to XR. Check: https://docs.unity3d.com/ScriptReference/XR.InputTracking-disablePositionalTracking.html
Upvotes: 0
Reputation: 21
I just found how to disable the head tracking on the current version of Google VR: GVR Unity SDK v1.10.0.
The way to do it is adding the GvrHead Script to the GvrViewerMain Prefab. After this you can make a simple code to access the head tracking, which is controlled by the GvrHead Script.
GameObject.Find ("VRPlayer").GetComponent<GvrHead> ().trackRotation = false;
GameObject.Find ("VRPlayer").GetComponent<GvrHead> ().trackPosition = false;
Write this code on a trigger or event, or wherever you want, to disable the head tracking.
This code find the attribute public bool trackRotation = true; and public bool trackPosition = true; on the GvrHead and change de bool to false.
To understand why this code lines enterely disable the headtracking, take a look at the GvrHead script.
To change back a enable the Headtracking change again the bool value of the GvrHead Script with the same type of code.
GameObject.Find ("VRPlayer").GetComponent<GvrHead> ().trackRotation = true;
GameObject.Find ("VRPlayer").GetComponent<GvrHead> ().trackPosition = true;
The Script and prefab came by deffault in the GVR Unity SDK package.
Example
I used it for a trigger object. When the player look right to the object, the code runs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StopHeadTrack : MonoBehaviour, IGvrGazeResponder {
public Animator anim;
public void OnGazeEnter () {
GameObject.Find ("VRPlayer").GetComponent<GvrHead> ().trackRotation = false;
GameObject.Find ("VRPlayer").GetComponent<GvrHead> ().trackPosition = false;
}
Upvotes: 2
Reputation: 61
You can disable tracking for both position and rotation.
GameObject.Find("Head").GetComponent<CardboardHead>().trackRotation = false;
Upvotes: 0
Reputation: 550
Use OnHeadUpdated
event
Following this code.
CardboardHead head;
void Start () {
head = GameObject.Find("Head").GetComponent<CardboardHead>();
head.OnHeadUpdated += Test_OnHeadUpdated;
}
private void Test_OnHeadUpdated(GameObject head_obj)
{
head_obj.transform.rotation = Quaternion.identity;
head_obj.transform.position = //the position when you stop cardboard;
}
Upvotes: 1