我有一个问题,我正在制作一个平滑的相机移动脚本,我无法弄清楚如何正确夹紧相机旋转 .

实际上,相机是玩家对象的孩子 .

按下WASD时,播放器会旋转,但由于脚本,相机不会跟随此旋转 .

var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));

            mouseDelta = Vector2.Scale(mouseDelta, CursorSensitivity); //new Vector2(CursorSensitivity.x * smoothing.x, CursorSensitivity.y * smoothing.y));

            if (m_doSmooth)
            {
                _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
                _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);

                // Find the absolute mouse movement value from point zero.
                _mouseAbsolute += _smoothMouse;
            }
            else
                _mouseAbsolute += mouseDelta;

            if (clampInDegrees.x > 0)
                _mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x, clampInDegrees.x);

            if (clampInDegrees.y > 0)
                _mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y, clampInDegrees.y);

            Camera.transform.rotation = Quaternion.AngleAxis(-_mouseAbsolute.x, Vector3.up) //transform.InverseTransformDirection(Vector3.up))
                * Quaternion.AngleAxis(_mouseAbsolute.y, Vector3.right);

嗯,问题很简单,夹紧的旋转值是全局的,因为我们设置 Camera.transform.rotation 而不是 Camera.transform.localRotation .

这种情况发生了:Link to video

该脚本根据玩家轮换来限制值 .

因此,如果玩家在X轴上的旋转(eulerAngle)为90度,并且钳位值为90到-90,则夹紧旋转将为0,180,而不是-90,90 .

但如果我从 rotation 更改为 localRotation ,则会发生这种情况:Link to video

我也尝试了以下内容:

Vector3 euler = Camera.transform.eulerAngles;

            if (clampInDegrees.x > 0)
                euler.x = ClampAngle(euler.x, -clampInDegrees.x, clampInDegrees.x);

            if (clampInDegrees.x > 0)
                euler.y = ClampAngle(euler.y, -clampInDegrees.y, clampInDegrees.y);

            Camera.transform.eulerAngles = euler;

设置 localRotation 之后......但这只会让人觉得奇怪 . 例如,当旋转时达到最小钳位值时,钳位返回最大钳位值 .