ray*_*ray 3 c# unity-game-engine gyroscope
我在c#中编写了一个脚本来测试unity3d 4.0中的陀螺仪.并得到信息打击:

然而,我旋转或移动我的谷歌nexus 7.每个参数保持"0"; 我不知道为什么.
有人可以帮帮我吗?
这是我的代码:
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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
您必须启用陀螺仪.您的Start方法应如下所示:
void Start ()
{
gyoBool = SystemInfo.supportsGyroscope;
if( gyoBool ) {
gyo1=Input.gyro;
gyo1.enabled = true;
}
Debug.Log (gyoBool.ToString ());
}
Run Code Online (Sandbox Code Playgroud)
此外,您不需要每帧都将陀螺仪分配给"gyo1"(即:从您的Update方法中删除gyo1 = Input.gyro;).