- Home /
What is the best way to navigate through a grid based level using Raycasting ?
I have a level with tiles of 1 unit each. How do I make sure that the player(lavender) never goes into the walls and always stays in the middle of the walkable area(steel blue) ? I have tried to put nodes(red) which, if hit by a raycast ray will allow the turn to happen, but this method fails when the player is at a high speed.
Check the level image here:
Check my code here:
RaycastHit hit;
Ray wRay;
Ray aRay;
Ray sRay;
Ray dRay;
void Update () {
transform.position += transform.forward * speed * Time.deltaTime;
if (Input.GetKey (KeyCode.W)) {
wRay.origin = transform.position;
wRay.direction = Vector3.left;
if (Physics.Raycast(wRay, out hit, 2f) && hit.transform.tag == "turnallowed")
{
gameObject.transform.rotation = Quaternion.Euler(0,270,0);
}
Debug.DrawRay(transform.position, Vector3.left, Color.red);
}
if (Input.GetKey (KeyCode.A)) {
aRay.origin = transform.position;
aRay.direction = Vector3.back;
if (Physics.Raycast(aRay, out hit, 2f) && hit.transform.tag == "turnallowed")
{
gameObject.transform.rotation = Quaternion.Euler(0,180,0);
}
Debug.DrawRay(transform.position, Vector3.back, Color.blue);
}
if (Input.GetKey (KeyCode.S)) {
sRay.origin = transform.position;
sRay.direction = Vector3.right;
if (Physics.Raycast(sRay, out hit, 2f) && hit.transform.tag == "turnallowed")
{
gameObject.transform.rotation = Quaternion.Euler(0,90,0);
}
Debug.DrawRay(transform.position, Vector3.right, Color.white);
}
if (Input.GetKey (KeyCode.D)) {
dRay.origin = transform.position;
dRay.direction = Vector3.forward;
if (Physics.Raycast(aRay, out hit, 2f) && hit.transform.tag == "turnallowed")
{
gameObject.transform.rotation = Quaternion.Euler(0,0,0);
}
Debug.DrawRay(transform.position, Vector3.forward, Color.green);
}
}
Answer by dterbeest · Sep 10, 2014 at 02:25 PM
The best way IMHO would be to keep an occupied flag in the grid itself..
class Cell {
public bool occupied;
}
class Grid {
public Cell[,] cells = new Cell[10,10];
}
class Move {
void Update() {
if (Input.GetKeyDown(Keycode.W)) { //move left
if (Grid.cells[currentX +1, currentY].occupied)
//false move
} //etc...
}
}
This is a very short example, but i think you get the idea
Working with arrays is exactly what I had done before and I had gotten it to work just fine, with the only exception that the player was basically teleporting into the empty space ins$$anonymous$$d of moving at a speed. And if it sees that the next grid is a wall, it will set the position of the player where it is and wont move. But, a problem that arose is that when I was making the player slide, and if the player moved onto the wall grid, it was only after reaching that grid, that the grid check was happening, and then it sets the position back to the previous grid. To be clear, even if I move into the wall grid, it is only after completing the movement, that the check comes into play and sets the movement back to the previous grid. Here is the script I was using:
using UnityEngine;
using System.Collections;
public class player : $$anonymous$$onoBehaviour {
public GameObject levelCreator_;
levelCreator temp;
public Vector2 playerPos;
int[,] mapArray = new int[13,17];
void Start () {
levelCreator_ = GameObject.Find ("LevelCreatorGameObject");
temp = levelCreator_.gameObject.GetComponent<levelCreator>();
mapArray = temp.mapArray;
for(int i = 1; i<12; i++)
{
for(int ii = 1; ii < 16; ii++)
{
if( mapArray[i,ii] == 2)
{
playerPos = new Vector2(i,ii);
}
}
}
transform.position = new Vector3(playerPos.x,0,playerPos.y);
}
void Update () {
if (gameObject.CompareTag("alive"))
getInput();
}
void getInput()
{
Vector2 oldPos = playerPos;
if(Input.Get$$anonymous$$eyDown("up"))
{
playerPos += new Vector2(-1,0);
}
if(Input.Get$$anonymous$$eyDown("down"))
{
playerPos += new Vector2(1,0);
}
if(Input.Get$$anonymous$$eyDown("left"))
{
playerPos += new Vector2(0,-1);
}
if(Input.Get$$anonymous$$eyDown("right"))
{
playerPos += new Vector2(0,1);
}
if(mapArray[(int)playerPos.x,(int)playerPos.y] == 1) //typecast as vector2 stores floats
{
playerPos = oldPos;
}
transform.position = new Vector3(playerPos.x,0,playerPos.y);
}
}
Answer by Jan_Julius · Sep 10, 2014 at 01:35 PM
Why are you having a grid based level when you are accelerating?
Why not just have the raycasts look for walls and if there is a wall you cant use that button to move the way the raycast is raying?
By grid based level, I meant that the tiles are manually placed in a grid fashion. Apologies if I didn't explain it better. I'll try looking for walls ins$$anonymous$$d. But I guess this still will make the player not align in the middle of the lane. I'm also not sure how to make the player turn sideways by itself if it touches a wall, headlong.
By turning you mean rotating the object? If you resize the cube a little so it doesn't touch the wall you can rotate it, you can also make it rotate first and then letting it move when the button gets pressed again so you make sure the object is facing that way by checking its rotation.
Something I've done before is giving it a status, whatever the status is in corresponds with it's rotation. So let's say pressing D will make you face to the right and rotate your object 270 degrees you can do something like this:
public int status;
void FixedUpdate(){
switch(status){
case 0:
gameObject.transform.eulerAngles = new Vector3(
0, 0, 0);
break;
case 1:
gameObject.transform.eulerAngles = new Vector3(
0, 0, 90);
break;
case 2:
gameObject.transform.eulerAngles = new Vector3(
0, 0, 180);
break;
case 3:
gameObject.transform.eulerAngles = new Vector3(
0, 0, 270);
break;
}
}