- Home /
Find open position
I would like to instantiate a new object at a point that is not occupied by another object. Is there an existing method for acheiving this?
This depends entirely on several things that you haven't told us- for example, is there a finite number of positions that objects can occupy? Are all the objects the same size and shape? Do the objects have colliders? Is there a limit to the area in which they must spawn?
Thanks syclamoth. This is the same as any game with a base that creates vehicles. The objects are of variable size and shape. They have rigid bodies now, but can have colliders. There is not a finite number of positions that they can occupy - theoretically anyway. There is not a limit to the area in which they can spawn.
Objects will spawn around a central object (base) on a plane, although I may change that to a terrain. So they should spawn as close to the central object as possible without overlapping another object excluding the central object (they may be spawned inside the base and then move to an open position, but I'm ok with spawning the at the open position for now).
Answer by syclamoth · Mar 12, 2012 at 04:05 PM
Finding an 'open space' can be quite difficult, as there are a lot of things to take into consideration. One way of doing this would be to use Physics.CheckSphere on random points until an empty one is found- however, this isn't guaranteed to produce a result in a finite time, and neither does it find the closest available spot (which, I should mention, makes things even harder).
If your objects have rigidbodies and colliders, you can just spawn it in any location, and let the physics engine sort out the arrangement of the objects. If you're using some kind of pathfinding / crowd simulation, you could adapt that to this problem as well.
Basically, you can use Physics.CheckSphere to make sure that there are no colliders in an area, but it's hard to know exactly what points you need to check.
Your answer
