Search Unity

Approaches to letting a model "fall apart"

Discussion in 'Scripting' started by dgoyette, Apr 21, 2018.

  1. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    I've created a robot model for my game. It's composed of many individual parts/meshes, because I want the robot to be able to break apart when it dies. (Think of a skeleton falling into all of its individual bones.) Technically speaking, the model is made up of many different pieces, such as upper leg, lower leg, chest, etc, each of which is a Skinned Mech Renderer. The model as a whole has a Humanoid rig, which I'm using to animate it. When the robot dies, I want each of the individual pieces to receive a collider and a rigidbody so that the robot falls apart realistically.

    My basic question is wondering how people would handle the transition from an animated single model, to a bunch of independent objects with rigidbodies and colliders. Two approaches seem vaguely reasonable to me, but I don't know which (if either) to pursue.

    Approach 1: Create new game objects which contain a Mesh Filter, Mesh Renderer, Mesh Collider, and a rigigbody for each Skinned Mesh Renderer object in my robot. This seems to work okay, but I'm not yet sure how I can figure out the exact position and rotation of the Skinned Mesh Renderers so that I can place the new object at its position.

    Approach 2: For each Skinned Mesh Renderer, somehow separate it from its rig, and add a collider and a rigidbody. I haven't had any luck getting this to work. Or, more specifically, the collider doesn't appear to line up with the skinned mesh renderer.

    Anyway, hopefully this isn't too vague, but I'm curious if there's a different kind of approach I haven't considered.

    Thanks,

    -Dan
     
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    It will vary depending on approach and type of model. I have tried several different approaches, they all work better in certain scenarios. I did a lot of testing found interesting performance results. My situation differs from your a bit, as I am not using skinned meshes, just animated collections of meshes (parts). My initial method was overly simple, I just walked thorough the hierarchy and unparented, and added colliders and rigidbodies. (I stored all the initial locations for pooling and instantiation.) That worked pretty well, and did what I wanted, but once I got several on the screen things started to drag.
    Early test using that method:


    My next approach was to basically have two models, a hero and "splode" model. I would generate the hero model procedually combining meshes to as few as I needed for animation, and at the same time, generating a second group of pieces that was disabled in the same place. When it gets destroyed, I turn off the hero and turn on the parts. This was nice because I could make the colliders a little bigger so they overlap and really blow apart nicely when turned on. This was better than the first approach, but still causes stutters on mobile, or if there is a lot going on. Here is what that looks like. Before explosion, they are single or just a couple of meshes. It is also more prefromant, as there is only one or two materials per ship, and the shaders are more optimized.


    The final approach I came upon by accident, sort of. I was testing when the optimal time was to introduce the parts into the world. I discovered that it was more performant to simple create them when needed, rather than pre building them and turning them on all at once. (which was counter intuitive to me). In the following example, the unbroken models are a single mesh with one material, (these aren't animated). When they are destroyed, I turn off the mesh renderer and kill the rb and collider on the model, then I use its postion and loop through all the parts and create them via new GameObject() adding the rb/collider/fader scripts and generate the mesh from stored data. (I don't instantiate prefabs or anything). It runs crazy fast, on a pc I can generate millions of pieces in a frame or two. The following video is captured from device
    runs smoothly.

    I would say try a couple of approaches. As I discovered, a method I would have assumed to be inefficient turned out to be the best.
    Good luck.
     
    DominoM likes this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    @zombiegorilla I've been using the two models approach myself for a while now and I'm really surprised to hear your results, could you possibly share more information?
    shaders aren't my strong suite