I have multiple objects spawning randomly in my game, but sometimes they spawn inside of each other. I would very much like to know how to make it not happen.
Here is one of my object spawning functions, all of the others are very similar.
You would need to check the spawn area for other objects and only spawn if it is clear. If your objects have colliders on them you can use Physics.CheckSphere to check the spawn area.
To add to what Gibbonator said, you’ll want to check specifically that there are no other ‘rocks’ in the spawn location.
The collision sphere may detect other objects that could prevent a rock from spawning.
You can do this by checking the tag of each item returned in the collision sphere’s radius, and if there is a rock detected, don’t spawn.
Make sure to tag your rock prefab as a rock though.
sorry to hijack this, but is there anyway of doing something similar with rectangle shapes?
same setup having multiple cubes spawn random across a space, but in there i need a walkway to be clear of object… but the capsule or sphere check destroy to much…
k so if i understand it right, you use a spherecheck to catch objects and then check, for example their position, if it lays inside the box collider bounds?