Simple simulation of destruction using Unity and Blender

Hello! recently wondered about destruction in Unity, searched the Internet for what is on this topic, but alas, I found only a bunch of questions and discussions.



And no, I am not claiming that my article is one of a kind, because the method is quite simple, but for some reason I did not find a direct tutorial for this.



Let's start with the fact that in Unity you CANNOT DESTROY / SEPARATE / CUT an object, our destruction will in fact not be destruction at all, but only their visual imitation, since, as I said above, it is impossible otherwise.



OWN!



Stop pouring water and wasting your time (I still saw fit to leave the preface). First, you need to download a program called Blender from the Steam store, after which we go in and start to understand.



We create a new project, and in the โ€œEdit / Editโ€ tab, look for the โ€œAddonsโ€ button, and write Cell Fracture in the search, then put a tick next to it to enable it.







Then we go to our scene itself in Blender, and we see there the cube that is there initially when creating the scene.



We are interested in the "Modeling" tab, go into it, right-click on the object> subdivide, and we see that the cube has become, as it were, a "cell"







Now go back to the first tab "Layout", click on the button object / object> fast effects / fast effects> Cell Fracture, and we will open a window for managing the Addon.







Just press the "YES" button and watch the result.



As you can see, our cube is divided visually, so everything went well, this is the very fragmentation.







The last steps are left in Blender, and you can proceed to creating a scene, click on file / file> export / export> .fbx, and save it to a folder convenient for us.







We open Unity, create or open an old project, and start actually assembling our scene. To do this, we need to create a plne and a sphere, now from our folder to the hierarchy window we add the cube that we just exported, then drag it directly onto the scene.



In order for Unity to understand that this is a physical object, you need to add two things, click on our cube in the hierarchy window, thereby expanding the list of objects inside (the parts of which it consists), then holding shift, select the very first object from the list, and the very last, with this manipulation we will select the entire list.



It remains to click on the AddComponent, and in the search having registered the collider, select "Mesh Collider".







In the collider itself, click on "Convex" to display it, and that's it.



Now we repeat what we did earlier, but we are only looking for the Rigidbody.







You need to do the same with the sphere, only without the collider, because it hangs there initially.

This concludes our preparation, and we can move on to the most interesting, as they say - "brevity is the sister of talent", which is why we create a new C # script in the hierarchy window, and write the following there:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FragmentSplit : MonoBehaviour {

	public bool isdead = false; //    ,   
	public float timeRemaining = 100;//        (   )

	void Start()
	{
		GetComponent<Rigidbody>().isKinematic = true;//          
	}

	void OnCollisionEnter(Collision collision)//    
	{
		    GetComponent<Rigidbody>().isKinematic = false;//    - ,      
		    isdead = true;//  ,        "",    
	}

	void Update()
	{
		if (isdead)//  ,    
		{
			timeRemaining -= Time.deltaTime;// 			

			if (timeRemaining < 0) //     ,  
			{
				Destroy (gameObject);//  
			}
		}
    }
}


Now, for the last time, select all the objects in the list, click on the AddComponent, and add our script.







Raise our sphere above the cube and turn on the project.







As you can see, everything works fine, the cube breaks, our sphere is rolling somewhere.



That's all, yes, perhaps you will say that you can make a smoother removal so that the pieces disappear, and not so clumsy, and all that.



I will only answer you that I just demonstrated how the script works, and did not go into the details of how and when it will remove these "debris", but simply showed that it is possible and so, and there is absolutely nothing complicated about it.



I do not argue that assets from Unity (or other developers) will be better than my scripts by a couple of lines, but still.



I did not do this article with an emphasis on the fact that I will be read by people who know and understand Unity and the language, who can write a script with more precise control of everything that happens to the object on the stage, I made an article for students and beginners.



I won't say that I know the language well and thoroughly, but this is not the first day in this, in general, that's all for me, I hope that I helped someone, good code for you friends! ..



All Articles