Grid Based Movement (Pokemon-like)

Hi,

I’m trying to write a script which will move the player depending on the value of a static variable, which is controlled by other scripts.

I have tried to get it to work, but am failing so badly, so I decided to put it here and see if anyone knows where I am going wrong.

So far the character only travels in one direction, which is ok, but I can only move him forward one time and then he won’t move.

I would like this movement to be like the character moves in Pokemon games.

This is starting to drive me crazy so any help would be much appreciated.

Here is my code so far:

KeyboardInput.js

#pragma strict

function Update () {
	if (Input.GetKey(KeyCode.UpArrow)){
		PlayerMove.directionPress = 1;
	}
	else if (Input.GetKey(KeyCode.RightArrow)){
		PlayerMove.directionPress = 2;
	}
	else if (Input.GetKey(KeyCode.DownArrow)) {
		PlayerMove.directionPress = 3;
	}
	else if (Input.GetKey(KeyCode.LeftArrow)){
		PlayerMove.directionPress = 4;
	}
	else if (Input.GetKey(KeyCode.W)){
		PlayerMove.directionPress = 1;
	}
	else if (Input.GetKey(KeyCode.D)){
		PlayerMove.directionPress = 2;
	}
	else if (Input.GetKey(KeyCode.S)) {
		PlayerMove.directionPress = 3;
	}
	else if (Input.GetKey(KeyCode.A)){
		PlayerMove.directionPress = 4;
	}
	//All other keys here.
	else {
		PlayerMove.directionPress = 0;
	}
}

PlayerMove.js

#pragma strict

static var directionPress : int = 0; //0= none 1 = up, 2= right, 3= down, 4= left
private var movingDirection : int = 0;
var moveSpeed : float = 5; //units per second
var isMoving : boolean = false;


class TileTrigger{ //class for 
    var isOccupied : boolean;
    var hasFloor : boolean;
}

var tileNorth = TileTrigger();
var tileEast = TileTrigger();
var tileSouth = TileTrigger();
var tileWest = TileTrigger();

function Update() {
	Debug.Log("directionPress = " + directionPress + ". movingDirection = " + directionPress + ".");
	Debug.Log("transform.position.z = " + transform.position.z);
	Debug.Log("isMoving = " + isMoving);
	if (!(isMoving) && movingDirection == 0 && directionPress != 0) {
		movingDirection = directionPress;
		isMoving = true;
	}
}

function LateUpdate () {
	if (isMoving) {
		if (movingDirection == 1) {
			if (parseInt(transform.position.z + (Vector3.forward*Time.deltaTime * moveSpeed).z) >= parseInt(transform.position.z)) {
				// It has gone over onto the next square.
				if (directionPress == movingDirection) {
					MoveForward();
					// if its moving in the same direction, then move normally.
				}
				if (directionPress != movingDirection) {
					if (directionPress == 0) {
						isMoving = false;
						transform.position.z = parseInt(transform.position.z + 1);
						// if no button press, then move to the next square.
						Debug.Log("Moving onto the next square.");
					}
					else {
						transform.position.z = parseInt(transform.position.z + 1);
						ChangeRot();
						// if another key pressed, then move to next square and rotate.
						// then next frame the norm happens.
						Debug.Log("Stopping at this square.");
					}
				}
			
			} //the end of if it has gone over to the next square.
			else {
				MoveForward();
			}
		} //the end of if movingDirection == 1
		
		if (movingDirection == 0) {
			isMoving = false;
		}// the end of if movingDirection == 0
	} //end of isMoving
	Debug.Log(" ");
}

function MoveForward () {
	transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
}

function ChangeRot() {
	Debug.Log("ChangeRot() just happened."); //but this never happens. :(
	movingDirection = directionPress;
	if (movingDirection != 0) {
		transform.rotation.z = (movingDirection - 1) * 90;
	}
}

Grid based movement

–David–