Moving smoothly between two cells


Consider you have a world composed of cells. Your character has to move between one cell to another, just with vertical and horizontal movements, not diagonal ones, and that you can’t stay in the middle of a cell - so every time you start a movement, you should arrive to that cell.

To accomplish this I thought about Vector2.Lerp, doing like this:

_movementLerpAmount = MathHelper.Clamp(
    _movementLerpAmount + Speed * (float)elapsed.TotalSeconds,

CharacterPosition = Vector2.Lerp(_startingLocation, _nextLocation, _movementLerpAmount);

Where _startingLocation is the starting cell, and _nextLocation is the cell in which I want to go.

It is working perfectly, but when I want to keep going in a direction (instead of stopping in a cell), I see a little movement “click” or “stop” everytime the character reaches a cell. I debugged and I saw the that _movementLerpAmount is constant until I reach a cell, but at a certain point it isn’t. I think that is the problem:

0 // Starting from a cell
1 // Arrived

Do you have any suggestions?

if the numbers at the end is the position/lerp per frame, your character is on the same cell for 2 frames, because 1 of the old is equal to 0 of the new cell - your char basically is 2 frames on the same cell.

I can’t tell for sure, why 1 is on a different frame then the following 0 because it should be on the same frame

1 Like

Think of it as conservation of velocity.

Looking at your own numbers,

0.0 - > 0.3 Step of 0.3
0.3 -> 0.6 Step of 0.3
0.6 -> 0,9 Step of 0.3
0.9 -> 1.0 Step of 0.1 ******* ouch

Change the step rate to 0.25 and it will be smooth

1 Like

Thanks a lot! :smiley: