Skip to content

Instantly share code, notes, and snippets.

@Zal0
Last active August 7, 2017 18:13
CameraManager - 2
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Camera))]
public class CameraManager : MonoBehaviour {
private Quaternion fromRotation;
private Vector3 fromPosition;
private float fromFov;
private float fromNear;
private float fromFar;
private Camera toCamera;
private float interpolationTime;
private float interpolationTimeAccum;
private Transform target;
private Camera cachedCamera;
void Awake() {
cachedCamera = GetComponent< Camera >();
}
public static void EnableComponents(Camera _cam, bool _value) {
_cam.enabled = _value;
AudioListener toCameraAudioListener = _cam.GetComponent< AudioListener >();
if(toCameraAudioListener != null) {
toCameraAudioListener.enabled = _value;
}
}
public void InterpolateTo(Camera _toCamera, Transform _target, float _time) {
if(_target == null) {
_target = _toCamera.transform;
}
//Update From Camera values
if (toCamera != null) { //Check if the previous target camera is still there to interpolate from it
EnableComponents(toCamera, false);
if(interpolationTimeAccum >= interpolationTime) {
//we don't need to care about this is the interpolation is not finished since this camera if currently being updated
transform.position = toCamera.transform.position;
transform.rotation = toCamera.transform.rotation;
cachedCamera.fieldOfView = toCamera.fieldOfView;
cachedCamera.nearClipPlane = toCamera.nearClipPlane;
cachedCamera.farClipPlane = toCamera.farClipPlane;
}
} else {
//Set interpolation time to 0 if there isn't any Camera to interpolate from
_time = 0.0f;
}
target = _target;
fromRotation = transform.rotation;
fromPosition = -transform.worldToLocalMatrix.MultiplyPoint(target.position); //translation from target to current camera position (in camera local)
fromFov = cachedCamera.fieldOfView;
fromNear = cachedCamera.nearClipPlane;
fromFar = cachedCamera.farClipPlane;
//Update To Camera values
toCamera = _toCamera;
EnableComponents(toCamera, false);
//Activate interpolator
gameObject.SetActive(true);
//Restet time
interpolationTime = _time;
interpolationTimeAccum = 0.0f;
}
void Refresh() {
float t = interpolationTimeAccum / interpolationTime;
//Update camera paramenters
cachedCamera.fieldOfView = Mathf.Lerp(fromFov, toCamera.fieldOfView, t);
cachedCamera.nearClipPlane = Mathf.Lerp(fromNear, toCamera.nearClipPlane, t);
cachedCamera.farClipPlane = Mathf.Lerp(fromFar, toCamera.farClipPlane, t);
//Rotate
transform.rotation = Quaternion.Lerp(fromRotation, toCamera.transform.rotation, t);
//Translate
transform.position = target.position;//Place in target position
Vector3 p0 = fromPosition;
Vector3 p1 = -toCamera.transform.worldToLocalMatrix.MultiplyPoint(target.position);
transform.Translate(Vector3.Lerp(fromPosition, p1, t));
}
void LateUpdate() {
interpolationTimeAccum = Mathf.Clamp(interpolationTimeAccum + Time.deltaTime, 0.0f, interpolationTime);
if(interpolationTimeAccum < interpolationTime) {
Refresh();
} else {
gameObject.SetActive(false);
EnableComponents(toCamera, true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment