oldschool left, right, up, down character movement

ok so I having a hard time getting a character to move like the characters did from the gameboy games where you could move left, right, up and down on a grid(grid or tiles whatever you want to call it), with one fixed camera prospective and the character will rotate to look the way they are moving. It will move in all the directions, but I havent managed to find a way to get it to move on a grid or to get it to look in the direction its moving. this is what i have right now...

using UnityEngine;

using System.Collections;

public class NewPlayerMovement : MonoBehaviour {

Vector3 start;
Vector3 end;
public Transform target;
public bool moveRight = false;
public bool moveUp = false;
public bool moveLeft = false;
public bool moveDown = false;
public bool move = false;

void Awake () {
    start = transform.position;
}

void Update() {
    #region MoveRight
    if(Input.GetKey(KeyCode.RightArrow)){
        transform.Translate(Vector3.right * Time.deltaTime);
        if(move == false){
            moveDown = false;
            moveUp = false;
            moveLeft = false;
            moveRight = true;
            move = true;
        }
    }

    if(Input.GetKeyUp(KeyCode.RightArrow)&&moveRight == true){
        move = false;
    }
    #endregion

    #region MoveUp
    if(Input.GetKey(KeyCode.UpArrow)){
        transform.Translate(Vector3.forward * Time.deltaTime);
        if(move == false){
            moveLeft = false;
            moveDown = false;
            moveRight = false;
            moveUp = true;
            move = true;
        }
    }

    if(Input.GetKeyUp(KeyCode.UpArrow)&&moveUp == true){
        move = false;
    }
    #endregion

    #region MoveLeft
    if(Input.GetKey(KeyCode.LeftArrow)){
        transform.Translate(-Vector3.right * Time.deltaTime);
        if(move == false){
            moveDown = false;
            moveRight = false;
            moveUp = false;
            moveLeft = true;
            move = true;
        }
    }
    if(Input.GetKeyUp(KeyCode.LeftArrow)&&moveLeft == true){
        move = false;
    }
    #endregion

    #region MoveDown
    if(Input.GetKey(KeyCode.DownArrow)){
        transform.Translate(-Vector3.forward * Time.deltaTime);
        if(move == false){
            moveRight = false;
            moveUp = false;
            moveLeft = false;
            moveDown = true;
            move = true;
        }
    }

    if(Input.GetKeyUp(KeyCode.DownArrow)&&moveDown == true){
        move = false;
    }
    #endregion      

    transform.LookAt(target);

    if(moveRight == true){
        target.position = Vector3(gameObject.transform.position.x + 1,gameObject.transform.position.y,gameObject.transform.position.z);
    }

    if(moveUp == true){

        target.position = Vector3(gameObject.transform.position.x,gameObject.transform.position.y,gameObject.transform.position.z + 1);
    }       

    if(moveLeft == true){

        target.position = Vector3(gameObject.transform.position.x - 1,gameObject.transform.position.y,gameObject.transform.position.z);
    }

    if(moveDown == true){

        target.position = Vector3(gameObject.transform.position.x,gameObject.transform.position.y,gameObject.transform.position.z - 1);
    }
}

}

I was thinking of having the character look at a directional indicating GameObject but that was only because I failed to understand how I might get the GameObject to rotate using quaternion class. if anyone out there has seen something like this, or knows how to help me it would be great if you would help me... thanks

Format code properly by selecting it and hitting the code format button.

1 Answer

1

See here for moving on a grid. See here for facing in the direction of movement.

You'd have to modify that script. Since it's moving on a grid, the simplest way would be to not use colliders, but an array representing the world.