首页 > 解决方案 > 沿 Vector3 轴设置旋转?

问题描述

我正在构建一个具有任意“向下”感的控制器,因此我无法使用正常的相机移动方法。

我可以很好地在上/右轴上移动相机,但这会产生不需要的“滚动”。我环顾四周/尝试了很多,但找不到一种在任意“前向”轴上设置此相机旋转而不搞砸其他运动的好方法。

有任何想法吗?

标签: c#unity3dcamerarotationquaternions

解决方案


使用四元数乘法根据相关输入(此处使用鼠标作为示例)围绕任意向上轴(您可以使用Quaternion.AngleAxis找到它)及其局部 x 轴旋转相机:

Vector3 myUp = Vector3.up; // set with arbitrary up
float rotationSpeed = 1f;

// ...

float horizMouseMove = Input.GetAxis("Mouse X") * rotationSpeed;
float vertMouseMove = Input.GetAxis("Mouse Y") * rotationSpeed
transform.rotation = Quaternion.AngleAxis(horizMouseMove, myUp) 
        * transform.rotation 
        * Quaternion.EulerAngles(-vertMouseMove, 0f, 0f);

推荐阅读