实现原理:相机随着陀螺仪旋转,就可以用手机模拟vr头盔看到的世界
using UnityEngine;
//相机随着陀螺仪旋转
public class cameraRotation : MonoBehaviour
{
void Start()
{
if (Application.platform == RuntimePlatform.Android)
{
Input.gyro.enabled = true;//打开陀螺仪权限
Screen.sleepTimeout = SleepTimeout.NeverSleep;
Input.gyro.updateInterval = 60f;
}
}
void Update()
{
this.transform.localRotation = CameraRotation();
//this.transform.localRotation = Quaternion.Slerp(transform.rotation, CameraRotation(), 0.2f);
}
Quaternion CameraRotation()
{
Quaternion input = Input.gyro.attitude;
input = Quaternion.Euler(90, 0, 0) * (new Quaternion(-input.x, -input.y, input.z, input.w));
return input;
}
}
网友评论