我想用Unity的陀螺仪控制相机,我在另一篇文章中看到了有关如何执行此操作的代码。但是Google Pixel和Samsung S7的行为有所不同,我必须测试该应用程序。似乎具有相同代码的同一场景沿其中一个轴翻转了180度。
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;
public class GyroControl : MonoBehaviour {
private bool gyroEnabled;
private Gyroscope gyroscope;
private GameObject cameraContainer;
private Quaternion rotation;
void Start () {
cameraContainer = new GameObject ("Camera Container");
cameraContainer.transform.position = transform.position;
transform.SetParent (cameraContainer.transform);
Screen.sleepTimeout = SleepTimeout.NeverSleep;
gyroEnabled = EnableGyro ();
}
private bool EnableGyro() {
if (SystemInfo.supportsGyroscope) {
gyroscope = Input.gyro;
gyroscope.enabled = true;
cameraContainer.transform.rotation = Quaternion.Euler (90.0f, 0.0f, 0.0f);
rotation = new Quaternion (0, 0, 1, 0);
return true;
}
return false;
}
void Update () {
if (gyroEnabled) {
transform.localRotation = gyroscope.attitude * rotation;
}
}
} 代码在这里