Random Spawning

Hi,
I have a area(4x4) in which I would like the randomly spawn my building prefabs, the problem is that some are 1x1, some are 1x2 and some are 4x4. I am just wondering how I could randomly spawn these buildings without them overlapping.

I have a cube GameObject with the renderer disabled and the box collider removed to represent the outlines of the building (they’re not all square so this makes the spawning easier), this is the area that I would like to spawn within. Does anyone have any ideas how this could be achieved in C# ?

Thanks in advance.

Spawn your first prefab. Then spawn your second prefab but don’t enable it’s renderer. Is its trigger box overlapping with another one? If yes, nudge it a block. If it isn’t, enable it’s renderer. Spawn next one.

first you should randomly choose a position, lets say you chose -5,4 (in terms of x and z only) then you should cast a ray from that point to all four cardinal directions in the size f your object, lets say your object size is 5 so casting a ray from center, you should take ray size as 2.5 and cast four rays at four directions, and if the ray hits an object than obviously you should again randomly choose the coordinates, hope this helps.