Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Demolishing a Model

Discussion in 'Scripting' started by haystack, Dec 10, 2014.

  1. haystack

    haystack

    Joined:
    Oct 20, 2014
    Posts:
    21
    I'm trying to get a basic understanding of how to demolish a 3d model in game.

    By repeatedly firing a weapon at a wall, I want to be able to destroy it over time. A standard way to do this would be to swap out the wall model with more and more damaged versions and play a particle effect upon every hit. Then finally play an animation of the wall collapsing after it has taken enough damage.

    However I'd like the effect to be a bit different every time, and based on physics, where the wall get's hit, and what hits it. For example a bullet would do little damage, whereas a grenade would do a lot. If the weapon is shot up at the wall, the pieces would go flying up, whereas if a weapon was shot from a side angle to the wall, say from the right, the pieces would go flying off to the left. In both these examples, where the impact is not straight on, but at an angle, the damage to the wall will be shallower in the Z axis, but strecth out longer in the X and Y axis across the wall; whereas a straight ahead impact will be deeper in the Z axis but smaller in the X and Y axis -- just like how it would happen in real life.

    Also, when a big enough hole is made in the wall, the top part would start to collapse, but this would be based on physics, not a predetermined amount of damage the wall would have to take. (For example, not, after ten hits, collapse wall). I would also like to be able to change the "strength" of the wall. Some walls will destroy very easy, other will take a lot of damage. Again, trying to do this all based on physics and without using pre built damaged models or animations.

    I would guess a first step would be to subdivide the wall model into many separate pieces? Not sure how to make the subdivisions on the fly based on the physics of the weapon impact though. Also I would think I'd want to keep the pieces non rigid body up until they are actually hit. If they all start out as rigid bodies, they will start to fall from gravity as soon as they are subdivided from the wall; but then, how to collapse the top of the wall after enough damage has been done?

    Just trying to get a basic understanding of how to go about doing this. I'd like to understand the steps in a more general way so I can apply the technique to other objects, objects that won't explode on impact but will dent or warp instead, for example.
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    This is where a lot of mesh subdivision techniques fail. They divide the polygons, but they don't represent depth, so walls look paper thin, like breaking a Christmas ornament. Noble Muffins' Turbo Slicer closes off the subdivided meshes, but it's all-or-nothing (full solid or paper thin). I think you're going to have to swap the model out with a version composed at design-time of different physics objects. Some buildings are made of thick cinder blocks, others of thin plywood. If you want to try to figure out the composition at runtime, you could tag each face with information about what it's made of. When you subdivide a face tagged as a brick wall, you'd replace it with a stack of bricks. A face tagged as sheetrock would be replaced with a bunch of sheetrock shards.

    If you want to minimize the physics load, you could progressively subdivide as necessary by swapping out each wall only when it takes damage. The other walls could each stay a single, solid mesh until they're also damaged.

    Denting and warping will probably use mesh deformation, which is a different technique. You could always write a higher-level wrapper to encompass both techniques, though.
     
  3. haystack

    haystack

    Joined:
    Oct 20, 2014
    Posts:
    21
    Thanks, TonyLi.

    I have been thinking about this a bit more. I found out about Blender's Cell Fracture add on, which basically breaks up a model into a bunch of pieces via a Voronoi algorithm. After breaking apart my model, I can import all the pieces into Unity, and then use Rigidbody.AddExplosionForce to send the pieces flying away from the collision point of the projectile.

    This brings up a problem that I am not sure how to solve, however.

    If I add a rigid body to all the pieces, they will start to fall from gravity as soon as the game starts, but if I don't use a rigid body, AddExplosionForce won't work. Is there some way to loop through all of the pieces, and if they are within a given radius of the collision point, then change them to rigidbody?
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    What about setting them up as kinematic rigidbodies? (Tick "Is Kinematic".) Then run Physics.OverlapSphere to get a list of all pieces within the collision radius and set isKinematic false. You might want to enable everything above the sphere, too. If a wall collapses, I would assume the roof would also come down.

    Before you put too much effort into code, test out Blender's Cell Fracture first. Make sure it produces the visual output that you're looking for.
     
    haystack likes this.