- Home /
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?
Answer by TowerOfBricks · Nov 26, 2009 at 02:59 PM
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.
Answer by Statement · Nov 26, 2009 at 01:45 PM
Make sure there is enough space to start with.
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.
Answer by Ash · Nov 26, 2009 at 03:53 PM
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!
Answer by torg · Nov 26, 2009 at 08:54 PM
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.
You can iterate through all the child objects and test which one is placed most to the right for example, or you can use the collider.bounds and figure out which direction is the longest, and from that generate a start and end value.
Regarding the layer mask. The best way is to define it in the editor, and make sure both the objects and the plane ground are set to different layers.
Answer by torg · Nov 26, 2009 at 03:14 PM
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 :)
Your answer
Follow this Question
Related Questions
HOW TO CHOOSE ONE OF THE PLAYERS AND TRANSFORM IT IN A PREFAB? 2 Answers
Issue with random gameobject enable / disable script. 1 Answer
how do I instantiate random gameobject every certrain meters the player moves? 1 Answer
Multiple spawn points without spawning in the same place. 2 Answers
Spawning an object at a random time | C# 2 Answers