- Home /
Question by
FatBOY2 · May 30, 2020 at 08:30 AM ·
gridlayoutgrid based game
2x2 grid instead of 1x1
Hello, I have got this code that produces a grid (and also draws it) but it is only 1x1, I need it to be 2x2 though. Changing the cellWidth and cellHeight does only change the size of the entire gridlayout. How can I do this? I can also provide further code, but I think this should suffice.
public GameObject bottomLeft, topRight;
Node[,] myGrid;
public List<Node> path;
public LayerMask whatStopsMovement;
//GRID INFO
int xStart, zStart;
int xEnd, zEnd;
int vCells, hCells; //amount of cells in Grid (vertical and horizontal)
int cellWidth = 1;
int cellHeight = 1;
private void Awake()
{
GridCreate();
}
void GridCreate()
{
xStart = (int)bottomLeft.transform.position.x;
zStart = (int)bottomLeft.transform.position.z;
xEnd = (int)topRight.transform.position.x;
zEnd = (int)topRight.transform.position.z;
hCells = (int)((xEnd - xStart) / cellWidth);
vCells = (int)((zEnd - zStart) / cellHeight);
myGrid = new Node[hCells + 1, vCells + 1];
UpdateGrid();
}
public void UpdateGrid()
{
for (int i = 0; i <= hCells; i++)
{
for (int j = 0; j <= vCells; j++)
{
bool walkable = Physics.CheckSphere(new Vector3(xStart + i, 0, zStart + j),0.4f ,whatStopsMovement);
myGrid[i, j] = new Node(i, j, 0, walkable);
}
}
}
private void OnDrawGizmos()
{
if(myGrid != null)
{
foreach (Node node in myGrid)
{
Gizmos.color = (node.walkable) ? Color.white : Color.red;
Gizmos.DrawWireCube(new Vector3(xStart + node.posX, 0, zStart + node.posZ), new Vector3(0.8f, 0.8f, 0.8f));
}
}
}
Comment