# grid movement

Hi, i need help with grid movement.
The player moves himselfs and he react at a direction changes. He can move at four directions (up, down, right, left), but not in a grid. There is code:

``````using UnityEngine;
using System.Collections;

public enum Direction { Right, Up, Left, Down };

public class NewBehaviourScript : MonoBehaviour {

public const float speed = 2.0f;
public Direction direction = Direction.Right;

void Update()
{
if (Input.GetKey(KeyCode.RightArrow))
{
direction = Direction.Right;
}
else if (Input.GetKey(KeyCode.UpArrow))
{
direction = Direction.Up;
}
else if (Input.GetKey(KeyCode.DownArrow))
{
direction = Direction.Down;
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
direction = Direction.Left;
}

Vector3 pos = transform.position;

if (direction == Direction.Right)
{
pos += Vector3.right;
}
else if (direction == Direction.Up)
{
pos += Vector3.up;
}
else if (direction == Direction.Left)
{
pos += Vector3.left;
}
else if (direction == Direction.Down)
{
pos += Vector3.down;
}

transform.position = Vector3.MoveTowards(transform.position, pos, speed * Time.deltaTime);
}
}
``````

I need the player to move along the grid.

If “move along the grid” means, that the character should teleport every x seconds onto the next possible position, you could do the following:

Remove the call of MoveTowards() and replace it by:

transform.position = pos + directionVector*cellSize;

(Where directionVector is what you find out in your if/else if cascade and cellSize is the size of one field of your grid.)

Then limit this procedure by time: Create a variable time = 0. Then do the following in Update():
IF (time > 0) reduce time by Time.deltaTime ELSE execute the movement and set time to x.