Unity陀螺儀功能
實現陀螺儀功能,旋轉設備,攝像機跟隨旋轉
將下面腳本拖拽到攝像機上,打包為Android或iOS項目,在真機上測試即可
場景中要放一些模型,不然看不到效果
- using UnityEngine;
- using System.Collections;
- public class AAA : MonoBehaviour {
- private const float lowPassFilterFactor = 0.2f;
- protected void Start()
- {
- //設置設備陀螺儀的開啟/關閉狀態,使用陀螺儀功能必須設置為 true
- Input.gyro.enabled = true;
- //獲取設備重力加速度向量
- Vector3 deviceGravity = Input.gyro.gravity;
- //設備的旋轉速度,返回結果為x,y,z軸的旋轉速度,單位為(弧度/秒)
- Vector3 rotationVelocity = Input.gyro.rotationRate;
- //獲取更加精確的旋轉
- Vector3 rotationVelocity2 = Input.gyro.rotationRateUnbiased;
- //設置陀螺儀的更新檢索時間,即隔 0.1秒更新一次
- Input.gyro.updateInterval = 0.1f;
- //獲取移除重力加速度后設備的加速度
- Vector3 acceleration = Input.gyro.userAcceleration;
- }
- protected void Update()
- {
- //Input.gyro.attitude 返回值為 Quaternion類型,即設備旋轉歐拉角
- transform.rotation = Quaternion.Slerp(transform.rotation, Input.gyro.attitude, lowPassFilterFactor);
- }
- void OnGUI()
- {
- GUI.Label(new Rect(50, 100, 500, 20), "Label : " + Input.gyro.attitude.x + " " + Input.gyro.attitude.y + " " + Input.gyro.attitude.z);
- }
- }
- 轉自:http://blog.csdn.net/liqiangeastsun/article/details/42744257