# Avoiding overlap between randomly placed object

Is there any smart and efficient methods that could be used to avoid collision and overlap between gameobjects generated and placed during runtime and already existing objects in the scene?

Take a look at Physics.CheckSphere: http://unity3d.com/support/documentation/ScriptReference/Physics.CheckSphere.html/ Physics.CheckSphere

First you generate a random position, then you use CheckSphere to find out if there is an object there already, if it is, generate a new position, if not, place the object there.

This won't look artificial and the objects wont overlap.

Also, if you're instansiating alot of object inside a volume you can use cells to place objects semi-randomly.

Imagine you've got a grid x*y*z large, and each grid might be like 10x10x10 units. Then randomly select n amount of cells to generate items into, and randomize the position of the object inside that cell. This will greatly aid instansiating objects somewhat randomly that won't overlap as long the object fit inside the cell. Remember you still have to make sure that none of the objects extents (bounding box) pass over to neighboring cells.

In a dense grid, this effect will look very artificial but in a sparse grid this is a good approach.

Note that if you register existing game objects as occupants of cells, then you'll also gurantee that you aren't instansiating objects into preexisting objects.

I used Check Sphere and it works fine, make sure you have an exit strategy if somehow there is no space to place the object, avoiding an infinate loop!

I've had some time to play with this now and must say I'm a little lost with how this exactly works. I'm using Physics.CheckCapusle as a capsule like shape is more in line with my objects, which are sometimes long and sometimes not.

static function CheckCapsule (start : Vector3, end : Vector3, radius : float, layermask : int = kDefaultRaycastLayers) : bool

I'm assuming that the "start" vector would be the position of one end of my object and "end" the other. The radius is self explanatory, and as I understand it the layermask is an option to ignore collisions with objects on said layer? My object are being placed on a terrain made of a planes so naturally I put these planes on this layer, which gives me only false results (even if the object are clearly colliding. And if I don't set this layermask I get only positives regardless of object collision. Could anyone point me in the right direction? :) Btw. Each object is made up of several children, sort of on a row of different length, so the way I figure out start and end positions is through the position of the first and last child object.

Thanks for some excellent suggestions. Already using a grid system of sorts, but the objects in question are fairly large and of different sizes so its difficult to fit them perfectly into a grid without making the spacing excessive. I'm also giving them a random rotation, which makes things even trickier, but the Physics.CheckSphere looks like a good solution to my problem :)