相机全局旋转夹紧问题Unity3D

时间:2018-06-13 12:46:41

标签: c# unity3d rotation global clamp

我有一个问题,我制作了一个流畅的相机移动脚本,我无法弄清楚如何正确地夹住相机旋转。

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

当按下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。 / p>

但如果我从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后......但这只会让人觉得奇怪。例如,当旋转时达到最小钳位值时,钳位返回最大钳位值。

0 个答案:

没有答案