Replace ragdoll with smaller cubes

Hello!

I’m creating this ragdoll wich falls to the floor. When it’s on the floor and been here for a given time I want to make it look like it’s split up in a lot of cubes that flies up and disappears.

The model is made of cubes only, and is placed in a hierarchy so the ragdoll works perfectly.
I have also made to iterate through the model and take the Transform properties of each part.

The model looks kind of like this, except the arms is the wrong size after converting from FBX (well done not saving the Blender-model).

Model

After this I don’t really know where to go. I would guess that going through each child and find it’s real size in Unity, then deciding how many and how large cubes there should be put in would be the thing.
But I don’t really have a clue how to do this.

I have also thought of replacing the model with another model, but I certainly don’t want to do this as I have to remodel both the “original” and the “cuby” one.

C# examples would be preferred.

Does anyone have a hint about how this could be done easily?

3 Answers

3

Picasso would love this cubic ragdoll!

If you’ve created a conventional ragdoll with rigidbodies and character joints, I think the better approach would be to create a “2nd ragdoll” in the Editor: an empty object with childed cubes matching the ragdoll parts in size and name, and replace the ragdoll with this object, copying the transform position and rotation of each part. You should not copy the hierarchy, just add each cube as a child of the empty object - kind of:

  Ragdoll2 <- the root empty object
    Head  <- use the same name of the corresponding ragdoll part
    Chest
    Pelvis
    LArm
    LForearm
    RArm
    RForearm
    LThigh
    LLeg
    RThigh
    RLeg

You could replace the 1st ragdoll with a function like this (1st ragdoll script):

public GameObject rag2prefab; // drag the cubic ragdoll here

void ReplaceMe(){ 
  Destroy(gameObject); // suicide this ragdoll and create the 2nd one:
  GameObject rag2 = Instantiate(rag2prefab, transform.position, transform.rotation) as GameObject;
  Rigidbody[] parts = GetComponentsInChildren< Rigidbody>(); // get the ragdoll parts
  foreach (Rigidbody part in parts){ // iterate through them
    Transform part2 = rag2.transform.Find(part.name);
    if (part2){ // if a part with same name exists...
      part2.position = part.transform.position; // copy position...
      part2.rotation = part.transform.rotation; // and rotation...
      // and add the up movement with a little random rotation:
      part2.rigidbody.velocity = Vector3.up*0.5f; 
      part2.rigidbody.angularVelocity = Random.insideUnitSphere*0.1f;
    }
  }
}

Call the function ReplaceMe() to replace this ragdoll with the Picasso cubic version.

NOTE: Destroy() doesn’t kills the object immediately, thus you can safely find its children and copy their positions and rotations while inside the routine ReplaceMe().

EDITED: You could create the cubes on the fly with a code similar to this one, but you should replace all ragdoll colliders with box ones in the Editor. Supposing all ragdoll colliders are box colliders, I think this code could do the job:

  ...
  foreach (Rigidbody part in parts){ // iterate through them
    // create the cube for each part...
    GameObject part2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
    part2.AddComponent< Rigidbody>(); // add a rigidbody to it...
    BoxCollider col = part.collider as BoxCollider; // get the original collider...
    part2.transform.localScale = col.size; // and get the size from it
    part2.transform.position = part.transform.position; // copy position...
    part2.transform.rotation = part.transform.rotation; // and rotation...
    // and add the up movement with a little random rotation:
    part2.rigidbody.velocity = Vector3.up*0.5f; 
    part2.rigidbody.angularVelocity = Random.insideUnitSphere*0.1f;
  }

I’m not sure however about the collider’s center: if it’s not (0,0,0), the cube may appear at the wrong position. Maybe you should add or subtract the collider’s center to the cube position to fix this.

I think this alternative is better because you can set different colors or materials to each cube, but creating the cubes at runtime is also possible: take a look at my answer, to which I added a code that can do that (maybe with some little changes).

I experimented a bit with it, and decided to make it a model and just replace it. Seemed you were right about the amount of work, and I’m starting to get annoyingly close to the deadline.

After I made the cuby model, I find that first of all it’s just finding the Root and Spine. This I believe is because it’s a quite large hierarchy that goes like this:

  • Root
    • Left_hip
      • Left_leg
    • Right_hip
      • Right_leg
  • Spine
    • Head
    • Left_arm
      • Left_forearm
    • Right_arm
      • Right_forearm

Second: when it finds a part, it just returns this microvalue, e.g. -0.29 in stead of e.g. 230.
I really cannot find this giving any sense at all.
Any hints?

Instead of a ragdoll, make all of it a bunch of rigidbodies, that will make it fall on the floor in a bunch of scattered pieces.