Unity3D 中的平滑 Y 轴对象旋转

问题描述 投票:0回答:1

我打开了输入系统并在我的项目中重构了我的代码并注意到当我移动鼠标时我的角色在缓慢移动鼠标时“滞后” - 好像相机从一个位置跳到另一个位置而不是平滑移动。

所以我想让它移动相机和角色很流畅,但我有一个问题。

我设法使相机在 X 轴上平滑移动,但我无法使角色在 Y 轴上平滑旋转。

我目前负责旋转相机和角色的代码:

private void MovePlayerCamera()
    {
        // Get Y axis rotation and apply to capsule "player" (doesn't smooth rotation but i need to smooth it)
        float yRotation = playerMouseInput.x * sensitivity;
        transform.Rotate(0f, yRotation, 0f);

        // Apply Mouse Sensitivity & clamp xRotation so camera doesn't flip upside down
        xRotation -= playerMouseInput.y * sensitivity;
        xRotation = Mathf.Clamp(xRotation, -lowerMaxRotation, upperMaxRotation);

        // Setup a camera rotation goal and apply to player (this smoothing code works)
        Quaternion goal = Quaternion.Euler(xRotation, 0f, 0f);
        cameraPivot.transform.localRotation = Quaternion.Slerp(cameraPivot.localRotation, goal, cameraSmoothingStrength * Time.deltaTime);
    }

我已经试过了:

        // Get Y axis rotation and apply to object (with this code player just rotates to right by itself)
        float yRotation = playerMouseInput.x * sensitivity;
        transform.Rotate(0f, Mathf.Lerp(transform.rotation.y, yRotation, cameraSmoothingStrength * Time.deltaTime), 0f);

但这不起作用-角色自己旋转到右侧:(

欢迎任何帮助,并提前致谢。

c# unity3d camera rotation smoothing
1个回答
0
投票

试试这个:

private float targetYRotation;

private void MovePlayerCamera()
{
    // Get Y axis rotation and store it as targetYRotation
    float yRotation = playerMouseInput.x * sensitivity;
    targetYRotation += yRotation;

    // Smoothly interpolate the current rotation to the targetYRotation
    Quaternion currentYRotation = transform.rotation;
    Quaternion targetYRotationQuat = Quaternion.Euler(0f, targetYRotation, 0f);
    transform.rotation = Quaternion.Slerp(currentYRotation, targetYRotationQuat, cameraSmoothingStrength * Time.deltaTime);

    // Apply Mouse Sensitivity & clamp xRotation so camera doesn't flip upside down
    xRotation -= playerMouseInput.y * sensitivity;
    xRotation = Mathf.Clamp(xRotation, -lowerMaxRotation, upperMaxRotation);

    // Setup a camera rotation goal and apply to player (this smoothing code works)
    Quaternion goal = Quaternion.Euler(xRotation, 0f, 0f);
    cameraPivot.transform.localRotation = Quaternion.Slerp(cameraPivot.localRotation, goal, cameraSmoothingStrength * Time.deltaTime);
}
© www.soinside.com 2019 - 2024. All rights reserved.