Unity3D之陀螺儀(轉)


Unity陀螺儀功能

 

實現陀螺儀功能,旋轉設備,攝像機跟隨旋轉

將下面腳本拖拽到攝像機上,打包為AndroidiOS項目,在真機上測試即可

場景中要放一些模型,不然看不到效果

  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class AAA : MonoBehaviour {  
  5.   
  6.     private const float lowPassFilterFactor = 0.2f;  
  7.     protected void Start()  
  8.     {  
  9.         //設置設備陀螺儀的開啟/關閉狀態,使用陀螺儀功能必須設置為 true  
  10.         Input.gyro.enabled = true;  
  11.         //獲取設備重力加速度向量  
  12.         Vector3 deviceGravity = Input.gyro.gravity;  
  13.         //設備的旋轉速度,返回結果為x,y,z軸的旋轉速度,單位為(弧度/秒)  
  14.         Vector3 rotationVelocity = Input.gyro.rotationRate;  
  15.         //獲取更加精確的旋轉  
  16.         Vector3 rotationVelocity2 = Input.gyro.rotationRateUnbiased;  
  17.         //設置陀螺儀的更新檢索時間,即隔 0.1秒更新一次  
  18.         Input.gyro.updateInterval = 0.1f;  
  19.         //獲取移除重力加速度后設備的加速度  
  20.         Vector3 acceleration = Input.gyro.userAcceleration;  
  21.     }  
  22.   
  23.     protected void Update()  
  24.     {  
  25.         //Input.gyro.attitude 返回值為 Quaternion類型,即設備旋轉歐拉角  
  26.         transform.rotation = Quaternion.Slerp(transform.rotation, Input.gyro.attitude, lowPassFilterFactor);  
  27.     }  
  28.   
  29.     void OnGUI()  
  30.     {  
  31.         GUI.Label(new Rect(50, 100, 500, 20), "Label : " + Input.gyro.attitude.x + "       " + Input.gyro.attitude.y + "         " + Input.gyro.attitude.z);  
  32.     }  
  33. }  
  34.                                                                                                                                              轉自:http://blog.csdn.net/liqiangeastsun/article/details/42744257


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM