public List <PFNode> GetNeighboringNodes(PFNode node) { List <PFNode> neighboringNodes = new List <PFNode>(); int xCheck; int yCheck; // Right side xCheck = node.GridX + 1; yCheck = node.GridY; if (xCheck >= 0 && xCheck < _gridSizeX) { if (yCheck >= 0 && yCheck < _gridSizeY) { neighboringNodes.Add(_grid[xCheck, yCheck]); } } // Left side xCheck = node.GridX - 1; yCheck = node.GridY; if (xCheck >= 0 && xCheck < _gridSizeX) { if (yCheck >= 0 && yCheck < _gridSizeY) { neighboringNodes.Add(_grid[xCheck, yCheck]); } } // Top side xCheck = node.GridX; yCheck = node.GridY + 1; if (xCheck >= 0 && xCheck < _gridSizeX) { if (yCheck >= 0 && yCheck < _gridSizeY) { neighboringNodes.Add(_grid[xCheck, yCheck]); } } // Bottom side xCheck = node.GridX; yCheck = node.GridY - 1; if (xCheck >= 0 && xCheck < _gridSizeX) { if (yCheck >= 0 && yCheck < _gridSizeY) { neighboringNodes.Add(_grid[xCheck, yCheck]); } } return(neighboringNodes); }
void CreateGrid() { _grid = new PFNode[_gridSizeX, _gridSizeY]; Vector3 bottomLeft = transform.position - Vector3.right * GridWorldSize.x / 2 - Vector3.forward * GridWorldSize.y / 2; // Iterate through the grid for (int y = 0; y < _gridSizeY; y++) // todo check for bug { for (int x = 0; x < _gridSizeX; x++) { Vector3 worldPoint = bottomLeft + Vector3.right * (x * _nodeDiameter + NodeRadius) + Vector3.forward * (y * _nodeDiameter + NodeRadius); bool obstacle = Physics.CheckSphere(worldPoint, NodeRadius, ObstacleLayerMask); // todo check for bug _grid[x, y] = new PFNode(obstacle, worldPoint, x, y); } } }