- Home /
Question by
EgoInsanus · Jul 06, 2012 at 12:56 PM ·
javascriptplayerplatformermaincamera
How can I zoom out my Field of View slowly?
I wanted to know how I could make my camera zoom out slowly (with the field of view) once the player hits a 'trigger' box collider that I set up.
function OnTriggerEnter (Collision : Collider)
{
if(Collision.gameObject.tag == "TriggerOut")
Camera.main.fieldOfView = 100;
}
I just need to know how to get to that 100 slowly
Comment
Answer by Tseng · Jul 06, 2012 at 12:59 PM
I've already posted in your forum thread about it, but will repost it here for others who my read it.
You can just use Mathf.Lerp to (linearly-)interpolate a value over time.
Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, 100, 0.1);
You will probably have to run it in a coroutine, when you are setting it from i.e. OnTrigger/OnColission which are only called once per collision or set a variable and use this variable for the second parameter of Lerp in Update() method.
Example coroutine:
function LerpFoV(fov : float) {
// lerping a value in this way may take quite some time to reach the exact target value, so we will just stop lerping when the difference is small enough, i.e 0.05
var dif : float = Mathf.Abs(Camera.main.fieldOfView - fov);
while(dif > 0.05) {
Mathf.Lerp(Camera.main.fieldOfView, fov, 0.1);
// update the difference
dif = Mathf.Abs(Camera.main.fieldOfView - fov);
yield;
}
}
// start the coroutine
StartCoroutine(LerpFoV(100));