- Home /
Lerp not working inside Coroutine.
Hello everyone, I'm trying to somehow understand what's going on my code. It's supposed to make a circle increase in size(localScale) after it's created. It has a start and ending size, the lerp is inside a coroutine which starts only ONCE. If I debug the Vector3.Lerp it returns the same (0.7,0.7,0.7) every frame while the IEnumerator coroutine is working. Please if you can point out the error I'd be really glad. The circle gets to the ending size after the coroutine has finished, but there's no transition visible
This snippet of code brings sets the parameters of the function EnlargeCircle
Vector3 initialSize = new Vector3(0.0f,0.0f,0.0f);
Vector3 endingSize = new Vector3(spread*2.0f,spread*2.0f,spread*2.0f);
StartCoroutine(EnlargeCircle(initialSize,endingSize));
Here is the function.That starts only once.
IEnumerator EnlargeCircle(Vector3 initialSize,Vector3 endingSize)
{
float t = 0;
while(t < 5)
{
t += Time.deltaTime;
circle.localScale = Vector3.Lerp (initialSize, endingSize, Time.deltaTime);
yield return null;
}
circle.localScale = endingSize;
}
Answer by tanoshimi · Mar 20, 2015 at 11:35 AM
There are hundreds of questions and answers about how to use Lerp correctly, and it's also explained in the manual: http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html, but here's one more time....:
circle.localScale = Vector3.Lerp (initialSize, endingSize, Time.deltaTime);
This line means:
"Set circle.localScale
to be Time.deltaTime
of the fraction between initialSize
and endingSize
."
Time.deltaTime is the time it took to render the last frame - a value somewhere around 0.02 (if your game runs at 50FPS). So, on every frame of your coroutine, you're just setting the localScale to an arbitrary fluctuating value. You need to increase the third parameter to Lerp from 0 to 1. The easiest way to do this is:
circle.localScale = Vector3.Lerp (initialSize, endingSize, t / 5.0f);
Your answer
Follow this Question
Related Questions
Issue lerping localscale 1 Answer
Why is this coroutine only firing once? 3 Answers
Static Coroutine being called endlessly 2 Answers
LERP gives final value way too early 2 Answers
Howcome this coroutine loops once? 2 Answers