How to destroy moving structure.

Sometimes you may need to destroy prefragmented structure which parent is moving, one of the possible issues is that after demolition fallen fragments may continue to move because of the moving parent. In this How To you can learn how to deal with this issue and in which cases it may occur.

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

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

3.	Create another Cube, this will be our moving platform.

4.	Set its name to "Platform", position to [-7,6,0] and scale to [7,1,3]

5.	Add Rigidbody component to Platform, enable Kinematic checkbox and set Collision detection to Continuous Dynamic. We need this so fragments which will fall to platform will collide with it and will be moved by friction.

6.	Add TranslateScript component to Platform, you can find it in 
Assets\RayFire\Tutorial\Scripts folder. 

This script provides two ways to move object. First one uses Rigidbody.MovePosition() method in FixedUpdate() to move Platform. Second uses Transform.Translate() but in this case, dynamic objects on the platform will not be moved by platform because, from Physics point of view, it will be teleportation between two frames and not physical movement from one point in space to another.

In the first example we will move Platform using Rigidbody.MovePosition() method. 

7.	Create another Cube, set its name to "Wall", position to [-7,10,0] and scale to [5,7,0.2]

8.	Add RayFire Shatter component to Wall, set Amount to 100 and click on Fragment button. Wall_root object with all fragments will be created.

9.	Destroy Wall object, we do not need it anymore.

10.	Set Wall_root as child for Platform.

11.	Add Rigid component to Wall_root, set Initialization to At Start, Object Type to Mesh Root and Simulation Type to Kinematic.

By this moment we created prefragmented wall which will be moved with our Platform. Now lets break it while it will be moving.

12.	Create Cube, set its name "Block", position to [0,10,0] and scale to [2,2,2]. This will be the object which will collide with our moving Wall and break it.

13.	Start Play Mode. 

As you see, Wall fragments do not collide with Block for now. This is because Wall fragments and Block are kinematic and kinematic objects do not collide with each other. To make them collide and fall we need to Activate fragments to make them dynamic a moment before they collide with Block.

14.	Turn Off Play Mode. 

15.	In Wall_root Rigid component Activation properties enable Activator property.

16.	Select Block, add RayFire Activator component, set Type to Box, Size to [1.2, 1.2, 1]

17.	Start Play Mode. 

Now Wall fragments activate a couple of frames before collide with Block and fall down to Platform. Some fragments fall to the Ground and stay there. Now lets fix frozen upper fragments which are not connected to bottom fragments anyhow but still continue to move with Platform.

18.	Turn Off Play Mode. 

19.	Select Wall_root object and in Rigid component Activation properties enable Connectivity checkbox.

20.	Add RayFire Connectivity component and set Type to Polygons, this type works good for fragments created by Voronoi fragmentation type.

21.	Add RayFire Unyielding component, set its Size to [5,1,1] and Center to [0,-3,0] so it will overlap bottom Wall fragments by its gizmo.

22.	Start Play Mode. 

Now you can see that upper group of fragments turns to Connected Cluster when it loses all connections with bottom group of fragments. 

23.	Turn Off Play Mode. 

Now lets try different type of moving. This time we will use Transform.Translate() to see the difference.

24.	Select Platform and in Translate component change Move Type to Translate.

25.	Start Play Mode. 

Now you may notice that fragments which fall to the Ground continue moving with Platform. This happens because they are still children of Wall_root object, which is child of platform. To fix this you need to reparent them after activation to some other steady object.

26.	Turn Off Play Mode. 

27.	Create empty game object, set its name "RayFire Man", and add RayFire Man component to it. Every time you start play mode RayFire creates object with RayFire Man automatically with default settings, but if you create it manually, you can edit default settings and this RayFire Man component will be used.

28.	Create another empty game object and name it "Steady_root"

29.	Select RayFire Man and define Steady_root object in Activation Parent field. This will force all activated fragments to change their parent to Steady_root object.

30.	Start Play Mode. 

This time fragments fall down to the Ground and stay there but also you may notice that fragments that fall to the Platform just slide over it and then fall to the Ground. This happens because for Physics Transform.Translate() simply teleports platform between frames.