- Home /
Change field of view over time
I'm trying to smoothly change the camera field of view when my level loads over a 3 second period of time. The camera starts at a 150 field of view and I want it to stop at 55. Along with this I want a countdown to show on the screen like "3", "2", "1" and then the level starts. Does that make sense? I've tried using the Mathf.Lerp()
method, but I can't get it to work over a set number of seconds. Also what would be the best way to update the countdown timer along with this? If someone could point me in the right direction, that would be great!
just use the animator built-in to unity. you can animate any value.
Answer by Benproductions1 · Mar 18, 2013 at 03:29 AM
I'll do a bit better than just point you in the right direction :)
if you want a proper countdown, there are really only 2 proper ways of doing it: saving the point you start counting, or saving the point you will stop counting. I prefer the latter!
//I presume your using Javascript
var timer:float; //the variable that will be when we stop counting
private var countdown:string = "3"; //the countdown on screen
private var tmp:float; //just a temporary float
function Awake() {
//first we start the timer
timer = Time.time + 3; //we set it to 3 seconds in the future
}
function Update() {
//lets calculate how long till we start
tmp = timer - Time.time;
//sets set the countdown string
if (tmp < 1) {
countdown = "1";
}
else if (tmp < 2) {
countdown = "2";
}
else {
countdown = "3";
}
//Now lets properly lerp the Camera fieldofview
camera.fieldofview = Mathf.Lerp(150, 55, tmp/3); //I don't know if fieldofview is what it's called... I don't have access to the docs right now :)
}
Hope this helps :) Benproductions1
Thanks for the code, that was it! I was pretty close, and the only thing I should mention was to divide by a float
"3.0f" ins$$anonymous$$d of int
"3" in the Lerp()
method (I'm using C# and this may not apply to JavaScript). Thanks again!
Here's a common way to do a timer like that ...
http://answers.unity3d.com/questions/419308/subtracting-variable-amount-over-time.html
anther very simple way:
countdown = "3"; yield WaitForSeconds(1); countdown = "1"; yield WaitForSeconds(1); countdown = "1"; yield WaitForSeconds(1); countdown = "0";