- Home /
Raycast based tile navigation
So, I am working on a puzzle roguelike (Odd, I know) and I use the following script to move my guy around. (It is a sprite on a 2-d plane- the rest of the enviroment is made of simple cubes with tile textures.)
var up : Texture;
var down : Texture;
var left : Texture;
var right : Texture;
var idle : Texture;
function Update () {
if(Input.GetKeyDown("w")){
renderer.material.mainTexture = up;
transform.Translate(Vector3.up * 1);
}
if(Input.GetKeyDown("a")){
renderer.material.mainTexture = left;
transform.Translate(Vector3.left * 1);
}
if(Input.GetKeyDown("s")){
renderer.material.mainTexture = down;
transform.Translate(Vector3.down * 1);
}
if(Input.GetKeyDown("d")){
renderer.material.mainTexture = right;
transform.Translate(Vector3.right * 1);
}
}
I threw this script together fairly quickly. W moves you up a unit, S down, ect. The game is tile based and turn based, so the player would move, then it would hand it over to the enemies, then back to the player. My problem is this: with the transform.translate, it does not seem to take into account colliders. So it moves through the walls. My levels are proceduraly generated, so statically mapping out co-ordinates is not an option. I was thinking that I could have the main gameobject send out raycasts in the cardinal directions, and if it hits something within one unit (One tile is 1x1) it would not allow you to move in that direction. I, however, have about as much experience with raycasts as my dog and there is probably a better way than what I am doing. So I am here to ask for some advice: how do I go about adding collision, and is there a better way of tile-based movement?