如何通过使用移动陀螺仪输入旋转相机时锁定相机z的旋转 [英] How to lock rotation in z of my camera while rotating the camera by using mobile gyroscope input
问题描述
我正在使用陀螺仪输入来统一旋转相机,因为 我希望玩家环顾x,y. 但是,z也会以某种方式旋转. 如何保持z旋转为0,同时至少保持我现在拥有的x和y的平滑旋转?
I am rotating the camera in unity using gyroscope input, because I want the player to look around the x,y. However somehow there is also rotation in z. How do I keep the z rotation at 0, while at least remaining the smooth rotation in x and y I have now?
一直在谷歌上搜索,但是还没有找到任何能使搜索结果保持真实状态的解决方案 z为0.
Have been googling but haven't found any solutions which actually keep the z at 0.
这是我用于旋转的代码,它已附加在相机上.
Here is the code I use for rotation, it's attached to the camera.
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);
行.尝试设置旋转值而不是乘以:
My guess is the problem comes from the transform.rotation *= Quaternion.Euler(gyro_In_x, gyro_In_y, 0);
line. Trying setting the rotation value instead of multiplying it:
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);
}
希望这会有所帮助,
这篇关于如何通过使用移动陀螺仪输入旋转相机时锁定相机z的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!