Smooth zoom for 2D game

Hi there! I’m making a 2D top down game and I just started using CInemachine for the firs time.

I’m trying to get a smooth zoom effect working and I’m having issues.

So, right now I’m using the following code:

private void Update()
    {
        if (active)
        {
            float horizontalInput = Input.GetAxisRaw("Horizontal");
            float verticalInput = Input.GetAxisRaw("Vertical");

            direction.x = horizontalInput;
            direction.y = verticalInput;
            direction = direction.normalized;
          
            velocity = direction * camSpeed * 15f;
            movement = velocity * Time.deltaTime;

            mapCam.transform.position = new Vector3(mapCam.transform.position.x + movement.x, mapCam.transform.position.y + movement.y, mapCam.transform.position.z);

            float zoom = Input.mouseScrollDelta.y * zoomMagnitude;
            mapCam.m_Lens.OrthographicSize = Mathf.Lerp(mapCam.m_Lens.OrthographicSize, mapCam.m_Lens.OrthographicSize + zoom, zoomSpeed * Time.deltaTime);
        }
    }

The zoom works (though it needs some major work) but it just snaps to the orthographic size and I wanted it to sort of smoothly transition. I have another virtual camera on the scene with a different orthographic size and when going from one camera to the other it zooms smoothly so I’m sure it can be done but I just can’t figure it out.

Not sure

This zooms in nicely:

private void Update()
{
        float zoom = Input.mouseScrollDelta.y;
        vcam.m_Lens.OrthographicSize = Mathf.Lerp(vcam.m_Lens.OrthographicSize, vcam.m_Lens.OrthographicSize + zoom, Time.deltaTime);
}

I don’t know what zoomSpeed is in your code, but it may scale up Time.deltaTime too much. Lerp expect a t value between 0 and 1.

@gaborkb

Hey, thanks for the reply! Sorry I should have specified what the variables are.

zoomMagnitude and zoomSpeed are just floats meant to control the rate at which each scroll on the mouse wheel zooms. I guess I really don’t need both, I changed my code to the following based on your suggestion:

private void Update()
    {
        if (active)
        {
            float zoom = -Input.mouseScrollDelta.y;
            mapCam.m_Lens.OrthographicSize = Mathf.Lerp(mapCam.m_Lens.OrthographicSize, mapCam.m_Lens.OrthographicSize + zoom * zoomMagnitude, Time.deltaTime);
            mapCam.m_Lens.OrthographicSize = Mathf.Clamp(mapCam.m_Lens.OrthographicSize, minZoom, maxZoom);
        }
    }

It’s still not smooth except for very small values of zoomMagnitude. Also when I look at the Orthographic size in the inspector it seems to be changing imminently and I thought Lerp was supposed to change the value over time.

Maybe it can’t be done using this method?

For me, it is smooth (tried zoomMagnitude from 0.1 to 100). Could you send a video recording showing what happens in your case exactly?