Smooth movement between waypoints 2D

Hi, I’m trying to achieve smooth movement between Vector2 waypoints (in 2D) without either jumping ahead or “lagging” behind because for 2 frames you can move to the same spot while the waypoints are being swapped. I’ve been struggling with the logic required for it for the past 2 days now.


This is my code:

        t += Time.deltaTime * speed;
        float nextFrame;
        Vector2 nextFramePosition;

        if (t < 1f)
        {
            transform.position = Vector2.Lerp(wayPointList[0], wayPointList[1], t);

            nextFrame = t + Time.deltaTime * speed;
            nextFramePosition = Vector2.Lerp(wayPointList[0], wayPointList[1], nextFrame);

            if (nextFramePosition == wayPointList[1])
            {
                t = 0;
                transform.position = Vector2.Lerp(wayPointList[0], wayPointList[1], nextFrame);
                endOfWaypoint = true;
            }

        }

In the part where I “predict” the position in the next frame I can either set that position early (which causes a jump ahead to the waypoint) or If I don’t it will still jump ahead on the next frame because t will be higher than 0 so it has a head start. And if I change the speed to a higher value the “head start” is even higher which is expected but not desirable.


Is there a spesific technique to tackle something like that ? And yes I am aware of the assets that can offer that but I’m trying to write my own implemetation.

Hi @Noshiz

You description doesn’t make Lerping sound like a good choice. If your target can change at any moment, why not just make your movement based on MoveTowards?

See manual for example: