我使用陀螺仪输入统一旋转相机,因为 我希望玩家环顾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);
}
答案 0 :(得分:1)
我猜这个问题来自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);
}
希望这有帮助,