How unity achieves object crushing effect

Source: Internet
Author: User

Gaming is getting closer and closer to reality. If there is a real VR device, we may feel like it is in the real world. The fidelity of a scenario is the result of rendering effects, role AI, game logic, physical effects, and so on. Now the game is getting bigger and bigger. Apart from rendering, physical estimation is part of the best performance. In action games, the destruction and crushing effects of Objects make the battle more vivid and smooth. This article mainly introduces the implementation of the crushing effect from the perspective of its own application.

1. Simple implementation

You can use 3D tools such as Maya to create an object composed of fragments. When an object in the game is broken, the original object is destroyed first, and then replaced with the pretab consisting of fragments, the effect is that each part has different loading methods and needs to be adjusted. See http://www.cnblogs.com/zsb517/p/3986278.html

Note the usage of several functions:

Rigidbody. addexplosionforce: add an explosive force to reduce the force size by distance.

Physics. overlapsphere: List of collision bodies within the detection range

The following code tests the effect:

Void fixedupdate ()

{

If (input. getkeydown (keycode. Space ))

{

Raycasthit hit;

If (physics. raycast (camera. maincamera. screenpointtoray (input. mouseposition), out hit ))

{

Debug. Log ("addexplosionforce ");

Collider [] hits;

Hits = physics. overlapsphere (hit. Point, raduis );

Foreach (collider t in hits)

{

If (T! = NULL & T. attachedrigidbody! = NULL)

T. attachedrigidbody. addexplosionforce (force, T. attachedrigidbody. Position, raduis, UPS );

// T. attachedrigidbody. addforce (New vector3 (0f, 0f, 1f) * 500366f );

}

}

} Segmentation

}

2. Real-time Mesh Segmentation

The shatter toolkit 1.41 plug-in is used to achieve effects similar to Fruit Cutting. The object can be divided into several parts by points or directly separated by plane, this method requires a CPU-consuming operation. During the cutting, the object is first divided into two parts based on the given plane equation. For the triangle surface on the boundary line, the vertex, normal, and texture are required, color, tangent, and so on are interpolated to generate new information. The plug-in meets the basic requirements. However, to achieve the specific effect of an individual, you still need to process and expand the events before and after cutting.

Instructions for use:

When you need to break down or split an object. You need to attach a shattertool script instance object and a uvmap instance object to the object. After cutting, the object fragments will be instantiated as the clone of the original object.

The crushing tool script requires the object to contain the meshfilter component, and the meshcollider and Rigidbody components are also required.

Note 1: If you are using meshcollision and rigid body components, You need to enable the meshcollider convex option to avoid component quality update after splitting.

NOTE 2: If the shattertool. fillcut option is enabled, each side of the grid belongs to two triangles, that is, the grid is closed.

Usage:

1. Add the shatteroncollision script to the object and trigger the collision.

2. Attach a mouse event to an object, including clicking, sliding, and dragging.

3. Send the world coordinates with "shatter" and split locations to trigger the shatter event in the shattle tool.

4. Send a "split" message with a plane sequence in the world coordinate system. Trigger the split event in shattle Tool

5. Use the shatterscheduler event management mechanism to send messages by adding tasks to control the root cause of more events.

About the mouse:

1. Click the broken result

2 cutting with mouse dashes

3. Drag the mouse

Level:

Hierarchyhandler script can be used to achieve segmentation Management in Cutting

Results:

If you have not done much processing, you can process the message events before and after cutting. Similar to the hierarchyhandler management method, you can process the effects before and after cutting.

Technical implementation:

(1) calculation plane

Plane splitplane = new plane (vector3.normalize (vector3.cross (line, Ray. Direction), hit. Point );

Line is the linear vector in the world coordinate system -- vector3 line = maincamera. screentoworldpoint (New vector3 (end. x, end. y, near)-maincamera. screentoworldpoint (New vector3 (start. x, start. y, near ));

Ray. direction is the vector from the screen to the cutting point -- "Ray = maincamera. screenpointtoray (vector3.lerp (START, end, (float) I/raycastcount ));

The result of the above two cross-multiplication is the normal vector of the plane, and the cutting plane can be determined by adding the cutting pad.

(2) single-point cutting solution:

Generate several random volume orientation using vector, and combine the cutting points to form several planes

Similar unity Plugin: shatter toolkit 1.41,: http://www.unitymanual.com/thread-19781-1-1.html

3. Blasting Effect (fracture can be divided into 50 blocks at most)

Use the plug-in: fracturing _ destruction_1.11. This plug-in is a little complicated. In fact, the basic principle is similar to the first one. The awesome thing is that no art is required to break down fragments, the plug-in will first break the object according to your settings, generate another pile of broken blocks, and record the connection relationships between the broken blocks, so as to facilitate subsequent processing during the broken blocks. The plug-in is very complete and can basically achieve some good crushing effects by setting it like using particles. Because real-time computing fragmentation is not required, the performance can still be used. If prefab is required, some resources will be occupied. Read the settings Panel carefully before use ~~ There are too many parameters. For more information, see here.

4. Particle effect

In summary, when the host performance is poor, especially the GPU performance is not strong, the fragments produced by the above two methods are rigid bodies, and the physical engine will be used during the cracking process, therefore, the performance consumption is very high, especially when there are many fragments. It is better to properly control the number of fragments.

A good effect should be a combination of multiple effects, such as generating particle effects immediately after fragmentation. However, it is natural for people to feel that, of course, good machine performance is also required to support these large amounts of physical computing and rendering. The company's host performance is still relatively poor, too much physical computing and fine rendering will cause lag. But to be honest, good games do not have to have excellent visual effects.

 

The "light rain" label indicates the "light rain" label.

How unity achieves object crushing effect

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.