Search Unity

Camera zoom and background image in 2D game

Discussion in '2D' started by alex1st1, Jan 14, 2020.

  1. alex1st1

    alex1st1

    Joined:
    Jan 26, 2019
    Posts:
    185
    Hi All!
    I'm working on Deep space rescue 2D space arcade game and wanted to add camera zoom in the beginning of a scene (look at first 3 seconds of video below to get the idea).
    It works fine with simple coroutine
    Code (CSharp):
    1. public class CameraZoom : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.     void Start()
    5.     {
    6.         StartCoroutine(ZoomIn());
    7.     }
    8.  
    9.     IEnumerator ZoomIn()
    10.     {
    11.         Camera.main.orthographicSize = 100;
    12.  
    13.         while (Camera.main.orthographicSize > 5)
    14.         { yield return new WaitForSeconds(0.01f);
    15.             Camera.main.orthographicSize -= 1f;
    16.         }
    17.     }
    18. }
    19.  
    but background image attached now to main camera as a child gameobject with sprite rendered (dark blue rectangle in video) obviously gets zoomed too.

    How to change it so that background image always looks the same - fully zoomed in to full screen - regardless of Camera.main.orthographicSize?



    Get game on Google play - https://play.google.com/store/apps/details?id=com.Omega486GameStudio.DeepSpaceRescue1
     
  2. alex1st1

    alex1st1

    Joined:
    Jan 26, 2019
    Posts:
    185
    solved by myself - just resize gameobject holding background sprite:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraZoom : MonoBehaviour
    6. {//for zoom in at level start
    7.  
    8.     private GameObject bg;
    9.  
    10.     void Start()
    11.     {//background spriterenderer holder object
    12.         bg = GameObject.Find("BG_Space");
    13.      
    14.         StartCoroutine(ZoomIn());
    15.     }
    16.  
    17.     IEnumerator ZoomIn()
    18.     {
    19.         //zoom out instantly
    20.         Camera.main.orthographicSize = 100;
    21.         bg.transform.localScale = new Vector3(20, 20, 1);
    22.  
    23.         //calc bg zoom step
    24.         float zs = (float)(20-1) / (float)(100-5); //calc: (init spriterenderer transform size - target size) / (init orthographicSize - target orthographicSize)
    25.         Debug.Log("CameraZoom - ZoomIn, zs:" + zs);
    26.      
    27.         //zoom in smoothly
    28.         while (Camera.main.orthographicSize > 5)
    29.         { yield return new WaitForSeconds(0.01f);
    30.             Camera.main.orthographicSize -= 1f;
    31.  
    32.             // Widen the object by x, y, and z values
    33.             bg.transform.localScale -= new Vector3(zs, zs, 0);
    34.           //  Debug.Log("CameraZoom - ZoomIn, bg.transform.localScale:" + bg.transform.localScale);
    35.         }
    36.     }
    37. }
    38.  
     
unityunity