我一直在尝试通过使用加速度计和指南针来模拟陀螺仪的行为。到目前为止,我已经实现了一些功能,但是仍然存在滞后;响应延迟。它不能像移动电话的倾斜一样快地改变视图。感谢您的帮助,谢谢。
注意:仍未添加Z轴旋转。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GyroSimulator : MonoBehaviour {
float zValue;
float zValueMinMax = 5.0f;
Vector3 accelometerSmoothValue;
//Smooth Accelerometer
public float AccelerometerUpdateInterval = 1.0f / 100.0f;
public float LowPassKernelWidthInSeconds = 0.001f;
public Vector3 lowPassValue = Vector3.zero;
void Start () {
Screen.orientation = ScreenOrientation.LandscapeLeft;
Input.compass.enabled = true;
}
//Update is called once per frame
void Update () {
//Set Z Min Max
if (zValue < -zValueMinMax)
zValue = -zValueMinMax;
if (zValue > zValueMinMax)
zValue = zValueMinMax;
accelometerSmoothValue = LowPass();
zValue += accelometerSmoothValue.z;
transform.rotation = Quaternion.Slerp(
transform.rotation,
Quaternion.Euler(
-zValue*5.0f,
Input.compass.magneticHeading,
0
),
Time.deltaTime*5.0f
);
}
Vector3 LowPass()
{
Vector3 tilt = Input.acceleration;
float LowPassFilterFactor = AccelerometerUpdateInterval / LowPassKernelWidthInSeconds;
lowPassValue = Vector3.Lerp(lowPassValue, tilt, LowPassFilterFactor);
return lowPassValue;
}
}