Hello, this time i want to show You small piece of my project that i am actualy working on. I hope to post many worthwhile pieces about my projects like this one.
Since I needed to create environment filled with randomly placed props I’ve studied few ready solutions but none of them suited my needs. Main problem with solutions like PopulateField was that it used only primitve shapes for collision detection and it wasn’t accurate for complex objects.
If you follow reddit /Unity3D/ tag, you may see it before. I’ve got really nice feedback there and over 400 upvotes (yay!), so i hope you like it even more 🙂

In my solution I engaged Unity Physic build system (PhysX) for accurate collision detection. All collision checks are based on template created from object prefab. Every prefab have just one template.
Main problem there, with usage of build in collisions system is that, physics is calculated only in fixed timesteps, and there isn’t any possibility to force it to update. So here’s what I came up with. I’ve used nested Coroutines to solve that problem. I’ve created class allowing physic interactions and add it to template whenever its created. As i mentioned before, template allows to prefect check if position is free. Check randomly picked position and rotation were made by moving template to that spot. Class added to template, contains corutine, whitch allow wait for physcis engine calculations. It supports multiple colliders even in childs gameobjects so it allows build every shape.
Coroutines usage  disclaims generate objects in one frame but it’s not nessesary for my project purposes. Since coroutines cant return any values, to hold values beetwen classes/methods I created simle class and pass it as parameter. This class holds all data contains informations about object placement and state of each try. 

Any object isn’t spawed/pooled before exacly known if picked possition/rotation is free to prevend unnessesary Instantiating and Destorying. Simple little objects without colliders like branches, apples or grass whitch can been overlaped do not need to wait for any calculations.

Few runs with same settings and objects.

For purposes of my project I’ve use flat plane as a terrain but i expand method to usage the terrain component and align objects to point height and normal of the ground. With some rules it’s looking pretty good.

Nearly realistic tree behaviour 🙂

Next is possibility to spawn objects that will use Rigidbody. Rigibody objects cant be just spawn and leave it yourself. In case where they falling or moving they should create an  position checks errors. To prevent that, whenever rigidbody objects is placed, I’ve adding simple class to controls whether physics affects the rigidbody. That class too care about enabling physics with listener to event whenever generating objects is done.

Becouse we run serval corutines parralery its not exacly know whitch finishes as last and when invoke an event. Soultion to that is with usage of returned at StartCoroutine object type of Coroutine. With it, its possible to query the state of the runned coroutines and to wait for they termination.

Earlier i forgot to notice you that static objects on map that we want to stay in same place are supporedt too and were not been overlapped any way.
Afterwards lets see how control over objects placment works. Since i’ve used a terrain as a … terrain :), its possible to check major texture in picked point. Acualy you can assign prefabs to used textures. In that example rocks, trees are assigned only to grass texture, little objects like branches, apples do nto chceck texture type.



Short conclusions :
– Actualy someone on reddit ask me why its so slow. I dont think its slow, i am generating nearly 800 objects with 50 max tries for each prefab in 1.4s – 1.6s . You always can adjust it by max possible tries and objects count attributes. One way either is simplify algorithm and firstly check position with raycast or multiple raycasts but it take off posibility to place new objects under others;
– For my purposes not ‘one frame’ generating is enough, since i can create in on runtime with some distance before player or on level loading;
– Whole system is prepared to use object pooling;
– Want to update system to working with mesh terrain (like low poly terrains), based on mesh vertices. It enlarge system usage (place objects procedurally on ‘planets’ for example).
Thats it for now, it takes quite a long time but i hope i didnt bore you to death 🙂