Help with Vector2.Lerp() [SOLVED]

Hi there,

So I’m trying to move a sprite to a specified position using Lerp, however, when I run my game the sprite just moves to one location between the two points and doesn’t constantly move and I can’t figure out why… I’m pretty sure its my ‘amount’ value but I have tried putting in a non-changing value and a value that changes with time.

In my code, the ‘sprite1’ is part of the components list. Here is my code:

public override void Update(GameTime gameTime)

        sprite1.pos = Vector2.Lerp(new Vector2(destionion.pos.X - 40, destionion.pos.Y - 40), gridclass.cord[7,3].nodepos, 0.4f);

        foreach (Components component in gamecomps)

Thanks for any help in advance!

The last parameter is a value from 0 to 1 that acts as a percentage for interpolating. If you put a value of 0.5, it will return a vector halfway between the start and the end (Ex. (0, 0) and (3, 1) will return (1.5, .5)); likewise, 0 will return the start vector and 1 will return the end vector.

You’ll need to update that value over time. Also make sure that your start and end vectors aren’t changing, or the lerp may be off.

1 Like

Hi thanks for your help, just got my sprite moving correctly! Thank you.
However, have one more question…
My sprite now moves beyond the second position and doesn’t stop there, I thought that with lerp the sprite would stop when reaching its destination?
Thanks for all the help

Lerp doesn’t do this by itself; to stop it, you’ll have to clamp the time value or stop calling lerp once the time has exceeded.

Lerp(a, b, t) just does a + (b - a) * t, which is a for t = 0 and b for t = 1. MG’s Lerp doesn’t clamp or check if t is in the [0 1] range. If you don’t want to overshoot you can clamp t yourself using t = MathHelper.Clamp(t, 0, 1).

1 Like

Thank a lot for your help! Much appreciated.