如何使用c#缩放3D相机

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

所以基本上我写了这段代码:

int zoom = 20;
int normal = 31;
float smooth = 5;

private bool isZoomed = false;
private bool notZoomed = false;

void Update()
{

    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
        isZoomed = !isZoomed;
    }

    if (isZoomed)
    {
        GetComponent<Camera>().fieldOfView = Mathf.Lerp(GetComponent<Camera>().fieldOfView, zoom, Time.deltaTime * smooth);
    }

    if (Input.GetKeyUp(KeyCode.LeftShift))
    {
        notZoomed = !notZoomed;
    }

    if (notZoomed)
    {
        GetComponent<Camera>().fieldOfView = Mathf.Lerp(GetComponent<Camera>().fieldOfView, normal, Time.deltaTime * smooth);
    }
}

这是用于平滑缩放,但效果不佳,充满了错误等。我想做一个类似的“ minecraft Optifine Zoom”(基本上,当我按住Shift时,将其缩放,如果我实现平移,它会回到正常的相机视野)请有人可以修复我的代码或给我另一个代码吗?请我真的需要它。谢谢<3

c# unity3d 3d camera zoom
1个回答
0
投票

首先,您只需要为if语句使用单个bool值,然后就可以对单个布尔值使用if语句。当前在按下和释放时切换两个不同的布尔值,但不能同时切换。

您的另一个问题是使用l​​erp,特别是最后一个参数。从本质上讲,您将永远无法达到目标缩放视域,因为您的计算永远不会导致1。

为了顺利过渡,您应该存储浮点数以用作lerp值

float lerp = 0f;
float initialFOV;
Start() {
    initialFOV = GetComponent<Camera>().fieldOfView;
}
void Update()
{

if (Input.GetKeyDown(KeyCode.LeftShift))
{
    isZoomed = !isZoomed;
}

if (isZoomed)
{
    lerp = Mathf.Max( Time.deltaTime * smooth + lerp, 1f);
    GetComponent<Camera>().fieldOfView = Mathf.Lerp(
} else {
    lerp = Mathf.Min( Time.deltaTime * smooth - lerp 1f);
}

if (Input.GetKeyUp(KeyCode.LeftShift))
{
    isZoomed = !isZoomed;
}

}

© www.soinside.com 2019 - 2024. All rights reserved.