c# - Unity 中相机平滑变焦的问题

标签 c# unity-game-engine camera zooming

每当我使用鼠标滚轮时,我都试图让我的相机平滑地放大和缩小,但由于某种原因它会立即缩放并且不平滑。

这就是我更新缩放的方式:

[SerializeField, Range(10, 100)] float scrollSpeed = 10f;
[SerializeField] Vector2 zoomAmount = Vector2.zero;

private float ScrollWheel
{
    get { return Input.GetAxis("Mouse ScrollWheel"); }
}

private new Camera camera = null;

void Update()
{
    if (camera == null) return;

    UpdateZoom();
}

void UpdateZoom()
{
    pos = camera.transform.position;

    pos = Vector3.Lerp(pos, new Vector3(pos.x, pos.y - scrollSpeed * 10 * ScrollWheel, pos.z), Time.deltaTime * 2);
    pos.y = Mathf.Clamp(pos.y, zoomAmount.x, zoomAmount.y);

    camera.transform.position = pos;
}

最佳答案

我认为您对 Lerp 有点困惑(当我开始使用它时,我也以同样的方式感到困惑)。当您传入 0 作为时间参数(第三个参数)时,它将返回您的“起始”向量。当您为时间参数传递 1 或更大的值时,它将返回您的“结束”向量。 但是,如果您传递 Time.deltaTime * 2,那么每帧都会返回大致相同的插值向量。 Lerp 不会“跟踪”它已经插值的距离,因此通过每帧传递相同的时间值,Lerp 将永远不会真正返回结束向量。因此,您不需要传递 Time.deltaTime * 2,而是需要执行类似的操作

float interpolatedTime = 0;
void Update()
{
   var myVector = Vector3.Lerp(vector1, vector2, this.interpolatedTime);
   this.interpolatedTime += Time.deltaTime; 
}

为你的相机做这样的事情怎么样:

float zoomTime;
float zoomTarget;
float lastScrollWheelDirection;

void Update()
{
    // If this camera is currently zooming in and the player started zooming
    // out (or vice versa), reset the amount that is remaining to be zoomed
    if ((this.lastScrollWheelDirection > 0 && this.ScrollWheel < 0) ||
        (this.lastScrollWheelDirection < 0 && this.ScrollWheel > 0))
    {
        this.zoomTarget = 0;
    }
    if (this.ScrollWheel != 0)
    {
        this.lastScrollWheelDirection = this.ScrollWheel;
    }

    // zoomTarget is the total distance that is remaining to be zoomed.
    // Each frame that the scroll wheel is moved, we'll add a little more
    // to the distance that we want to zoom
    zoomTarget += this.ScrollWheel * this.scrollSpeed;

    // zoomTime is used to do linear interpolation to create a smooth zoom.
    // Each time the player moves the mouse wheel, we reset zoomTime so that 
    // we restart our linear interpolation
    if (this.ScrollWheel != 0)
    {
        this.zoomTime = 0;
    }

    if (this.zoomTarget != 0)
    {
        this.zoomTime += Time.deltaTime;

        // Calculate how much our camera will be moved this frame using linear
        // interpolation.  You can adjust how fast the camera zooms by
        // changing the divisor for zoomTime
        var translation = Vector3.Lerp(
            new Vector3(0, 0, 0),
            new Vector3(0, this.zoomTarget, 0),
            zoomTime / 4f);   // see comment above

        // Zoom the camera by the amount that we calculated for this frame
        this.transform.position -= translation;

        // Decrease the amount that's remaining to be zoomed by the amount 
        // that we zoomed this frame
        this.zoomTarget -= translation.y;
    }
}

关于c# - Unity 中相机平滑变焦的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55229906/

相关文章:

c# - 如何在 C# 中格式化美分和美元?如何将一个转换为另一个?

c# - 使用内部数据结构作为内存数据库是一种好方法吗?

c# - 当玩家触摸边缘时,2D 游戏平台相机会升起

ios - 如何知道iPhone或iPad相机的视角

java - 媒体扫描仪正在扫描,但不刷新 (Android)

c# - 通过OpenCV的camshift算法控制鼠标指针(或者鼠标的基本功能)

c# - 查找所有交叉数据,而不仅仅是唯一值

python - Unity3d - 在 Google App Engine 上托管

c# - 如何将unity 3d场景导出到Android Studio

android - 使用 Android Camera 进行图像处理