Lerping at a constant rate?
I'm using the basic code for Lerping between two points:
while(t <= 1)
{
t += Time.deltaTime * speed;
transform.position = Vector2.Lerp(a, b, t);
yield return null;
}
Now, because this position it needs to move to (b) is being recalculated 10 times a second (used for a 2d AI following the player), the actual speed it is moving at is constantly changing, due to t's incrementation being somewhat relative.
Does anyone have a solution for lerping at a constant speed, regardless of distance?
Thanks - Tom01098
Answer by Downstream · Mar 26, 2016 at 08:25 PM
Why are you using Lerp instead of something that is actually constant when changing the start and end position? For example monster.position += (player.position - monster.position).normalized * speed * Time.deltaTime
should work.
(Lerp cannot be constant unless you clamp it or something, which is just complicating things for no reason. If you start at 0 and your end point is at 1 then 1/10th of the way there will be 0.1, if your end point is 2 then it's 0.2.)
Aha! That's actually a really nice piece, thanks for clarifying!
Answer by meat5000 · Mar 26, 2016 at 08:23 PM
Why increment t at all? If you are moving your goalposts your t simply determines your step size.