I thought a good way to implement a grid of nodes would be to use an existing tilemap. I have designed a 2D isometric tile, that is 128x64 pixels, and a block which is 128x128 pixels. The grid displays but is offset to where it should be, I’m not sure if I have something wrong in the settings for tilemaps or if I have made a mistake with the script.

```
public class Grid : MonoBehaviour
{
public Tilemap tilemap; // Reference to the isometric tilemap
public LayerMask walkableLayerMask; // For obstacle detection
Node[,] grid;
int gridSizeX, gridSizeY;
void Start()
{
gridSizeX = tilemap.cellBounds.size.x; // x-axis grid size from tilemap
gridSizeY = tilemap.cellBounds.size.y; // y-axis grid size from tilemap
CreateGrid();
}
void CreateGrid()
{
grid = new Node[gridSizeX, gridSizeY];
BoundsInt bounds = tilemap.cellBounds; // boundaries of tilemap in cell co-ordinates
TileBase[] allTiles = tilemap.GetTilesBlock(bounds); // Retrieves all tiles in the given bounds
for (int x = 0; x < gridSizeX; x++) // For x-axis tiles
{
for (int y = 0; y < gridSizeY; y++) // For y-axis tiles
{
TileBase tile = allTiles[x + y * bounds.size.x];
Vector3 cellWorldPos = tilemap.GetCellCenterWorld(new Vector3Int(x, y, 0)); // Gets tilemap centre
bool walkable = !(Physics2D.OverlapCircle(cellWorldPos, 0.1f, walkableLayerMask));
grid[x, y] = new Node(walkable, cellWorldPos); // Create a Node instance and store it
}
}
}
public Node NodeFromWorldPoint(Vector3 worldPosition)
{
// Calculate the percentage of the x and y co-ordinates within the tilemap
float percentX = Mathf.Clamp01((worldPosition.x + tilemap.cellSize.x * 0.5f - tilemap.origin.x) / tilemap.size.x);
float percentY = Mathf.Clamp01((worldPosition.y + tilemap.cellSize.y * 0.5f - tilemap.origin.y) / tilemap.size.y);
// Calculate the array indices based on the percentages
int x = Mathf.RoundToInt((gridSizeX - 1) * percentX);
int y = Mathf.RoundToInt((gridSizeY - 1) * percentY);
return grid[x, y];
}
void OnDrawGizmos()
{
Gizmos.DrawWireCube(transform.position, new Vector3(gridSizeX, gridSizeY, 1));
if (grid != null)
{
foreach (Node n in grid)
{
Gizmos.color = n.isWalkable ? Color.white : Color.red;
Gizmos.DrawCube(n.position, Vector3.one * (Mathf.Min(tilemap.cellSize.x, tilemap.cellSize.y) - 0.1f));
}
}
}
}
```

I think the grid is of by about 31,22, from quickly counting it.