感谢网友分享,原文地址(How to Make an Object Shatter Into Smaller Fragments in Unity,中文翻译地址(Unity实现物体破碎效果

In this tutorial I will show you how to create a simple shattering effect for your Unity game. Instead of just "deleting" a crate (or any other object) when it is hit 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 more advanced effect later in the tutorial, a 3D modelling tool will 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 are also available there.)

In the basic version of this effect, a cube will be destroyed, leaving several fragments in its wake which will fall realistically 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 will act as our ground, and a cube, which will be the destructible object. Also, place a directional light to make things more visible. Create two 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 be seen:

Destroying the Cube

There are 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:

1
2
3
4
5
6
7
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 be deleted.

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

Creating the Fragments

Now create eight smaller cubes; these will be the "fragments" of the current cube. Give them the same material as the cube. (Don't worry about the looks yet, we'll make 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 deactivateis kinematic. This will make the fragments fall down and use physics. If you want, you can tweak these values later to produce results that are better suited for your game.

Now, group the cubes under a new empty gameObject and call it remainsCube. When the original cube is destroyed, it will 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 is ready to use.

Making the Remains Appear

Add the highlighted lines to the destructionController script:

1
2
3
4
5
6
7
8
9
var remains: GameObject;
function Update()
{
    if(Input.GetKey(Keycode.space))
    {
        Instantiate(remains, transform.position, transform.rotation);
        Destroy(gameObject);
    }
}

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

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

First Optimizations

Make a test run! If everything is set up 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 is not guaranteed that the fragments will tumble in a nice way. Fortunately, there are ways to solve that.

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

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

Removing the Fragments

Depending on the game you are 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 so, create a new JavaScript file and name it selfDestruct.js. Put the following code in it:

1
2
3
4
5
function Start()
{
    yield WaitForSeconds(4.0);
    Destroy(gameObject);
}

When the object is created, it will 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 will destroy themselves after four seconds.

And that's it! Now you have the basics to efficiently have an object "shatter" into 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 are adept in a 3D modelling tool, you can create your own objects. If not, or if you do not have one available, you can get the prepared 3D file from the source download.

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

If you look at 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 meshesbarrel_fragment_01 to barrel_fragment_08. After those are assigned, set their local positions to (0, 0, 0). (Their pivot-points have been set so that they can be easily zeroed in that way.)

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

Once that is done, 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 not be necessary.

It everything is set up correctly, you should have a barrel which will spring apart into eight smaller pieces.

 

Possible Further Details

The same system can also be used to add other effects to the destruction. Do you have an 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 you the most straightforward way of making an object shatter into smaller fragments. Now you know how to destroy an object, removing it from the game; how to swap the object with smaller fragments directly before its destruction; and how to have the fragments self-destruct afterwards.

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

Unity 实现物体破碎效果(转)的更多相关文章

  1. unity 实现物体破碎效果的一些方法 - 细雨淅淅

    游戏越来越接近现实的感觉,如果有一个真是的 虚拟现实设备,可能我们真的会感觉是在真实世界.场景的逼真是在渲染效果.角色AI.游戏逻辑.物理效果等等一起导致的结果.现在游戏越来越大,除了渲染,物理估计是 ...

  2. unity 实现物体破碎效果的一些方法

    游戏越来越接近现实的感觉,如果有一个真是的 虚拟现实设备,可能我们真的会感觉是在真实世界.场景的逼真是在渲染效果.角色AI.游戏逻辑.物理效果等等一起导致的结果.现在游戏越来越大,除了渲染,物理估计是 ...

  3. Unity Shader实现描边效果

    http://gad.qq.com/article/detail/28346 描边效果是游戏里面非常常用的一种效果,一般是为了凸显游戏中的某个对象,会给对象增加一个描边效果.本篇文章和大家介绍下利用S ...

  4. Unity CommandBuffer物体轮廓

    1.command buffer具有很高的灵活性,它的作用是预定义一些渲染指令,然后在我们想要执行的时候去执行这些指令(见图1),绿点表示可以在"Forward Rendering Path ...

  5. Unity查找物体的子物体、孙物体

    Unity查找物体下的所有物体 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...

  6. three.js使用卷积法实现物体描边效果

    法线延展法 网上使用法线延展法实现物体描边效果的文章比较多,这里不再描述. 但是这种方法有个缺点:当两个面的法线夹角差别较大时,两个面的描边无法完美连接.如下图所示: 卷积法 这里使用另一种方法卷积法 ...

  7. unity 图片 粉碎效果 破碎效果

    效果: 点击按钮后: 这些碎片具有物理碰撞效果,下面会有隐形的支柱垫着碎片,n秒后支柱消失,碎片落下 当然你也可以控制生成的碎片,让他们从下而上一块一块地落下 插件源码: https://github ...

  8. 通过改变unity中物体的alpha值实现若隐若现的效果

    RawImage logo = mainLogo.transform.FindChild("back/headBack/Logo").GetComponent<RawImag ...

  9. 用Unity实现时间倒退效果

    记得以前看过一个电影,叫做<独立游戏大电影>,其中有个一个游戏可以实现时间回退的功能,可以像倒带一样,十分有趣.因此我就想着用Unity也实现一个类似的简单Demo,说不定哪天会用到. 效 ...

随机推荐

  1. 如何找回Oracle中system,sys用户的密码[转]

    Oracle中如果不知道system,sys用户的密码后可用如下方法找回: 首先以一个普通用户等入数据库: 在SQL*Plus中执行如下命令: SQL>connect/as sysdba (也可 ...

  2. NSNotificationCenter应用总结

    通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,例如 View 加载完后会触发 viewDidLoad.  Apple 还为我们提供了另一种通知响应方式,那就是 NSNot ...

  3. iOS本地推送与远程推送

    原文在此 分为本地推送和远程推送2种.可以在应用没有打开甚至手机锁屏情况下给用户以提示.它们都需要注册,注册后系统会弹出提示框(如下图)提示用户是否同意,如果同意则正常使用:如果用户不同意则下次打开程 ...

  4. 网络初见&网络监测

    这里需要下载一个第三方 Reachability-master 大家可以百度一下 下载之后把 Reachability拖进来 具体代码如下 #import "ViewController.h ...

  5. 不修改Xcode项目加载Reveal

    关 于iOS UI调试工具Reveal的配置,很多初学者朋友可能在网上搜索到一些文章,这些文章大部分都是讲述了如何通过配置Xcode项目,通过加入一些库文件, 并且在程序中编写额外的代码来调用Reve ...

  6. C#中方法的参数的四种类型

    C#中方法的参数有四种类型:       1. 值参数类型  (不加任何修饰符,是默认的类型)       2. 引用型参数  (以ref 修饰符声明)       3. 输出型参数  (以out 修 ...

  7. github代码上传之命令提交

    Git GUI的用法比较简单,随便弄弄就可以将本地git库中的代码提交到远端github服务器,所以想把Git bash这玩意儿的操作流程快速过一遍,主要是做个笔记,以后忘记了可以看看怎么操作的. 首 ...

  8. visual studio生成后调试启动又提示部分项目需要生成问题总结

    长久以来若干个项目都遇到过类似的情形,已经成功生成的项目启动调试或者再生成依然认为部分项目需要生成而不是跳过.总结以往的经验,记录下来,以便大家遇到时处理. 若有多个项目提示需要重新生成,优先检查被依 ...

  9. SQL Server开发接口生成方法

    为提高开发效率,生成固定格式的接口是必须的,以下以提供新增/修改/删除/读取接口为例: 以常见的表结构为例,特殊表结构可自己尝试去调整方法 主要通过系视图 sys.columns生成方法:为包含列的对 ...

  10. Windows下用Codeblocks建立一个最简单的DLL动态链接库

    转自:http://blog.csdn.net/wangwei_cq/article/details/8187576 来源:http://hi.baidu.com/hellosim/item/9ae4 ...