Unity 实现物体破碎效果(转)
感谢网友分享,原文地址(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 实现物体破碎效果(转)相关推荐
- Unity 实现物体破碎效果(转)
感谢网友分享,原文地址(How to Make an Object Shatter Into Smaller Fragments in Unity),中文翻译地址(Unity实现物体破碎效果) In ...
- unity 实现物体破碎效果的一些方法 - 细雨淅淅
游戏越来越接近现实的感觉,如果有一个真是的 虚拟现实设备,可能我们真的会感觉是在真实世界.场景的逼真是在渲染效果.角色AI.游戏逻辑.物理效果等等一起导致的结果.现在游戏越来越大,除了渲染,物理估计是 ...
- Unity Shader·屏幕破碎效果
Unity Shader·屏幕破碎效果 前言 最近在做一个新的MMD(用Unity来实现),其中用到了一些好看的渲染技术在这里分享一下. 视频链接 https://www.bilibili.com/v ...
- [教程] 在Unity中制作物体破碎效果
这篇教程将教大家如何在Unity中制作一个简单的碎片效果.当物体撞击或销毁时,我们将物体分裂为更小的碎片来取代之前的仅仅直接"删除"物体. 需求 这篇教程需要最新版本的Unity, ...
- Unity物体破碎效果
物体破碎的效果在游戏中非常常见,在本文中将实现任意大小的长方体的敲击破碎,并为实现更复杂物体的破碎效果做好铺垫. 最终效果图如下: 为了实现这种破碎效果,我们必须生成碎片物体,在本例中 ...
- unity 3d物体描边效果_从零开始的卡通渲染描边篇
序言: 一直对卡通渲染非常感兴趣,前后翻找了不少的文档,做了一些工作.前段时间<从零开始>的手游上线了,试着渲染了一下的其中模型,觉得效果很不错.打算写一个专栏记录其中的渲染技术.在后面的 ...
- unity 陀螺仪 物体旋转和移动效果
unity 陀螺仪 物体旋转和移动效果 直接上码 带注释 public class SDKGyroController : MonoBehaviour {//陀螺仪是否存在class GyroGame ...
- Unity 实现自定义图片破碎效果-2D_Destruction
Unity 实现自定义图片破碎效果-2D_Destruction 导引 效果预览 源码下载地址 实现流程 1.添加SrpiteRenderer组件 2.添加Explodable组件 3.Polygon ...
- Unity 控制物体移动的一些方法
Unity 控制物体移动的一些方法 开坑, 回头慢慢补. 移动方法的总结. 1, 直接+=Vector3 transform.position += Vector3.forward * moveSpe ...
最新文章
- P4145 上帝造题的七分钟2 / 花神游历各国(线段树区间开平方)
- py文件 添加模块映射_Python模块的定义,模块的导入,__name__用法实例分析
- Android中AIDL的使用详解
- 支持高并发的IIS Web服务器常用设置
- python3.4用循环往mysql5.7中写数据并输出
- 如何基于Redis Replication设计并实现Redis-replicator?
- 如何root安卓手机_你的手机你做主!免 ROOT 卸载安卓手机预装APP
- micropython lcd_MicroPython动手做(05)——零基础学MaixPy之LCD液晶屏
- [106].从中序与后序遍历序列构造二叉树
- spark的三种运行模式以及yarn-client和yarn-cluster在提交命令上的区别
- HTML5应用 + Cordova = 平台相关的混合应用
- C++中? : 运算符的使用
- 使用注解匹配Spring Aop切点表达式
- java 获取文件大小_阿里Java后端开发面经,面试官都替我感到绝望
- 【致青春】我们挥霍时间的年代
- AIX系统修改文件系统的方法
- 怎么做口碑营销?口碑营销有哪些方式?
- leetcode 678 有效的括号字符串
- 通过Element开发基础增删改查页面——Vue项目实战(三)
- QT中常用的输入控件
热门文章
- 信息系统项目管理师论文_「高项论文」谨防信息系统项目管理师论文12个写作丢分点...
- 数组名与函数的结合使用注意项
- vs2017编译QT with ssl
- C++ STL容器之map 简单使用
- C++ 异常基本语法
- c语言中 %.2s,C2S是什么意思
- Linux的SOCKET编程 简单演示
- 非阻塞connect
- 我在美团Android研发岗工作的那5年,含泪整理面经
- 恶意软件伪装“正规军”,撕开Booster Cleaner“画皮”下的真相