How to move the player only 1 tile per buttonPress?

Hi.

I’m creating a 2D puzzle game where the player moves in a top-down world. Now, I’m having a bit of trouble in the movement section because I don’t really know how to make the player move only 1 tile at a time.

This kind of movement is critical part of my game.

Thanks for any kind of infromation.

=========

PS. If possible, would love a example script. :slight_smile:

vector2 target;

if (Input.GetKeyDown (KeyCode.UpArrow)) {

			y = moveDistance;

			x = 0;

		}

`` else if (Input.GetKeyDown (KeyCode.DownArrow)) {

			y = -moveDistance;

			x = 0;

		}

`` else if (Input.GetKeyDown (KeyCode.LeftArrow)) {

			x = -moveDistance;

			y = 0;

		} 

``else if (Input.GetKeyDown (KeyCode.RightArrow)) {

			x = moveDistance;

			y = 0;

		}

if (moveUp || moveDown || moveLeft || moveRight) {

			transform.position=new Vector2(transform.position.x+x,transform.position.Y+y);
		}

	}

note

// in the start assign how much distance you want to move .(if you ur tail 100*100 then moveDistance is 1…

var target:Vector3;
target=transform.position;
var go:Vector3;
var speed:float;
speed=10;
function Update () {
go = Vector3.MoveTowards(go, target,speed*Time.deltaTime);
gameObject.transform.position=go;

if(Input.GetKeyDown("up"))   {target.y=Mathf.Round(transform.position.y)+1;}
if(Input.GetKeyDown("down")) {target.y=Mathf.Round(transform.position.y)-1;}
if(Input.GetKeyDown("right")){target.x=Mathf.Round(transform.position.x)+1;}
if(Input.GetKeyDown("left")) {target.x=Mathf.Round(transform.position.x)-1;}
}

this would keep his position rounded off to 1. this script assumes your sqaure positions are exactly 1 apart and 1 wide. i assume that your y is up. anyways this should give you the idea. if you want to slide to any square just change the target variable to match the new sqaure position in your script and and he will go there.