Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Make Camera Zoom Out Based on Player Height

Discussion in 'Scripting' started by denissuu, Feb 19, 2020.

  1. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162
    Hi there! I've been trying for a while to figure out how I could use an ortographic camera in a 2D so when the player goes up in height, the camera zooms out so he can still see things below him.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraFollow : MonoBehaviour
    6. {
    7.     public GameObject player;
    8.     private Vector3 offset;
    9.     public float ZoomOutFactor;
    10.  
    11.     // Update is called once per frame
    12.  
    13.     void Start()
    14.     {
    15.         offset = this.transform.position - player.transform.position;
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         ZoomOutFactor = GetComponent<PlayerControls>().player.position.y - 115f;
    21.     }
    22.     void LateUpdate()
    23.     {
    24.         if (ZoomOutFactor < 1)
    25.         {
    26.             transform.position = player.transform.position + offset;
    27.         }
    28.         else
    29.         {
    30.             transform.position = player.transform.position + (offset *(ZoomOutFactor / 10f));
    31.         }
    32.     }
    33. }
    34.  
    Currently I've tried this and tried playing around with it and stuff, but the first issue is that it always gives a value of 0.


    How could I achieve the effect that I want?

    Thanks for the help! :)
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    For zooming orthographic camera use .size property, orthographics means no perspective, i.e. size of objects is not dependent on it's distance from camera.
     
  3. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    159
    You need to set the Camera orthographicSize and you can zoom out or in with a Lerp, basically each frame it's going to change the zoom just a little until it reaches the zoomTarget you have set according to the player position.
    Something like this:

    Code (CSharp):
    1.  
    2. // Target to achieve.
    3. private float zoomTarget;
    4.  
    5. // Multiplier.
    6. private float zoomLerpTime;
    7.  
    8. // Min value for zoom.
    9. private float MinCameraOrthoSize;
    10.  
    11. // Max value for zoom.
    12. private float MaxCameraOrthoSize;
    13.  
    14. private Update()
    15. {
    16.     UpdateZoomTarget();
    17.     UpdateZoom();
    18. }
    19.  
    20. private void UpdateZoomTarget()
    21. {
    22.     Vector2 playerPos = get the player position.
    23.     zoomTarget = here you set the target according to the player Position.
    24. }
    25.  
    26. private void UpdateZoom()
    27.     {
    28.         if (Camera.main.orthographicSize != zoomTarget)
    29.         {
    30.             float target = Mathf.Lerp(Camera.main.orthographicSize, zoomTarget, zoomLerpTime * Time.deltaTime);
    31.             Camera.main.orthographicSize = Mathf.Clamp(target, MinCameraOrthoSize, MaxCameraOrthoSize);
    32.         }
    33.     }
    34.