I would like to achieve the zooming in of a camera like aim down a sniper scope over time. Here is my current script.
class FieldsOfView {
var narrow : float;
var wide : float;
}
var fieldsOfView : FieldsOfView;
var delay : float;
function Update () {
if (Input.GetKeyDown("f")) {
StartCoroutine(ChangeFieldOfView());
enabled = false;
}
}
function ChangeFieldOfView () {
yield WaitForSeconds(delay);
camera.fieldOfView = camera.fieldOfView == fieldsOfView.narrow ? fieldsOfView.wide : fieldsOfView.narrow;
enabled = true;
}
4 Answers
4
It's easier to animate your sniper using Unity's Animation Timeline
Shawn
You probably want to have a shot with the Lerp function:
http://unity3d.com/support/documentation/ScriptReference/Mathf.Lerp.html
I've used this recently to control the field of view to get a similar effect.
EDIT
For example you could try:
camera.fieldOfView = (Mathf.Lerp(fieldsOfView.narrow, fieldsOfView.wide, Time.time), 0, 0);
Try this...
var fZoomDuration: float = 1.0; // This determines how fast the zoom will be. Lower values mean faster animation.
var fOneOverDurationToZoom: float = 1.0 / fZoomDuration;
var fStartValue: float = camera.fieldOfView;
var fEndValue: float = camera.fieldOfView == fieldsOfView.narrow ? fieldsOfView.wide : fieldsOfView.narrow;
var fTime: float = 0.0;
var fLerp: float = 0.0;
while (fLerp <= 1f)
{
fLerp = fTime * fOneOverDurationToZoom;
camera.fieldOfView = Mathf.Lerp(fStartValue, fEndValue, fLerp);
fTime += Time.deltaTime;
yield;
}
Just watch this tutorial!
Hope it works… @DevonJS
How to animate
– raptorkwokfieldOfViewin Animation timeline ? Details please