Last active
April 11, 2019 15:24
-
-
Save unitycoder/d5b77ddd00e5d09be1b5 to your computer and use it in GitHub Desktop.
DollyZoom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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