3

我在 c# 中编写了一个脚本来测试 unity3d 4.0 中的陀螺仪。并获得信息打击: 在此处输入图像描述

但是我旋转或移动我的 google 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");
        }
    }


}  
4

1 回答 1

6

您必须启用陀螺仪。您的 Start 方法应如下所示:

void Start ()
{
    gyoBool = SystemInfo.supportsGyroscope;

    if( gyoBool ) {
       gyo1=Input.gyro;
       gyo1.enabled = true;
    }

    Debug.Log (gyoBool.ToString ());
}

此外,您不需要每帧都将陀螺仪分配给“gyo1”(即:从您的 Update 方法中删除 gyo1=Input.gyro; )。

于 2013-09-30T20:27:51.590 回答