Search Unity

Question Smooth zoom for 2D game

Discussion in 'Cinemachine' started by PaperMouseGames, Oct 15, 2020.

  1. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    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:

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         if (active)
    4.         {
    5.             float horizontalInput = Input.GetAxisRaw("Horizontal");
    6.             float verticalInput = Input.GetAxisRaw("Vertical");
    7.  
    8.             direction.x = horizontalInput;
    9.             direction.y = verticalInput;
    10.             direction = direction.normalized;
    11.          
    12.             velocity = direction * camSpeed * 15f;
    13.             movement = velocity * Time.deltaTime;
    14.  
    15.             mapCam.transform.position = new Vector3(mapCam.transform.position.x + movement.x, mapCam.transform.position.y + movement.y, mapCam.transform.position.z);
    16.  
    17.             float zoom = Input.mouseScrollDelta.y * zoomMagnitude;
    18.             mapCam.m_Lens.OrthographicSize = Mathf.Lerp(mapCam.m_Lens.OrthographicSize, mapCam.m_Lens.OrthographicSize + zoom, zoomSpeed * Time.deltaTime);
    19.         }
    20.     }
    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
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    This zooms in nicely:
    Code (CSharp):
    1. private void Update()
    2. {
    3.         float zoom = Input.mouseScrollDelta.y;
    4.         vcam.m_Lens.OrthographicSize = Mathf.Lerp(vcam.m_Lens.OrthographicSize, vcam.m_Lens.OrthographicSize + zoom, Time.deltaTime);
    5. }
    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.
     
    Last edited: Oct 15, 2020
  3. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    @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:

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         if (active)
    4.         {
    5.             float zoom = -Input.mouseScrollDelta.y;
    6.             mapCam.m_Lens.OrthographicSize = Mathf.Lerp(mapCam.m_Lens.OrthographicSize, mapCam.m_Lens.OrthographicSize + zoom * zoomMagnitude, Time.deltaTime);
    7.             mapCam.m_Lens.OrthographicSize = Mathf.Clamp(mapCam.m_Lens.OrthographicSize, minZoom, maxZoom);
    8.         }
    9.     }
    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?
     
  4. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    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?