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
原文地址:https://www.cnblogs.com/weiqiangwaideshijie/p/6840588.html