Reputation: 71
I write a script in c# to test the Gyroscope in unity3d 4.0.
and get the information blow:
However I rotate or move my google nexus 7. Every parameter keeps "0" ; I don't know why.
anyone can help me ?
here is my code:
using UnityEngine;
using System.Collections;
public class gyroscope : MonoBehaviour
{
private Gyroscope gyo1;
private bool gyoBool;
//private Quaternion rotFix;
// Use this for initialization
void Start ()
{
gyoBool = SystemInfo.supportsGyroscope;
Debug.Log (gyoBool.ToString ());
}
// Update is called once per frame
void Update ()
{
gyo1=Input.gyro;
}
void OnGUI ()
{
if (gyoBool != null)
{
GUI.Label (new Rect (10, Screen.height / 2 - 50, 100, 100), gyoBool.ToString ());
if (gyoBool == true)
{
GUI.Label (new Rect (10, Screen.height / 2-100, 500, 100), "gyro supported");
GUI.Label (new Rect (10, Screen.height / 2, 500, 100), "rotation rate:" + gyo1.rotationRate.ToString ());
GUI.Label (new Rect (10, Screen.height / 2 + 50, 500, 100), "gravity: " + gyo1.gravity.ToString ());
GUI.Label (new Rect (10, Screen.height / 2 + 100, 500, 100), "attitude: " + gyo1.attitude.ToString ());
GUI.Label (new Rect (10, Screen.height / 2 + 150, 500, 100), "type: " + gyo1.GetType ().ToString ());
}
else
GUI.Label (new Rect (Screen.width / 2 - 100, Screen.height / 2, 100, 100), "not supported");
}
}
}
Upvotes: 3
Views: 7794
Reputation: 620
You must enable the Gyroscope. Your Start method should look like this:
void Start ()
{
gyoBool = SystemInfo.supportsGyroscope;
if( gyoBool ) {
gyo1=Input.gyro;
gyo1.enabled = true;
}
Debug.Log (gyoBool.ToString ());
}
Also you don't need to assign the gyroscope to "gyo1" every frame (ie: remove gyo1=Input.gyro; from your Update method).
Upvotes: 6