Hey I am working on a tutorial learning A* pathing for my game following this tutorial series:
I have followed the code to the point of grid creation, my grid outline wireframe appears, but when I press play only a single red cube appears at world center (0,0,0)
I have been staring at my code for two days trying different fixes. What am I doing wrong?
;
public class Grid : MonoBehaviour
{
Node[,] grid;
public Vector2 gridWorldSize;
public float nodeRadius;
public LayerMask unwalkableMask;
public float nodeDiameter;
int gridSizeX, gridSizeY;
void Start()
{
nodeDiameter = nodeRadius * 2;
gridSizeX = Mathf.RoundToInt(gridWorldSize.x / nodeDiameter);
gridSizeY = Mathf.RoundToInt(gridWorldSize.y / nodeDiameter);
CreateGrid();
}
void CreateGrid()
{
grid = new Node[gridSizeX, gridSizeY];
Vector3 worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x / 2 - Vector3.up * gridWorldSize.y / 2;
for (int x = 0; x < gridSizeX; x++)
{
for (int y = 0; y < gridSizeY; y++)
{
Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + Vector3.up * (y * nodeDiameter + nodeRadius);
bool walkable = !(Physics.CheckSphere(worldPoint, nodeRadius, unwalkableMask));
grid[x, y] = new Node(walkable, worldPoint);
}
}
}
void OnDrawGizmos()
{
Gizmos.DrawWireCube(transform.position, new Vector3(gridWorldSize.x, gridWorldSize.y, 1));
if (grid != null)
{
foreach (Node n in grid)
{
Gizmos.color = (n.walkable) ? Color.white : Color.red;
Gizmos.DrawCube(n.worldPosition, Vector3.one * (nodeDiameter - 0.1f));
}
}
}
}
and here is the Node class
public class Node
{
public bool walkable;
public Vector3 worldPosition;
public Node(bool _walkable, Vector3 _worldposition)
{
_walkable = walkable;
_worldposition = worldPosition;
}
}
Thank you for any help