- Home /
How to make a Grid movement (tile per tile)
I want to make my player move one tile each time I press the button on keyboard, without teleporting and smoothily. Just exactly like the game Tibia or the old pokemons. In my code I did it, but the player moves too fast from one tile to another, and I don't know how to make it slower, i'm quite new to programming.
public class PlayerMovement : MonoBehaviour { float sqm = 0.16f; public float moveSpeed;
public Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
// Moves the GameObject using it's transform.
rb.isKinematic = true;
}
private void FixedUpdate()
{
if(Input.GetKeyDown(KeyCode.W)){
rb.MovePosition(transform.position + transform.up * moveSpeed * sqm * Time.deltaTime);
}
}
How exactly should the Input work? Do you want the player needs to press every time the "W" key to move or can he also hold "W" to move contantly?
Answer by FlaSh-G · Aug 11, 2020 at 08:28 PM
There's two things you're trying to do (potentially) each frame:
Check if the player made an input to move.
Move.
Since 1. is a one-time thing and 2. is supposed to happen over the course of multiple frames, you need to start the movement in the input if statement, and then let it run. So, you cannot put the movement code in the body of the if statement; you rather need something to start it.
There's multiple ways to do it, here's one I'd personally consider first. I'll not use a Rigidbody here because you don't need one.
private Vector2Int targetPosition;
private void Awake()
{
// Set this so we don't wander off at the start
targetPosition = new Vector2Int(Mathf.RoundToInt(transform.position.x), Mathf.RoundToInt(transform.position.y));
transform.position = (Vector2)targetPosition;
}
private void Update()
{
var moving = (Vector2)transform.position != targetPosition;
if (moving)
{
MoveTowardsTargetPosition();
}
else
{
SetNewTargetPositionFromInput();
}
}
private void MoveTowardsTargetPosition()
{
transform.position = Vector2.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
}
private void SetNewTargetPositionFromInput()
{
if (Input.GetKey(KeyCode.W))
{
targetPosition += Vector2Int.up;
}
else if ...
}
To get some questions out of the way:
Why Vector2Int? Vector2Int is a Vector2 that only has ints, so no decimal places. Vector2 would work as well until at some point, adding Vectors on top of each other might introduce rounding errors, and your player character will end up at position 14.001 instead of 14.
What does
moving
do? Moving is the result of the check whether the player character is at its target position. If it's not, it's "moving". While it's moving, the user can't update the target position, but has to wait until the player character has reached the target tile. And while it's moving, the method that actually moves is being called, usingVector2.MoveTowards
to move the position at a constant rate.
In the end, all the input does is update the target position, which in turn makes the character be "moving". Not how it's Input.GetKey
so the player can hold down a direction key. It's fine to use it here because the game only checks whether it's pressed in while we're not moving.
Side note: Never use Input.GetKeyDown
in FixedUpdate
, inputs may be lost or counted multiple times.
Your answer
Follow this Question
Related Questions
[2D] Moving the player 1 tile at a time using rigidbody movement 0 Answers
Making a bubble level (not a game but work tool) 1 Answer
2D How to make character slowly stop after moving 1 Answer
How to make the player move a fixed distance? (Tile per tile) 0 Answers
Question on Sprite and Movement 0 Answers