Hello everyone! This is my first tutorial but I’ll do my best to explain everything clearly. You can check whole package in asset store after some refactor and more generic from this tutorial here!

https://www.assetstore.unity3d.com/#!/content/116503 .

As for difficulty of this tutorial I’d rate it as intermediate, but I think everyone with good understanding of coroutines and unity’s functions’ execution order should understand it 🙂

So main point in this tutorial is generate random environment without overlap based on objects actual collider. To achieve that its necessary to wait for physic engine update to know whenever objects enters any other.

The key in this point is understand how unity physic engine collision is invoked/checked. Take a look at the graph from official documentation . Physics update is invoked just before WaitForFixedUpdate and there isn’t any way to force it to update.
So the only way to check if two newly instantiated objects are colliding is to wait for physics engine update (WaitForFixedUpdate). We can achieve this via coroutines. If you are not familiar with them, check out this great tutorial https://www.alanzucconi.com/2017/02/15/nested-coroutines-in-unity/

So let’s get to the point and start scripting. First we are going to write simple method handling spawning objects using coroutine with some parameters.

Drag any prefab to script’s inspector window and look how it works.
So next thing is allow objects generate some collision. As you already know in most cases is rigibody component needed to achieve collisions. Lets check what official documentation can told us about it https://docs.unity3d.com/Manual/CollidersOverview.html . So Rigidbody Trigger Collider is invoked in every case so lets use that case. But not every object we placing has trigger or even rigidbody component. option there is create an template from prefab, modify it to be sure physics is active and allow us checks collisions. Create simple mono class which on awake care to active physics on template for us.

CheckForCollisions class will on awake grab every collider attached to this prefab, set them as triggers and if object doesn’t have rigidbody add it to object and be sure to set it as active.

Next use that template in our SpawnObject function.

We already not checking any collisions yet but check possible mistake there, if we have mixed list of few prefabs to add some randomization, we Instantiate and destroying objects with every object placed. As everyone should know its very expensive and its important avoid they if there not necessary. So lets create new method which calls and Run each coroutine for each prefab. We can use parallel coroutines to achieve that.

Next step is actual collisions detection after putting new object on scene. We would check collisions on template so lets mark it on CheckForCollisions class. Since coroutines cant return any value and we need access from ObjectSpawning class to check result . On the end i see it also can be just a normal CheckForCollisions class variables but i leave it to your choose how do you write that 🙂 I create simple class and cache it in CheckForCollisions class.

Next let’s pass desired position and rotation to template. I made it passing my data class. To wait for physic update which invoke our OnTriggerEnter event is necessary use coroutine which would wait for fixed update as i mentioned in prologue (if doesn’t check it yet just look at unity’s functions’ execution order). Let’ do that.

And now run coroutine synchronous from SpawnObject method. We achieve that with yield return and start new coroutine object (yea, StartCoroutine method returning coroutine object which is pretty useful in some cases, we ll use it later when we trying notice when spawning objects were finished).

Now check if its working. I generated something like this. As you can see any object should not collide any other. Its prefect point to work with it 🙂

Lets refactor this and made it generic to work with some other cases.

Now with passing randomize delegate method allow us to change picking random position and rotation values. Lets use it with some terrain data.

As you can see we obtaining point on terrain with ray cast at specific layer. Remember to set your terrain layer to proper one. In my case its a Terrain. At this point we should generate some like that cute environment.

Hmmm or maybe not, something more realistic, fix it with alignment to terrain normals and move to next point !

Great ! Next thing is spawning objects without colliders, to place they there is not necessary to wait for physic engine update. It ll be simple one.

Now let’s hold for a second on real physic object with rigibody component actually attached in it. If we spawn them and leave it alone they would start falling down. I can occurs some mistakes in our collisions checks. Lets fix that case. To do that it ll be great to notice them when placing objects end.

So we write simple locking and unlocking physics methods in new mono class. Next thing is invoking event in right time. Caching corutines that way allow us yielding since each started coroutine finish. We using Coroutine object which I mentioned before.

At spawning physic object we attach that simple component only to objects were have rigidbody within. Also we can attach it before to objects and checks for it but i leave it to your choice. Main purpose of  that class is listening On Complete event to unlock physic, since we create it to invoke event we sets them as kinematic to be sure to keep them in same spot we spawn it.

So now we finished with terrain, i leave you with it so put some prefabs into inspector and create ! Last time I end up with this :

If you done with terrain let’s try another example, an planet from a gif. Since we can randomize our method lets just write new randomizer for mesh.

Pass new randomize method to spawn object function and try it.

Of course all physics object now just fall down , but we created class to hold them right ? Use it wisely. Let’s edit PhysicsObject class to work with our planetoid gravity system.

So that’s it  ! Thanks for your time. If you found my tutorial useful leave me a comment !

Tell me what do you think in comments on reddit, below or even on mail 🙂 See you soon !