Don't overlap "tiles"

I can’t figure out how to stop “overlapping” with my prefab objects, called tiles. I’m very close, but can’t don’t really understand how to save “taken spots”. The grid is 1x1 but the tiles are 3x3. Here’s what I have so far.

  public Transform gridCellPrefab;
  public Transform tile;
  public Transform onMousePrefab;
  public Vector3 smoothMousePosition;
  [SerializeField] private int height;
  [SerializeField] private int width;

 Vector3 mousePosition;
 private Node[,] nodes;
 private Plane plane;

 void Start()
 {
     CreateGrid();
     plane = new Plane(inNormal:Vector3.up, inPoint:transform.position);
}

 void Update()
 {
     GetMousePositionOnGrid();
}

 void GetMousePositionOnGrid()
 {
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if (plane.Raycast(ray, out var enter))
     {
         mousePosition = ray.GetPoint(enter);
         smoothMousePosition = mousePosition;
         mousePosition.y = 0;
         mousePosition = Vector3Int.RoundToInt(mousePosition);
         foreach (var node in nodes)
         {
             if (node.cellPosition == mousePosition && node.isPlaceable)
             {
                 if(Input.GetMouseButtonDown(0) && onMousePrefab != null)
                 {
                     node.isPlaceable = false;
                     onMousePrefab.GetComponent<ObjMousePlacement>().isOnGrid = true;
                     onMousePrefab.position = node.cellPosition + new Vector3 (0,.05f,0);
                     onMousePrefab = null;
                     
                     
                 }
             }
         }
     }
 }

    public void OnMouseClickOnUI()
    {
        if(onMousePrefab == null)
        {
            onMousePrefab = Instantiate (tile, mousePosition, Quaternion.identity); 
        }
    }

    private void CreateGrid()
    {
        nodes = new Node[width, height];
        var name = 0;
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                Vector3 worldPosition = new Vector3 (i,0,j);
                Transform obj = Instantiate(gridCellPrefab, worldPosition, Quaternion.identity);
                // Name object in hierarchy
                obj.name = "Cell " + name;
                nodes[i,j] = new Node(isPlaceable: true, worldPosition, obj);
                name++;
            }

        }
    }

}

public class Node
{
    public bool isPlaceable;
    public Vector3 cellPosition;
    public Transform obj;

    public Node(bool isPlaceable, Vector3 cellPosition, Transform obj)
    {
        this.isPlaceable = isPlaceable;
        this.cellPosition = cellPosition;
        this.obj = obj;
    }
}
1 Like

When you create the grid, you can multiply i and j by the size of the tiles (3).

Vector3 worldPosition = new Vector3 (i * 3, 0, j * 3);

Checking if mouse is over the tiles you could use:

foreach (var node in nodes) {

    var cellPosition = node.cellPosition;
	if (mousePosition.x > cellPosition.x - 1.5f &&
	    mousePosition.x < cellPosition.x + 1.5f &&
	    mousePosition.z > cellPosition.z - 1.5f &&
	    mousePosition.z < cellPosition.z + 1.5f) {

      //Mouse over tile
	}
}