Search Unity

A small correction regarding dolly zoom

Discussion in 'Documentation' started by Ippokratis, Mar 26, 2017.

  1. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Hi,
    A minor correction in page https://docs.unity3d.com/Manual/DollyZoom.html for 5.5.

    The C# example has no header, the unityscript example has a wrong header "C# script example", while under the unityscript example there is an orphan "JS script example" header.

    Kind regards.
    - Ippokratis
     
    Last edited: Mar 26, 2017
  2. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Well,
    The c# script cannot compile as is since it throws some errors and warnings.
    I attach here a corrected version, that is verified to work as expected, you can use it if you wish.
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class MoveCameraAlongView : MonoBehaviour
    7. {        
    8.     public Transform targetObject;
    9.     public Camera targetCamera;
    10.  
    11.     private float initHeightAtDist;
    12.     private bool dzEnabled;
    13.  
    14.     // Calculate the frustum height at a given distance from the camera.
    15.     float FrustumHeightAtDistance(float distance)
    16.     {
    17.         return 2.0f * distance * Mathf.Tan(targetCamera.fieldOfView * 0.5f * Mathf.Deg2Rad);
    18.     }
    19.  
    20.     // Calculate the FOV needed to get a given frustum height at a given distance.
    21.     float FOVForHeightAndDistance(float height, float distance)
    22.     {
    23.         return 2.0f * Mathf.Atan(height * 0.5f / distance) * Mathf.Rad2Deg;
    24.     }
    25.  
    26.     // Start the dolly zoom effect.
    27.     void StartDZ()
    28.     {
    29.         var distance = Vector3.Distance(transform.position, targetObject.position);
    30.         initHeightAtDist = FrustumHeightAtDistance(distance);
    31.         dzEnabled = true;
    32.     }
    33.  
    34.     // Turn dolly zoom off.
    35.     void StopDZ()
    36.     {
    37.         dzEnabled = false;
    38.     }
    39.  
    40.     void Start()
    41.     {
    42.         StartDZ();
    43.     }
    44.  
    45.     void Update()
    46.     {
    47.         if (dzEnabled)
    48.         {
    49.             // Measure the new distance and readjust the FOV accordingly.
    50.             var currDistance = Vector3.Distance(transform.position, targetObject.position);
    51.             targetCamera.fieldOfView = FOVForHeightAndDistance(initHeightAtDist, currDistance);
    52.         }
    53.  
    54.         // Simple control to allow the camera to be moved in and out using the up/down arrows.
    55.         transform.Translate(Input.GetAxis("Vertical") * Vector3.forward * Time.deltaTime * 5f);
    56.     }
    57.    
    58. }
    59.  
    60.  
     
    Cosmono likes this.
  3. telferman

    telferman

    Joined:
    Jan 25, 2016
    Posts:
    35
    this works nicely, but when i use the arrow keys to dolly in and out it's jumpy, not smooth. any ideas?
     
  4. Cosmono

    Cosmono

    Joined:
    Nov 6, 2018
    Posts:
    2
    omg, kinda dig this issue out of the tomb. It seems still to remain as a prob on the page though. Thank you for this fix.
     
    Ippokratis likes this.
  5. roccoasselin

    roccoasselin

    Joined:
    Feb 8, 2021
    Posts:
    1
    Still relevant in 2021!