- Home /
How would you start another lerp based off of a condition of a current lerp?
I'm trying to monitor the state of a current lerp, and then start another lerp mid lerp of the first lerp!
:P
(sorry just like typing lerp!)
But seriously, If I have a Mathf.Lerp making a number go from 1-0, I'm trying to start another lerp when that number reaches say the mid point (.5). But for some reason, I'm not getting the results I'm after.
Here's my code;
if ( lives == 0 )
{
var t: float = 0.0f;
var on: float = 1.0f;
var off: float = 0.0f;
while(t < 1)
{
t += Time.deltaTime / panelFadeOutTime;
panel.GetComponent(UIPanel).alpha = Mathf.Lerp (on, off, t);
yield;
}
}
if (panel.GetComponent(UIPanel).alpha <= .8)
{
var r: float = 0.0f;
while(r < 1)
{
r += Time.deltaTime / panelFadeOutTime;
panelLose.GetComponent(UIPanel).alpha = Mathf.Lerp (off, on, r);
yield;
}
}
These lerps are controlling an alpha attribute of two different sprites, I'm simply trying to fade one down and fade one up just before the first one fades down.
What's happening with this current code is one will fade down completely, and then one fades up.
thanks for reading!
$$anonymous$$aybe declare r next to t at the start of the script. Don't forget to reset them if you need to.
I'm assu$$anonymous$$g this is inside Update()? Lerp needs to be called constantly to update the value. If this runs through once you won't get the effect you are trying to achieve.
It's inside a function which is inside of Update (). Everything is lerping fine, it's just one lerp finishes before another lerp starts.
Could you type out what you mean by declare r next to t? I'm still new and learning. And code examples of the right way help me learn this easier.
Answer by meat5000 · Oct 07, 2013 at 07:22 PM
I would declare t and r at the start of the script and set them to 0. Increment the timers, then zero them when you want to reset the lerps.
Your Lerps are inside while loops which is why one finishes before the other. A while loop will complete until the condition is false before continuing with the rest of the script. Remove the whiles and use an if to add running conditions instead.
I'm sure what you are saying is right, however if you could just give me an example in code, so I don't get it wrong...that would be great.
Like I had already said, I'm new to coding and learn best by seeing what the right way is rather then someone trying to talk me through it.
It's like asking for directions, I'd understand better with a map drawn then someone trying to explain it verbally.
Your answer
Follow this Question
Related Questions
Sliding Door OnButtonDown 4 Answers
how to use time on lerp 2 Answers
Keeping track of how many times games as been Restarted 0 Answers