Search Unity

2D Orthographic Smooth Zoom

Discussion in '2D' started by SMGreer, Mar 19, 2015.

  1. SMGreer

    SMGreer

    Joined:
    Dec 13, 2014
    Posts:
    14
    Hey, so I've implemented a basic zoom function so that when the player and enemy objects are close/far it zooms in/out.

    The problem now is that I'm looking to implement a smooth transitional zoom rather than an instantaneous change.

    Here's my code:

    Code (CSharp):
    1. void Update()
    2.     {
    3.  
    4.         playerPosition = GameObject.FindGameObjectWithTag ("Player").transform;
    5.  
    6.         enemyPosition = GameObject.FindGameObjectWithTag ("Enemy").transform;
    7.  
    8.         if (playerPosition.position.x + 8 > enemyPosition.position.x)
    9.         {
    10.             cam.orthographicSize = 5;
    11.         }
    12.  
    13.         if (playerPosition.position.x + 8 < enemyPosition.position.x)
    14.         {
    15.             cam.orthographicSize = 10;
    16.         }
    17.     }

    How would I go about making it a smooth change in zoom instead of an instant one? Any help would be greatly appreciated.
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
  3. SMGreer

    SMGreer

    Joined:
    Dec 13, 2014
    Posts:
    14
    Cheers, that is exactly what I needed to know. Thanks for your help!
     
  4. DuffyT

    DuffyT

    Joined:
    Dec 16, 2014
    Posts:
    37
    You could also use Mathf.smoothstep with a bit of tweaking. For anyone who can't use lerp for whatever reason.