I am following an A* tutorial but the part that I am mostly interested in is the way the grid is created … http://unitygems.com/astar-1-journeys-start-single-step/ … I understand what all the elements separatley do but I dont understand how the grid is actually created.
//Representation of a grid cell
public class GridCell
{
public bool walkable;
public float height;
}
//Width and height of the map in cells
int width, height;
//Active map of the world
public GridCell[,] cells;
//Size in world units of a cell
public float cellSize = 0.5f;
//Scan the bounds of the model and create a grid
bounds = renderer.bounds;
//Work out the top left corner
topLeftCorner = bounds.center - bounds.extents + new Vector3(0, bounds.size.y, 0);
//Calculate the dimensions of the grid map
width = Mathf.RoundToInt(bounds.size.x / cellSize);
height = Mathf.RoundToInt(bounds.size.z / cellSize);
//Create the grid map
cells = new GridCell[width, height];
//Scan for walkable terrain in each cell
for(var x = 0; x < width; x ++)
{
for(var y = 0; y < height; y++)
{
//Get the position for a ray
var currentPosition = topLeftCorner + new Vector3(x * cellSize, 0, y * cellSize);
RaycastHit hit;
//Create a cell for the grid
var cell = new GridCell();
cells[x, y] = cell;
//Cast the ray
if(Physics.Raycast(currentPosition, -Vector3.up, out hit, bounds.size.y))
{
//The height of the highest item in the cell
cell.height = hit.point.y;
//Test if the thing we hit was walkable
if(((1 << hit.collider.gameObject.layer) & walkableLayer) != 0)
{
//Flag the cell as walkable
cell.walkable = true;
}
}
}
}
This is the script that is used to create the grid. I understand what all the elements do separatley but I dont know how the grid is represented…I mean it has no Vector3 position…the cell isnt attached to a separate gameObject…and as for the RayCast how do I know if and when the ray hits a cell adn the rays are cast on the cells.
I know the grid is made from a gameObject (just a cube) divided into cells but I dont understand where is a cel represented on the cube…I see a 2d array cells[x,y] = cel and I know it is the grid but what is its position on the cube…what is the link betwen the RayCast and each cel in the grid?
The for x,y loop is used for the currentPosition , the grid cells[x,y] = cel; and the RayCast…I see the link betwen the rayCast and currentPosition but what is the link betwen cells and the rays.