Unity C# 四元数旋转校正方向

本文关键字:方向 旋转 四元数 Unity | 更新日期: 2023-09-27 18:36:43

IHi 我一直在研究相机旋转方法。

我正在尝试使用鼠标输入旋转相机。我需要水平旋转平行于 x,y 平面(我的板),垂直旋转限制为一定角度(例如 40 度到 -40)。

我使用四元数来旋转相机。

到目前为止,这是我的代码

float angle = (mouseX_Current - mouseX_ActionStart) * 0.25f;
Quaternion horizontalRotationQuat = Quaternion.AngleAxis(angle, Vector3.up);
angle = (mouseY_Current - mouseY_ActionStart) * 0.25f;
Quaternion verticalRotationQuat = Quaternion.AngleAxis(angle, Vector3.right);
Camera.main.transform.rotation *= (horizontalRotationQuat * verticalRotationQuat);

我的问题是,通过将这些与鼠标相关的旋转添加到我当前的相机方向四元数中,相机不再与它正在查看的平面(x,y)平行。

添加那些与鼠标相关的旋转后,我一直在尝试创建一个校正四元数以添加到相机四元数中,但我似乎找不到合适的四元数。

Quaternion currentOrientationQuat = Camera.main.transform.rotation;
Quaternion corretionQuat = new Quaternion(0.0f, 0.0f, - currentOrientationQuat.z, currentOrientationQuat.w);
Camera.main.transform.rotation *= corretionQuat;

如果有人能帮助我解决这个问题,我将不胜感激。谢谢。

对不起英语,不是我的主要语言。

Unity C# 四元数旋转校正方向

使用另一种方法解决了这个问题

float angle = (mouseX_Current - mouseX_ActionStart) * 0.25f;
Camera.main.transform.Rotate(0.0f, 0.0f, angle, Space.World);
angle = (mouseY_Current - mouseY_ActionStart) * 0.25f;
Camera.main.transform.Rotate(angle, 0.0f, 0.0f, Space.Self);
看到做起来

这么容易后,感觉很傻。甚至不需要这个的四元数。