Unity for object Breaking effect

Source: Internet
Author: User

Thank you for sharing, the original address (how to make a object shatter into Smaller fragments in Unity), the Chinese Translation address (Unity to achieve object fragmentation effect)

In this tutorial I'll show you what to create a simple shattering effect for your Unity game. Instead of just "deleting" a crate (or any other object) while it is hits or destroyed, we can make it splinter into smaller Pieces.

Requirements

For this tutorial, you'll need the newest version of Unity, and some basic experience with it. For the most advanced effect later in the tutorial, a 3D modelling tool would also be necessary. If you don't have one available or don ' t want to model the objects yourself, I ' ve included them in the source downloads. (The Unity files themselves is also available there.)

In the basic version of this effect, a cube would be destroyed, leaving several fragments in its wake which would fall Reali Stically to the ground:

Later, we ' ll switch the cube for a more complicated barrel model:

You can try it out for yourself here:

Click to try the demo. (The cube demo is available here, too.)

Basic Setup

Create a new Unity project and open a fresh scene. Create a plane, which would act as our ground, and a cube, which would be the Destructible object. Also, place a directional light to make things more visible. Create new materials and assign them to the floor and the cube, so that we can tell them apart, and move the camera so That everything can seen:

Destroying the Cube

There is many ways to "destroy" the cube. For now, we'll take the simplest approach possible.

Create a new JavaScript file and name it destructionController.js . In this, we'll put all the functionality of removing the cube and creating the fragments. Add the following lines to it:

1234567 function  update ()   {        if ( Input.getkey (keycode.space))       {          destroy (gameobject);         }

Now, add the script to the cube by dragging it onto it. Start the game and make a test run. If You press space, the cube should is deleted.

After being removed, it'll also no longer appear in the hierarchy, as can see in the screenshot.

Creating the Fragments

Now create eight smaller cubes; These'll is the "fragments" of the current cube. Give them the same material as the cube. (Don ' t worry about the looks yet, we'll make a them look awesome later on.) They should look like this:

Stack all 8 cubes to form a bigger, single cube, without them intersecting:

Give every cube a rigidbody, set their mass to 22 , activate use gravity, and deactivate areKinemat IC. This would make the fragments fall down and use physics. If you want, you can tweak these values later to produce results that is better suited for your game.

Now, group the cubes under a new empty and call gameObject it remainsCube . When the original cube is destroyed, it'll be replaced with this new object made out of smaller cubes.

Drag the remainsCube object into the project folder to make a prefab out of it. Once it is safely in the Prefab folder, delete it out of the main scene, and it's ready to use.

Making the remains Appear

Add the highlighted lines to the destructionController script:

123456789 varremains: GameObject;functionUpdate(){    if(Input.GetKey(Keycode.space))    {        Instantiate(remains, transform.position, transform.rotation);        Destroy(gameObject);    }}

This would create a copy of the remains at the exact position of the cube. Afterwards, the cube would be removed, giving the illusion, the new one was actually the old one, but "broken".

To actually get the manually assign the the cube. Click on it, and in the Inspector should see a tab containing the destruction Controllerscript. There should be a slot called remains, which should currently is empty. Drag the prefab from the project folder to the this remains slot. The destruction Controllerscript in the " Inspector should now" look like this:

First optimizations

Make a test run! If everything is set to correctly, then when you press space, the remains should replace the cube. If you ' re lucky, they should then tumble to the ground.

So, this basic cube:

... should turn into something similar to this:

I wasn ' t Lucky

Sadly, it isn't guaranteed that the fragments would tumble in a nice. Fortunately, there is ways to solve.

Create a new empty gameObject and give it a sphere collider, but no rigidbody. Pull the remains into the scene and so on you can edit them. Add the Sphere Collider object to the remains, and place it for it intersects with some of the cubes:

Now, the fragments would immediately collide with the sphere, creating a tumble effect:

Removing the fragments

Depending on the game is building, we can ' t afford too many "splinters" at once in a scene. The straightforward solution is to delete them after a few seconds. In order to do, create a new JavaScript file and name it selfDestruct.js . Put the following code in it:

12345 functionStart(){    yieldWaitForSeconds(4.0);    Destroy(gameObject);}

When the object is created, it'll wait for four seconds, and then delete itself. ADD This code to the remains object. If you now destroy the cube and create the fragments, the remains would destroy themselves after four seconds.

And that ' s it! Now, the basics to efficiently has an object "shatter" in several smaller pieces when it is destroyed. You can use this effect as-is, but let's take it a little further and see how to use it with a more complex object.

Using an Actual Object Instead of Cubes

Now that we've got the basic system in place, we can make it more pretty by replacing the cubes with actual objects.

If you is adept in a 3D modelling tool, you can create your own objects. If not, or if you don't have a available, you can get the prepared 3D file from the source download.

Copy the file into your asset folder, and the 3D models would automatically be imported to your use. Before using them, click the file in the Asset Explorer and make sure that the source files is being imported CO Rrectly at a scale factor of 1 (not 0.1 or; that is only 0.001 complicates things).

If you are on the objects, you can see a field called Meshfilter in the Inspector. If you click it, you get a list of all available meshes in the project. Now replace all the cubes in the Cube remains with barrel parts.

The intact cube gets the barrel mesh; the smaller cube fragments need the meshes barrel_fragment_01 to barrel_fragment_08 . After those is assigned, set their local positions to (0, 0, 0) . (Their pivot-points has been set so, they can be easily zeroed on that.)

Instead of a Box collider, a mesh collider would is much more convenient. Remove all the box colliders in the fragments, and replace them with mesh colliders. Check each of the mesh collider and make sure each have the correct mesh applied (that's, barrel_fragment_01 needs barrel_fragment_01 the mesh, and so on).

Once that's done and set all mesh colliders to convex . (A Non-convex Mesh collider can ' t collide with other Non-convex mesh colliders. It ' s a programming thing.) Also, remove the Sphere collider we added to the remains, as it might isn't be necessary.

It everything is set up correctly, and you should has a barrel which would spring apart into eight smaller pieces.

Possible further Details

The same system can also is used to add other effects to the destruction. Does explosion? Add it to the remains! Add sounds, for a satisfying crack. Put a particle effect in there, creating a small puff of smoke.

Conclusion

In this tutorial I ' ve showed your the most straightforward-of-the-making of an object shatter into smaller fragments. Now your know how to destroy a object, removing it from the game; How to Swaps the object with smaller fragments directly before its destruction; And how to has the fragments self-destruct afterwards.

This system can-now is modified and adapted to fit many specific purposes. You could has a crate or a barrel splinter and shatter when shot. You could create a explosion after a plane are hit. Or you could has a boat crack into and a pieces. enjoy!

Unity achieves object breaking effect (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.