分开平滑放大和缩小

时间:2019-07-13 01:29:08

标签: c# unity3d

我只在一起工作了几天,就遇到了以下问题,我不知道如何使放大和缩小变得平滑,而其余部分也无法平滑运行。不过,我想保留我可以决定其余部分是否平滑的功能。如前所述,主要问题是我不知道如何平滑放大和仅缩小。当我使用当前代码启动项目时,感觉鼠标的dpi很高。而且,每个动作都很流畅,我也不想要。我很乐意提供帮助。

Vace:)

if (thirdperson)
        {
            yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
            pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
            pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);

            // out
            if (Input.GetAxis("Mouse ScrollWheel") < 0)
            {
                if (dstFromTarget < 12)
                {
                    dstFromTarget++;
                }
            }

            //in
            if (Input.GetAxis("Mouse ScrollWheel") > 0)
            {
                if (dstFromTarget > 2)
                {
                    dstFromTarget--;
                }
            }

            currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
            transform.eulerAngles = currentRotation;

            Vector3 test = Vector3.SmoothDamp(transform.position, target.position - transform.forward * dstFromTarget, ref zoomSmoothVelocity, zoomSmoothTime);

            transform.position = new Vector3(test.x, test.y, test.z);
        }

1 个答案:

答案 0 :(得分:1)

使用Vector3.SmoothDamp的浮动版本:Mathf.SmoothDamp

使用它找到dstFromTarget的平滑目标。将zoomSmoothVelocity更改为浮点型,然后将当前的缩放变量初始化为与初始化dstFromTarget相同的值。

然后,您只需将Vector3.SmoothDamp的目标直接分配到相机的位置即可。

使用transform.eulerAngles = new Vector3(pitch.yaw);绕过您对Vector3.SmoothDamp的首次呼叫

我还建议您使用Mathf.Clamp将缩放比例保持在有效范围内。

总的来说,看起来像这样:

private float zoomSmoothVelocity = 0f;
private float curZoom = 6f; // however dstFromTarget is initialized 

// ...

if (thirdperson)
{
    yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
    pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
    pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);

    // out
    if (Input.GetAxis("Mouse ScrollWheel") < 0)
    {
        dstFromTarget++;
    }

    //in
    if (Input.GetAxis("Mouse ScrollWheel") > 0)
    {
        dstFromTarget--;
    }

    dstFromTarget = Mathf.Clamp(dstFromTarget, 2f, 12f);

    transform.eulerAngles = new Vector3(pitch, yaw);

    curZoom =  Mathf.SmoothDamp(curZoom , dstFromTarget, ref zoomVelocity , zoomSmoothTime);

    transform.position = target.position - transform.forward * curZoom;
}