Scale Gui .box over seconds

Hey there,

I’m trying to make a gui.box scale over “X” seconds (let’s say 3 seconds, but it’s going to vary).

Does anyone have any suggestions about how I’d go about doing this? It’s really doing my head in.

Essentially it’s a casting bar, I want the bar to always be a certain amount long (250 pixels), then scale down to 0 pixels over the ‘cast time’, which will just be an intiger.

Does anyone have any suggestions?

Unity - Scripting API: Mathf.Lerp ?

gah i don’t know why I didn’t think of it, thats what happens when you stare at it for too long. thanks so much man, got it working perfectly

One trick I’ve learned is that the GUI functions and their Rects work with float values. So say you have a variable that represents progress of some sort. You can Lerp it between 0.0 and 1.0, then in OnGUI you can do this

GUI.DrawTextue(Rect(10, 10, 250 * progress, 30), barFill);

Yeah that’s how I got it to work, here’s the code if it helps anyone:

private var timer : float;
private var percentage : float;

timer += Time.deltaTime;
        percentage = timer / CurrentCastTime;
        barwidth = Mathf.Lerp(250, 5, percentage);

the 250, and 5 are the max and min widths of the bar - barwidth is the width of the bar which you plug into the ongui)
CurrentCastTime is the time you want it to take when lerping between the 250 and 5 (in this case it was 3 seconds)