unity陀螺仪

using UnityEngine;  
using System.Collections;  
   
public class gyroscope : MonoBehaviour {
    
      
   
    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);          
            draw = false;  
        }  
        else 
        {
    
      
            draw = true;  
        }  
    }  
   
    void OnGUI()  
    {
    
      
        if (draw)  
        {
    
      
            GUI.Label(new Rect(100, 100, 100, 30), "启动失败");  
        }  
    }  
      
}

https://blog.csdn.net/xingqing_myz/article/details/78407796

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraControllerByGyro : MonoBehaviour
{
    
    
    private const float slerpFactor = 0.5f;
    // Use this for initialization
    void Start()
    {
    
    
        Input.gyro.enabled = true;
        Input.gyro.updateInterval = 0.05f;
    }

    // Update is called once per frame
    void Update()
    {
    
    
        if (Input.gyro.enabled)
        {
    
    
            transform.rotation = Quaternion.Slerp(transform.rotation, ConvertRotation(Input.gyro.attitude), slerpFactor);
       //transform.rotation = ConvertRotation(Input.gyro.attitude);
        }
    }

    private Quaternion ConvertRotation(Quaternion q)
    {
    
    
        return Quaternion.Euler(90, 0, 0) * (new Quaternion(-q.x, -q.y, q.z, q.w)); //横屏旋转90度,然后左右手坐标系反转。
    }
}

猜你喜欢

转载自blog.csdn.net/wodownload2/article/details/112650139