Spawn GameObejcts random on an area without overlapping in 2d

Hi Pros,

i am trying to script a “builder” that walks aorund in a specific area and builds randomly objects in there. Biggest problem is how do i check if the area is “free” and my new building doesnt collide with another. ?

And how do i define the walking range ? made an Collider and in this collider he should be able to walk and build? How i let him check if he is inside this area?

Any tipps would be great. Thx so far

The easiest way to do it would be by having a collider on each buildable object, and using Physics2D.OverlapCircle() to determine if a spot is free. I would setup a layer specific for this check (“buildRangeColliders”) and disable it’s collisions with everything. Then create a serialized LayerMask field on the builder and enable only “buildRangeColliders” in it. Of course use that mask inside Physics2D.OverlapCircle().
How to determine where to try to build you might have already figured out. You can run a check on a regular grid with some random offset for each point, then check if that point is free and build there if it is. If you want more objects in the middle of an area you can have a chance of an object not being build when it is farther away from the center of the area.
You can use noise functions to have more complex patterns, and even make a shader in a shader graph that will serve you as a mask for object density. You would sample that shader by rendering it into a RenderTexture and using GetPixel() on it. Not terribly effective, but might work for you.