Search Unity

how can i increase or decrease size of camera with control speed.

Discussion in '2D' started by sharpg, Mar 27, 2015.

  1. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    hello guys,
    i am working on 2D game using orthographic camera.I am using the following code to decrease the size of camera.my object got zoom in perfectly,but i want to control the zoom in speed a bit slow instead of sudden.
    please tell me how can i do that.
    Code (csharp):
    1.  
    2. camera.orthographicSize=4.6;
    3.  
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Here's a quick way of doing a soft camera zoom:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ZoomIn : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start ()
    8.     {
    9.         StartCoroutine(ZoomCamera(20, 5, 5, 100));
    10.     }
    11.  
    12.     IEnumerator ZoomCamera(float from, float to, float time, float steps)
    13.     {
    14.         float f = 0;
    15.  
    16.         while (f <= 1)
    17.         {
    18.             Camera.main.orthographicSize = Mathf.Lerp(from, to, f);
    19.  
    20.             f += 1f/steps;
    21.  
    22.             yield return new WaitForSeconds(time/steps);
    23.         }
    24.     }
    25. }
     
    theANMATOR2b and sharpg like this.
  3. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    Thanks Sir :)