Controlling Lerp Movement Speed

Hello, I’ve been trying to create a script for my Tile Based movement game where the Player moves to a selected destination tile when you click on it, and it navigates to it via Dijkstra pathfinding. The problem I have it that instead of moving directly from the start tile to the end tile at a constant speed, it stops at each tile in the path until it reaches the destination. Furthermore, it moves incredibly quickly wheras I want it to move at a speed that I choose but for some reason that is not working. Here is a segment of my script with movement Lerp.

                 float i = 0.0f;
             float rate = 1f / 0.001f;
    
             if (i < 10f) {
                 i += Time.deltaTime * rate;
                     if (Vector3.Distance (transform.position, map.TileCoordToWorldCoord (tileX, tileZ)) < 0.001f)
                     AdvancePathing ();
             transform.position = Vector3.Lerp (transform.position, map.TileCoordToWorldCoord (tileX, tileZ), i);

Any help would be greatly appreciated.

Your movement code does not produce a constant velocity.
This blog post goes into detail about the misconceptions of using linear interpolation among new users, and how to use it properly.

Vector3.MoveTowards is probably more like the behaviour you expect.

2 Likes