Recently I’ve posted this GIF on Reddit and it received surprisingly good feedback, so I decided to post about how to achieve this effect using Blender and Unity3D.

wallBreaker.gif

First of all, you need an object with good geometry, no disconnected vertices or edges, no internal faces or areas with zero thickness. Sometimes you can repair your model with the triangulate modifier. If your model does not have proper topology the Cell Fracture function will fail and you will not obtain the desired results.

destructible1
The wall that I’ll destroy in this example.

Next, enable the Cell Fracture add-on function in Blender under File > User Preferences…

destructible2
Search for cell fracture function in the Add-ons tab, and tick the check box at the right side of the screen.

Now you can use the grease pencil to mark the impact areas. In the image below the maximum fracturing will occur in the center. Make sure you are drawing in a layer inside the object in hierarchy, and that stroke placement is set to surface.

destructible3
The grease pencil guides the cell fracturing. Denser areas spawn more shards.

Next, find the Cell Fracture Tool in the left side of the screen. You can tweak these values to obtain different results, but make sure to set Point Source to Grease Pencil, and to increase noise so that the fractures look more natural. Keep source limit to a minimum, as this will determine how many shards will be generated and can cause performance issues if you use real time physics.

destructible4

This is the end result after cell fracturing.

destructible5
In this case 31 shards look good enough and don’t impact performance too badly.

Now you can import both the intact model and the fractured one into Unity. You can switch between them once the player hits the wall. I check for this event with OnTriggerEnter.

void OnTriggerEnter(Collider other) {
  if(other.CompareTag("PlayerHitbox"))
  {
    Instantiate(fracturedWall, transform.position, transform.rotation);
    Destroy(gameObject);
  }
}

Finally you can add the Rigidbody component, the Mesh Collider component, and a “DestroyOverTime” script component to each of the shards of the fractured wall.

destructible6
Inspector view of an individual shard of the destroyed stone wall. Every shard has these components.

Performance Issues

As reddit user Novezz has mentioned, this method can be performance heavy, as Unity’s physics engine is doing lots of real time calculations for collisions of each shard of the fractured object. Here follow some important considerations:

  • Check Convex at the mesh colliders, this makes the physics calculations simpler for the game engine.
  • Instantiate as few shards as possible. Tweak the source limit parameter in the cell fracture function to obtain a different amount of shards.
  • Destroy the shards as soon as possible, you can make fancy dissolve or shrink effects to make it seem more believable.
  • Consider using primitive colliders for the shards instead of mesh colliders if performance is a real issue (mobile platforms).
  • Consider baking the destroy animation instead of using real time physics at all if you don’t need it to look dynamic. If you’re concerned that the animation might look repetitive, you could create different clips that are randomly picked by mecanim once the object is destroyed.

BONUS: Breaking a Building

In order to add extra physics fun to the game you can make it so that the object only breaks if impact force is big enough. This is achievable by using the relativeVelocity collision property.

void OnCollisionEnter(Collision collision) {
  if (collision.relativeVelocity.magnitude > breakLimit)
  {
    SpawnFracturedObject();
    Destroy(gameObject);
  }
}

This way you could treat each individual floor of a building as a different block object, all of them stacked to look like a skyscraper. If a cannonball is thrown with enough force at the lowest floor, the whole thing will gradually go down crashing piece by piece.

breakBuilding.gif

Conclusions

Breaking stuff is fun and surprisingly easy to implement! More games should let us break stuff in fun and different ways. If you find this article helpful or if you would like to share different methods please let me know, now go break some shit up!

Advertisements

One thought on “Making your game go Ka-Boom with destruction physics

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s