Spawn random in area and then remove
Hi, I've recently begun trying to create a system for generating objects within given areas on a terrain. By default i would simply use random.range(value 1, value 2) to achieve this.
------------------------------------.
However since i do not want these objects to overlap each other. I want to give them areas to determine where and where not it should be. Also, these areas needs to be different since im using them in a way to create areas for placing other objects, basicly im randomly trying to create areas for cities and the subobjects being houses.
-------------------------------------. Heres a pic to show what im trying to achieve:
----------------------------------------.
Problem is i have no idea how i would go about spawning these areas in a manner so they do not go ontop of each other. They cannot be brought into a grid structure since i wanna keep the randomness they have and i prefer not to simply retry the spawn process if it happens to land onto a other area since that process could go about happening several times.
-----------------------------------------.
So the way I've figured out would work is if i could remove the areas from the spawn area...but i have no idea how to do that.
---------------------------------------.
(removing the blue spots from the red areas in the picture)
----------------------------------------.
any help or ideas are appriciated, thanks.
Answer by thoxey · Jul 21, 2018 at 05:49 PM
There is a good link here if you look at the first section about room that'll explain this next part beter than I can,
But basically create a box collider of a pseudo random size, (i.e. in some bound you desire),
Then repeat that again but if the colliders overlap (see: here for how to do this) then try again.
Set a finite number of attempts and then leave the loop early if you have an amount of rooms you're happy with.
Then obviously delete the collision objects when your done with them to address the "removing the blue spots from the red areas" if I've understood what you mean
Thanks pal, I've been trying to figure out strange algorithms all night i didn't consider using colliders to do it :P really owe you one
Your answer
![](https://koobas.hobune.stream/wayback/20220612171218im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
AI placement of ships (battleship game) 1 Answer
Random Number every 5 seconds 2 Answers
Help with Randomized Sprite on GUI button click 0 Answers
Randomly generate blocks on a flat map 0 Answers