I’m trying to make a function that will allow my players to power up their weapons. I cannot figure out how to limit it. I can have it happen all at once, but I want it to build up a bit, but when I put that in, the bar empties almost immediately.
Here is the function that does the power up.
function Power (power : float)
{
t += .1 * Time.deltaTime;
currentPower = Mathf.Lerp(player.timeDamage, player.timeDamage + maxPower, t);
player.timeDamage += currentPower;
}
Your lerp function is written incorrectly!
When you write a lerp function, the first parameter is the beginning value, the second is the end value, and the third is where you are at between these two. Your first and second parameters seem to be fitting, but your t is wrong.
For Lerping, don’t think of your last parameter as time as much as percentage. For instance, if your t is 0.5, then currentPower = halfway between player.timeDamage and ( player.timeDamage + maxPower ). If your t is 1, then currentPower = player.timeDanage + maxPower. Get it?
Therefore, a lerp function really works best when you write it like so:
var lerp = 0.0;
var track = 0.0;
var value : float;
function lerpPos( start : float, end : float, time : float){
if(track < time){
track += Time.deltaTime;
lerp = track/time;
value = Mathf.Lerp(start, end, lerp);
}
}
That is how you would write it if you were calling the function in Update. If you want to call the function just once via StartCoroutine, you can write it like so:
function FadeAudio ( start : float, end : float, time : float) {
var i = 0.0;
var step = 1.0/time;
while (i <= 1.0) {
i += step * Time.deltaTime;
audio.volume = Mathf.Lerp(start, end, i);
yield;
}
}