How to make character move set distance (tile based)

Hi,
I have been following

(Brackeys Top Down Movement Tutorial), and I was wondering, how would you make your character move tile by tile, so if you tap the “W” key once, it moves one tile, and if you hold down the “W” key, you move smoothly across tiles until you let go, which stops you in the middle of one tile again. Anyone know? I tried searching it up, but no avail.
Thanks!

Hi @Roberto_Sanchez

Search here or google for tile (or grid) based movement, and tile (or grid) smooth movement. You’ll find several discussions.

It is very common technique. You can find it in Unity’s tutorials; See 2D Roguelike tutorial series.

Basically decide the axis based on player input (L/R/U/D) then based on this, get the next closest tile center and move there.

Smooth movement only requires little more work - after getting input, do lerp / smoothdamp with Vector3 to move to target position. When target is reached, allow input again.

1 Like

Determine the length of time it takes to move to the tile. Use IEnumerator to move the char to that tile. maybe MoveLeft takes longer than move right? You would need to customize each direction NOT unlike moving up,down, left, or right. You’re only doing so for a certain allotted amount of time.

ok

So is there no simple way to just make the movement in steps?

@Roberto_Sanchez

Seems like you ignored my suggestion;

So why not watch Unity’s tutorial, where it is explained?

“So is there no simple way to just make the movement in steps?”

And what is a simple way? If you are somewhat familiar with the concepts and unity/C# commands needed you can create this yourself - otherwise someone else will have to give you the code…

Anyway I think I pretty much explained the steps that are needed;

  1. Decide the axis of movement / direction based on player input (left / right / up / down) .
  2. Get the next closest tile center/position based on input (depending on your character pivot).
  3. Set some state variable so you know not to take input while moving (unless you do this by design).
  4. Move to target by using lerp / smoothdamp on your transform.
  5. When target is reached, allow input again.

If you are not familiar with the steps needed to do this, then you probably want to watch how it can be done?

1 Like

I meant like put a timer after the move script to so it moves stops then moves

Hi again @Roberto_Sanchez

I don’t know how that relates to your original question “how would you make your character move tile by tile” - adding some automatic movement to tile based movement is another question.

If you just need a pause after each tile to tile move, add a timer with counter, increment it until some defined time is passed, then allow input again. Or use coroutines instead of running your code in Update.

How would I add a timer? Can you please add some sample code? I’m really new.

private float timer = 3.0f;

private void Update()
{
timer -= Time.deltaTime;
if(timer <= 0f)
{
// Add your movement or pause code/function here
}
else if(timer <= 0.25f)
{
timer = 3.0f; // Reset your timer
}
}

Or you could use an IEnumerator

private bool timeToPauseMove;
private float pauseTimer;

void Start()
{
timeToPauseMove = false;
pauseTimer = 3.0f;
}

void Update()
{
if(timeToPauseMove == false)
{
StartCoroutine(MovePlayer());
}
}

IEnumerator MovePlayer()
{
timeToPauseMove = true;
// insert your movement code or function here

yield return new WaitForSeconds(pauseTimer);
timeToPauseMove = false;
}