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).
– aldonaletto