Unity3D 陀螺仪两种方法

来源:互联网 发布:素描教学软件下载 编辑:程序博客网 时间:2024/06/05 05:19
  //01      Gyroscope gyro;    Quaternion quatMult;    Quaternion quatMap;    GameObject player;    GameObject camParent;    void Awake()    {        player = GameObject.Find("Player");        // find the current parent of the camera's transform          Transform currentParent = transform.parent;        // instantiate a new transform          camParent = new GameObject("camParent");        // match the transform to the camera position          camParent.transform.position = transform.position;        // make the new transform the parent of the camera transform          transform.parent = camParent.transform;        // make the original parent the grandparent of the camera transform          //camParent.transform.parent = currentParent;          // instantiate a new transform          GameObject camGrandparent = new GameObject("camGrandParent");        // match the transform to the camera position          camGrandparent.transform.position = transform.position;        // make the new transform the parent of the camera transform          camParent.transform.parent = camGrandparent.transform;        // make the original parent the grandparent of the camera transform          camGrandparent.transform.parent = currentParent;        //gyroBool = true;          //if (gyroBool) {          gyro = Input.gyro;        gyro.enabled = true;        camParent.transform.eulerAngles = new Vector3(90, 0, 0);        quatMult = new Quaternion(0, 0, 1, 0);    }    void Update()    {        quatMap = new Quaternion(gyro.attitude.x, gyro.attitude.y, gyro.attitude.z, gyro.attitude.w);        Quaternion qt = quatMap * quatMult;        transform.localRotation = qt;    }    // 两种方法  ----------------------- 02    bool draw = false;    bool gyinfo; Gyroscope go;    void Start()    {        gyinfo = SystemInfo.supportsGyroscope;        go = Input.gyro; go.enabled = true;    }    void Update()    {        if (gyinfo)        {            Vector3 a = go.attitude.eulerAngles; a = new Vector3(-a.x,   -a.y, a.z);            //直接使用读取的欧拉角发现不对,于是自己调整一下符号             this.transform.eulerAngles = a; this.transform.Rotate(Vector3.right * 90, Space.World);        }    }