>我使用陀螺仪输入统一旋转相机,因为我希望玩家环顾x,y。然而,不知何故,z 中也有旋转。如何将 z 旋转保持在 0,同时至少保持我现在拥有的 x 和 y 的平滑旋转?
一直在谷歌搜索,但还没有找到任何真正保持z 在 0.
这是我用于旋转的代码,它附加到相机上。
private void Start()
{
_rb = GetComponent<Rigidbody>();
Input.gyro.enabled = true;
}
private void Update()
{
float gyro_In_x = -Input.gyro.rotationRateUnbiased.x;
float gyro_In_y = -Input.gyro.rotationRateUnbiased.y;
transform.Rotate(gyro_In_x, gyro_In_y, 0);
}
我的猜测是问题来自transform.rotation *= Quaternion.Euler(gyro_In_x, gyro_In_y, 0);
行。尝试设置旋转值而不是将其相乘:
private void Start()
{
_rb = GetComponent<Rigidbody>();
Input.gyro.enabled = true;
}
private void Update()
{
Vector3 previousEulerAngles = transform.eulerAngles;
Vector3 gyroInput = -Input.gyro.rotationRateUnbiased; //Not sure about the minus symbol (untested)
Vector3 targetEulerAngles = previousEulerAngles + gyroInput * Time.deltaTime * Mathf.Rad2Deg;
targetEulerAngles.z = 0.0f;
transform.eulerAngles = targetEulerAngles;
//You should also be able do it in one line if you want:
//transform.eulerAngles = new Vector3(transform.eulerAngles.x - Input.gyro.rotationRateUnbiased.x * Time.deltaTime * Mathf.Rad2Deg, transform.eulerAngles.y - Input.gyro.rotationRateUnbiased.y * Time.deltaTime * Mathf.Rad2Deg, 0.0f);
}
希望这有帮助,