Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Zoom in .. fine... Zoom out = BORKED

Discussion in '2D' started by Zace666, Apr 22, 2015.

  1. Zace666

    Zace666

    Joined:
    Jan 28, 2014
    Posts:
    21
    Hi there, I hope someone can help. Im trying to create a little script that zooms in to my player and back out - toggling.
    The zoom in works fine, but when I try to zoom back out it doesn't work, it gets stuck. Ive created a bool to ensure it only runs the code when it needs to and I'm wondering if that is what's causing the error.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraZoom : MonoBehaviour
    5. {
    6.  
    7.     public float zoom = 10f;
    8.     public float normal = 3.471398f;
    9.     public float smooth = 5f;
    10.     private bool isZoomed = false;
    11.  
    12.     public Camera cam;
    13.     public GameObject player;
    14.  
    15.     // lock the camera settings
    16.     public float LockedX = 0f;
    17.     public float LockedY = 0f;
    18.     public float LockedZ = 0f;
    19.     private bool hasBeenZoomed =  false;
    20.  
    21.     Vector3 targetPos;
    22.  
    23.     private Transform playerTransform;
    24.  
    25.     // Use this for initialization
    26.     void Start()
    27.     {
    28.         targetPos = transform.position;
    29.         playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.         if (Input.GetKeyDown("z")) { isZoomed = !isZoomed; }
    36.         if (isZoomed == true)
    37.         {
    38.             ZoomInToPlayer();
    39.             hasBeenZoomed = true;
    40.         }
    41.         else
    42.         {
    43.             if (hasBeenZoomed)
    44.             {
    45.                 ZoomOutFromPlayer();
    46.                 hasBeenZoomed = false;
    47.             }
    48.         }
    49.     }
    50.  
    51.     void ZoomInToPlayer()
    52.     {
    53.         // By default the target x and y coordinates of the camera are it's current x and y coordinates.
    54.         float targetX = transform.position.x;
    55.         float targetY = transform.position.y;
    56.  
    57.         // ... the target x coordinate should be a Lerp between the camera's current x position and the player's current x position.
    58.         targetX = Mathf.Lerp(transform.position.x, playerTransform.position.x, smooth * Time.deltaTime);
    59.         //Debug.Log("player x is " + playerTransform.position.x + " and TargetX is " + targetX);
    60.  
    61.         // ... the target y coordinate should be a Lerp between the camera's current y position and the player's current y position.
    62.         targetY = Mathf.Lerp(transform.position.y, playerTransform.position.y, smooth * Time.deltaTime);
    63.         //Debug.Log("player y is " + playerTransform.position.y+ " and TargetY is " + targetY);
    64.  
    65.  
    66.         // Set the camera's position to the target position with the same z component.
    67.         cam.transform.position = new Vector3(targetX, targetY, transform.position.z);
    68.  
    69.         // Change the size of the camera viewport
    70.         cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, zoom, Time.deltaTime * smooth);
    71.     }
    72.  
    73.     void ZoomOutFromPlayer()
    74.     {
    75.         // By default the target x and y coordinates of the camera are it's current x and y coordinates.
    76.         float targetX;
    77.         float targetY;
    78.  
    79.         // Change the size of the camera viewport
    80.         cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, normal, Time.deltaTime * smooth);
    81.  
    82.         // ... the target x coordinate should be a Lerp between the camera's current x position and the original x position.
    83.         targetX = Mathf.Lerp(transform.position.x, LockedX, smooth * Time.deltaTime);
    84.         // ... the target y coordinate should be a Lerp between the camera's current y position and the original y position.
    85.         targetY = Mathf.Lerp(transform.position.y, LockedY, smooth * Time.deltaTime);
    86.         // Set the camera's position to the target position with the same z component.
    87.         cam.transform.position = new Vector3(targetX, targetY, transform.position.z);
    88.  
    89.     }
    90. }
     
  2. Zace666

    Zace666

    Joined:
    Jan 28, 2014
    Posts:
    21
    Luckily COAC over at stackoverflow solved this for me. Sharing it in case someone else has the same sort of problem.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraZoom : MonoBehaviour
    5. {
    6.  
    7. public float zoom = 10f;
    8. public float normal = 3.471398f;
    9. public float smooth = 5f;
    10. private bool isZoomed = false;
    11. private bool isZoomFinished = true; // the animation zoom is over ?
    12.  
    13. public Camera cam;
    14. public GameObject player;
    15.  
    16. public float LockedX = 0f;
    17. public float LockedY = 0f;
    18. public float LockedZ = 0f;
    19. private bool hasBeenZoomed =  false;
    20.  
    21. Vector3 targetPos;
    22.  
    23. private Transform playerTransform;
    24.  
    25. void Start()
    26. {
    27.     targetPos = transform.position;
    28.     playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
    29. }
    30.  
    31. void Update()
    32. {
    33.     if (Input.GetKeyDown("z") && isZoomFinished) {
    34.         isZoomed = !isZoomed;
    35.         isZoomFinished = false;
    36.     }
    37.  
    38.     if (isZoomed && !isZoomFinished)
    39.     {
    40.         ZoomInToPlayer();
    41.  
    42.     }
    43.     else if (!isZoomed && !isZoomFinished)
    44.     {
    45.        ZoomOutFromPlayer()
    46.     }
    47. }
    48.  
    49. float delta = 0;
    50.  
    51. void ZoomInToPlayer()
    52. {
    53.     delta += smooth * Time.deltaTime;
    54.  
    55.  
    56.     //Cam size
    57.     cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, zoom, delta);
    58.  
    59.     //Cam pos
    60.     float targetX = transform.position.x;
    61.     float targetY = transform.position.y;
    62.     targetX = Mathf.Lerp(transform.position.x, playerTransform.position.x, delta);
    63.     targetY = Mathf.Lerp(transform.position.y, playerTransform.position.y, delta);
    64.     cam.transform.position = new Vector3(targetX, targetY, transform.position.z);
    65.  
    66.  
    67.  
    68.     // is animation over ?
    69.     if(delta >= 1) {
    70.         isZoomFinished = true;
    71.         delta = 0;
    72.     }
    73. }
    74.  
    75. void ZoomOutFromPlayer()
    76. {
    77.  
    78.     delta += smooth * Time.deltaTime;
    79.  
    80.     //Cam size
    81.     cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, normal, delta);
    82.  
    83.     //Cam pos
    84.     float targetX;
    85.     float targetY;
    86.     targetX = Mathf.Lerp(transform.position.x, LockedX, delta);
    87.     targetY = Mathf.Lerp(transform.position.y, LockedY, delta);
    88.     cam.transform.position = new Vector3(targetX, targetY, transform.position.z);
    89.  
    90.     // is animation over ?
    91.     if(delta >= 1) {
    92.         isZoomFinished = true;
    93.         delta = 0;
    94.     }
    95.  
    96. }
    97. }
     
  3. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Uhh, care to elaborate on what the fix was?
     
  4. Zace666

    Zace666

    Joined:
    Jan 28, 2014
    Posts:
    21
    Sure, the code above....
    basically attach this code to the camera, ensure that all delta time are the same, so that all zooms/moves work together. It also runs the animation of the move until the movement is finished.