下面给大家介绍下Unity中实现陀螺仪功能,实现旋转设备,摄像机跟随旋转的效果,一起来看看吧。
将下面脚本拖拽到摄像机上,打包为Android或iOS项目,在真机上测试即可;
场景中要放一些模型,不然看不到效果:
- using UnityEngine;
- using System.Collections;
-
- public class AAA : MonoBehaviour {
-
- private const float lowPassFilterFactor = 0.2f;
- protected void Start()
- {
-
- Input.gyro.enabled = true;
-
- Vector3 deviceGravity = Input.gyro.gravity;
-
- Vector3 rotationVelocity = Input.gyro.rotationRate;
-
- Vector3 rotationVelocity2 = Input.gyro.rotationRateUnbiased;
-
- Input.gyro.updateInterval = 0.1f;
-
- Vector3 acceleration = Input.gyro.userAcceleration;
- }
-
- protected void Update()
- {
-
- 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);
- }
- }