using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridSystem
{
private int width;
private int height;
private float cellSize;
private GridObject[,] gridObjectArray;
public GridSystem(int width, int height, float cellSize)
{
this.width = width;
this.height = height;
this.cellSize = cellSize;
gridObjectArray = new GridObject[width, height];
for (int x = 0; x < width; x++)
{
for (int z = 0; z < height; z++)
{
GridPosition gridPosition = new GridPosition(x, z);
gridObjectArray[x, z] = new GridObject(this, gridPosition);
}
}
}
public Vector3 GetWorldPosition(int x, int z)
{
return new Vector3(x, 0, z) * cellSize;
}
public GridPosition GetGridPosition(Vector3 worldPosition)
{
return new GridPosition(
Mathf.RoundToInt(worldPosition.x / cellSize),
Mathf.RoundToInt(worldPosition.z / cellSize)
);
}
public void CreateDebugObjects(Transform debugPrefab)
{
for (int x = 0; x < width; x++)
{
for (int z = 0; z < height; z++)
{
GameObject.Instantiate(debugPrefab, GetWorldPosition(x, z), Quaternion.identity);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridObject
{
private GridSystem gridSystem;
private GridPosition gridPosition;
public GridObject(GridSystem gridSystem, GridPosition gridPosition)
{
this.gridSystem = gridSystem;
this.gridPosition = gridPosition;
}
}
Thank you for your help!