Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active April 11, 2019 15:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save unitycoder/d5b77ddd00e5d09be1b5 to your computer and use it in GitHub Desktop.
Save unitycoder/d5b77ddd00e5d09be1b5 to your computer and use it in GitHub Desktop.
DollyZoom
using UnityEngine;
using System.Collections;
// http://en.wikipedia.org/wiki/Dolly_zoom
public class DollyZoom : MonoBehaviour
{
public Transform target;
Camera cam;
float distance = 0f;
float fov = 60;
float viewWidth = 10f;
void Start()
{
cam = Camera.main;
}
void Update()
{
Vector3 pos = target.transform.position;
fov = cam.fieldOfView;
distance = viewWidth / (2f * Mathf.Tan(0.5f * fov * Mathf.Deg2Rad));
pos.z = -Mathf.Abs(distance);
cam.transform.position = pos;
Debug.Log(distance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment