How to setup complex connected structure using prefabs

In this How To you can learn how to use prefabs in complex connected structures using RigidRoot component. Rigid component with MeshRoot object type can only manage simulation of its Mesh objects (objects with MeshFilter component ) children and it is not suitable for complex simulations which may involve hundreds of fragments and several prefabs. In such cases, it is better to use RigidRoot which can have not only Mesh objects as its children, but other roots with Mesh objects. This makes complex setups much easier and faster to manage, since these roots can be Prefabs with fragments.

1.	Create Cube, this will be a ground cube that will be used for collisions.

2.	Set its name to "Ground", position to [0,0,0] and scale to [20, 1, 20]

3.	Create Cube, set its name to "Slab", position to [-3, 2.5, 3] and scale to [3, 0.1, 3]

4.	Add RayFire Shatter component to "Slab", set amout to 100 and click on Fragment button. 

Slab_root object with fragments will be created. Now we need to save it as prefab. In order to do this, we first need to export fragments meshes into an asset file.

5.	At the bottom of RayFire Shatter component click on Export Last Fragments button and Save asset in Assets folder.
	
6.	Now select Slab_root object in the Hierarchy and drag it to the Project tab into the Assets folder to create Prefab.

7.	Destroy "Slab" object.

8.	Create Cube, set its name to "Pillar", position to [-4, 1.5, 4] and scale to 
[0.2, 2, 0.8]

9.	Add RayFire Shatter component to "Pillar", set amout to 50 and click on Fragment button. 

10.	Export Last Fragments and create Prefab for Pillar_root object like in steps 5 and 6, then destroy Pillar object.

11.	Create Empty game object, set is name to "Structure" and position to [-1, 0, 1]

12.	Set it as parent for Slab_root and Pillar_root objects.

Now we need to duplicate and move these prefabs to create complex structure.

13.	Duplicate Slab_root object, set position for new object to [1, 2.5, 2] and rotation to [0, 90, 0]

14.	Duplicate Slab_root object again, set position for new object to [-2, 2.5, -1] and rotation to [0,-90,0]

15.	Duplicate Pillar_root object, set position for new object to [-3, 1.5, 0.5]

16.	Duplicate Pillar_root object, set position for new object to [-3, 1.5, -2]

17.	Duplicate Pillar_root object, set position for new object to [-1, 1.5, 3]

18.	Duplicate Pillar_root object, set position for new object to [2, 1.5, 3]

19.	Duplicate Pillar_root object, set position for new object to [-1, 1.5, 0.5]

20.	Duplicate Pillar_root object, set position for new object to [2, 1.5, 1]

21.	Duplicate Pillar_root object, set position for new object to [-0.5, 1.5, 3.2] and rotation to [0, 90, 0]

22.	Duplicate Pillar_root object, set position for new object to [-0.5, 1.5, 1] and rotation to [0, 90, 0]

So, we finally create one floor of our structure. Now let's duplicate the whole floor and create a few more floors.

23.	Select all Structure children objects, duplicate selection and move it so bottom fragments of duplicated Pillars will be slightly upper than Slab_root objects and there will be very small gap between them.

Now we finally finished with creating complex structure using two different prefabs and can start setup RigidRoot and other RayFire components. 

24.	Select Structure object, add RayFire RigidRoot component and set Simulation Type to Inactive.

First, let's solve any possible simulation issues we may have.

25.	Start Play Mode.

You may notice that some fragments move and rotate immediately after the simulation starts. This happens because some of them interpenetrate with each other, so their colliders interpenetrate as well.

To fix this, we can make such fragments ignore each other. This obviously reduces simulation accuracy, but it is the cost of stable simulation.If you do not want interpenetrated fragments to ignore each other, you should model your structure in such a way that there won't be interpenetration. In this howto such interpenetration were made on purpose to show you how to fix them.

26.	Turn Off Play Mode. 

27.	Select Structure object and in its RayFire RigidRoot component Physics properties enable Ignore Near property.

28.	Start Play Mode.

Now all fragments do not move and rotate at start and we can setup Collapse for this tructure.

29.	Turn Off Play Mode. 

30.	Select Structure object, add RayFire Connectivity component and set Type to By Bounding Box and Polygons. This connectivity type will establish connections among fragments based on their matching shared surfaces to set the correct shared area size and then create connections for the rest of the fragments based on their overlapping Bounding Boxes to create connections among different prefab because their fragments do not have matching surfaces.

31.	Under Connectivity Type property set Expand value to 0.05. This property increase fragments bounding box size to make sure they will overlap in case there is some gap between fragments.

32.	In RigidRoot component Activation properties enable Connectivity checkbox so fragments will be possible to activate by Connectivity when they will lose connection with any of Unyielding fragments.

33.	In Activation properties set Offset property value to 0.1 so some fragments will be activated if they are pushed far enough by already activated fragments.

34.	Select Structure object, add RayFire Unyielding component, set Size to [10, 1, 10] and Center to [0, 1, 0] 

Now when you will start Play Mode Connectivity component will establish connections among all fragments to activate fragments which will not be connected to any of fragments defined as Unyielding by Unyielding component gizmo, this allows to avoid fragments frozen in air without any support. 

Structure is ready to be demolished, you can shoot, explode or throw some heavy objects to it to demolish. But lets use Connectivity component Collapse feature. Collapse allows to activate (turn to dynamic) fragments and remove their connections with other fragments. 

It is possible to instantly activate fragment and destroy all its connections using By Size or Random collapse types or destroy only connections using By Area collapse type, so when fragment will be activated when it lose all its connections with other fragments.

35.	In Connectivity component under Collapse proeprties set Initiate to At Start

36.	Open Collapse Properties, set Type to By Area, Start to 0 and End to 50. This value means that Collapse will start to destroy connections among fragments with smallest shared surface area and when it will stop 50% of connections with the smallest area will be destroyed.

37.	Set Steps to 30 and Duration to 15. This mean that whol collapse will take 15 seconds and it will perform 30 steps to destroy 50% connections slowly increase Area threshold with every step so more and more connection will be destroyed each step.

38.	Start Play Mode.

You may notice that every Pillar has the same collapse pattern, at every step the same fragments activates on every Pillar. This happens because every Pillar has the same fragments with the same shared area. To fix this we need to add some variation.

39.	Turn Off Play Mode. 

40.	In Collapse properties set Variation to 50. This property very useful if you have duplicated prefabs in your structure or if you have several of the same structures and you want to get slightly a different collapse pattern for every structure.

41.	Start Play Mode.

Now collapse of very Pillar and Slab is slightly different. But still it is better to have at least two different prefabs fragmented by Shatter with different seed so it won't be noticeable that all prefabs has the same fragmentation pattern. 

42.	Turn Off Play Mode. 

With a high number of connections, it is hard to see sometimes collapse simulation so you can disable connections preview in Connectivity.

43.	In Connectivity component toggle Off Show Connections and Show Nodes buttons.

We finished with our Structure setup, but every time we start Play Mode Connectivity component has to establish connections among fragments every time and RigidRoot has to add Mesh Colliders, set physical materials to fragments and define ignore pairs for colliders.

Depends on amount of fragments this process can be pretty resourceful, in order to avoid making such calculations every time you can perform them just once in Editor so all necessary data will be cached in Connectivity and stored in Prefab. In this way, you can instantiate whole structure in runtime and it will not make all these resourceful calculations so there won't be FPS drop. 

44.	Select Structure object and in RayFire RigidRoot component click on Editor Setup button on top.

After this you will see amount of shards under the button. It is better to use Editor Setup only after you finished with tuning all properties because Editor Setup cache a lot of physical and connection properties so they won't be calculated in runtime, if you will change some properties after this they won't be used because cached data will be used. 

So, if you decide to change some properties later, it is better to click on Reset Setup button on top first, change properties and then click on Editor Setup again.

Now after all data is cached you can save whole structure in prefab, so later when you will instantiate Structure prefab in Runtime it will only need to add objects in simulation and use already cached data.

45.	Select Structure object in the Hierarchy and drag it to the Project tab into the Assets folder to create a Prefab.