Randomly place buildings in an area/plot


The red boxes - Building prefabs
The black box - The plot in which I wanna place the buildings
So I wanna randomly place these prefabs in the plot without overlapping (if possible I would be happy if their positions are snapped to 1 unit)
Can anyone give me a good source to see how to do this or tell me how I can go about this?

Nvm, so the tetris is an inventory system, I actually found this useful, ty

Your question is pretty vague, so I make some assumptions.

If you have a grid and objects made of tiles of even size, use a single tile from a block (like a tetris tetromino block) as pivot. If your L-shaped block is at location 2,2 (assuming the corner block is the pivot block and object is made of three tiles), then its right side block is at 3,2, and the top part is at 2,3. Then you’ll have to check those locations in your grid object. You’ll also have to take into account rotation, if you allow to rotate objects, then you’ll also have to rotate the objects block representation and use it to check for overlaps. You also have check for grid edges, so if a block is out of bounds, that location is not OK. You could simply brute force the placement if you only place a few buildings, pick a location and check if you can build there, if not try again until you find a free spot. But if you really want to fill the area to the max, you’ll have to walk the map and see in there is suitable spots left.

If your objects are not tile based, then the easiest way is probably to pick a random spot and do a collider check to see if it overlaps with a collider of another object, repeat until a free spot is found. Of course this is suboptimal way to do this but still pretty fast on modern hardware until the map is large and only a few spots are free. You also could keep track of areas already populated, and then pick random locations on areas that are not yet populated.

If you are looking for tutorials on this topic, IIRC Code Monkey’s YouTube channel might be helpful. “inventory tetris” or “tetris inventory” tutorial is also something you could google.

nono, this is a 3d game, not tetris with blocks falling down, I wanna procedurally generate segments for my endless runner game, in the middle is a road and on the sides are empty areas, I wanna randomly position buildings in these empty areas