How stop Update when Lerp is finished?
In my update function I execute a LERP to position my button. The problem is, once the button has reached its final position, the Lerp function keeps getting executed every update (30 to 60 times per second). I don't need the update function anymore when the LERP is finished. How do I achieve this?
void Update() {
transform.localPosition = new Vector3(startpos.x, Mathf.Lerp(startpos.y, maximum, Time.time), startpos.y);
}
Answer by Namey5 · Oct 23, 2016 at 05:40 AM
You can't exactly remove the Update function. In your case, all you would need to do is just return once the t value of the Lerp is greater than 0, i.e.
void Update() {
if (Time.time > 1)
return;
transform.localPosition = new Vector3(startpos.x, Mathf.Lerp(startpos.y, maximum, Time.time), startpos.y);
}
Answer by Team_Benanna · Oct 23, 2016 at 06:51 AM
bool lerping = true;
Vector3 endPos;
void Update()
{
if(lerping)
{
MoveButton();
if(transform.position == endPos)
{
lerping = false;
}
}
}
void MoveButton()
{
//Put your lerp here.
}
This is how I've been doing it anyway :)
Your answer
Follow this Question
Related Questions
Lerp not working with UI button 1 Answer
Why Mathf.Lerp dosen t work correct? 1 Answer
How to generate a different number every time? 1 Answer
Unity 5.5 Mac OS laggy animations 0 Answers
unity 5.6 slow running on android 1 Answer