Help with Grid Movement

I am trying to make a game controls similar to a Turn Based Strategy game where the player moves on a grid. I am planing on having the player be a sprite so it i will be 2D. The way i plan to implement it is simple. Have a Plane with a 'GridDetection' game object right below the center of the plane. This way i can have a empty game object connected to the Player to shoot out rays to see if that is a legitimate place to move. As in, i do not want the player to be able to move out side of the grid or into spaces within the grid they aren't aloud to. I plan to make it so when the GridDetection object is hit, it sends information to the player about what type of terrain it is telling the player if it can move on it or has to wait (like Water, if the player is a boat it can, but not as a person). The problem is, i can't seem to figure out how to make it move correctly. I want to shoot out a ray into the direction indicated by the players input (WASD) and check if that is a valid space to move. If so, rotate to face it, then move forward 10 units (that is the distance between the centers of the the grids). So the question is: How do i rotate to face the correct direction then move to it. Thanks for you help. Here is the script i made that does not work and is not complete as i work on one part, test it, then code the next part, test it....as it didn't work at first i havn't finished it.

var RayLength : int = 10;
var rotateSpeed : int = 10;
var moveSpeed : int = 10;
var Parent : GameObject;

private var leftRotate : boolean = false;
private var rightRotate : boolean = false;
private var turnAround : boolean = false;
private var move : boolean = false;

function FixedUpdate () {

    var left = (transform.TransformDirection(-Vector3.right));
    var right = (transform.TransformDirection(Vector3.right));
    var forwards = (transform.TransformDirection(Vector3.forward));
    var backwards = (transform.TransformDirection(-Vector3.forward));
    var hit : RaycastHit;   

    if(Input.GetKeyDown("a")) {
        if(Physics.Raycast(transform.position, left , hit,RayLength)){
            if(hit.transform.tag == "GridDetection") {
                Debug.Log("Left");
                leftRotate = true;
            }
        }
    }
    if(Input.GetKeyDown("d")) {
        if(Physics.Raycast(transform.position,right,hit,RayLength)){
            if(hit.transform.tag == "GridDetection") {
            }
        }
    }
    if(Input.GetKeyDown("s")) {
        if(Physics.Raycast(transform.position,backwards,hit,RayLength)){
            if(hit.transform.tag == "GridDetection") {
            }
        }
    }
    if(Input.GetKeyDown("w")) {
        if(Physics.Raycast(transform.position,forwards,hit,RayLength)){
            if(hit.transform.tag == "GridDetection") {
            }
        }
    }
    if(leftRotate) {
            Parent.transform.rotation.y -= 1 * Time.deltaTime; 
            if(Parent.transform.rotation.y == 270) {
                leftRotate = false;
                move = true;
            }
        }

    if(move) {
        Parent.transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    }
}

The nice thing about using a grid is that it simplifies things a lot. You can create a simple class or struct that represents the different attributes of your grid (like "is water" or "is forbidden", whatever you need). Also, in a grid you only have 4 possibilities of movement (or 8 if you decide to allow diagonal movement). So, depending on the attributes of your game space you can either create an array of your "Tile" class/struct, or create a linked list kind of structure. You will use a linked list in cases where the game space is more sparse because an array would be a bit of waste in that case. However, an array may still make sense because it allows random access (which a linked list does not).

So, if you need to be able to check "what's going on in position x,y", an array might be nice - if you just need to know "what's coming when I move north", a linked list is fine. Even with the linked list, you could create lookup structures (Dictionary) for random access - but keep in mind that this will only make sense if you have a lot of "dead space" where the player can't move and nothing ever happens.

Assuming you're using a linked list structure like this (this code is just meant as illustration, so don't worry too much about the syntax which is C# which I find much more readable):

EDIT: Thanks to Quietus for pointing out that structs might not work for this due to their "by-value-nature" ... I'm not sure if anyone actually tried this but it probably wouldn't have worked in the original form (public struct GridTile). With the new form (public class GridTile) it should work fine.

public class GridTile {
    public GridTile north; // JavaScript would be var north : GridTile;
    public GridTile south;
    public GridTile west;
    public GridTile east;
    public bool isWater = false; // just a default value
    public bool isAccessible = true; // just a default value
}

And your player having an attribute "GridTile myCurrentTile;", you could do this:

if (notMoving && Input.GetKeyDown("w")) {
    if (myCurrentTile.north.isAccessible) {
        if (!myCurrentTile.north.isWater || hasBoat) {
            StartMovingNorth();
        }
    }
}

So, "notMoving" would be a boolean that you'd use to check if the player is currently in a moving animation, and "hasBoat" is a bool which indicates whether the player currently has a boat that he could use to move over water.

Obviously, depending on your game design there's a lot of possibilities (like, if the player doesn't have a boat but if the player object is a boat, you'd have to check for "water and boat" or "not water and not boat"), but this should give you a starting point.

One tricky thing with those grids: You'll need to spend some time thinking about how to author the grid attributes.

Contrary to what I said in the beginning, you might actually implement those "Tiles" as MonoBehaviours and lay them out in the editor. And you could then easily write editor scripts to design your levels ... but that would be another topic. In the end, these things really depend a lot on how you want to set up things so I'm trying to just give a general idea of what are possible approaches.

EDIT: There's some related questions / answers that might help setting up the grid system:

And one more thing came to my mind regarding whether to use an array or linked list structure: Arrays work best with rectangular maps while the linked list approach allows any kind of shape (and it could even support "teleporters" trivially by simply assigning a tile that is on a completely different location to one of the slots ... hm ... sounds like creating grid-based games could be quite a bit of fun ;-) ).

I wouldn't use raycasting or anything. You can use something like this for smooth grid movement, and use a 2D array to track what sort of square the player is in, to determine whether an attempted move is valid or not.