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);
}
}