I cant Find any simple script to position all the children in the ragdoll Replacement to match the original character children position.
The Hierarchy and naming is the same except root, so how should i got about doing it?
found the 3rd person ragdoll instansiator, but it had a pretty hard recursive c# version of doing it. but i can’t simplify it.
I’m not sure what exactly you wanna do, but simply turning a biped character into a ragdoll should be quite easy.
Every bone-GameObject need a rigidbody and a hinge-joint which have to be connected to the parent.
Like the unity - documentation tells us: “never have a rigidbody on a child of another rigidbody” because that screws up the physics-system.
So all you have to do is unparenting all childs recursively.
I’ve never worked with ragdolls, but the easiest way i could think of would be something like:
(C#)
public void ConvertIntoRagdoll()
{
Transform[] childs = transform.GetComponentsInChildren<Transform>();
foreach (Transform T in childs)
{
if (T.hingeJoint == null)
T.gameObject.AddComponent<HingeJoint>();
if (T.rigidbody == null)
T.gameObject.AddComponent<Rigidbody>();
if (T.parent != null)
{
// the parent must have a rigidbody
if (T.parent.rigidbody == null)
T.parent.gameObject.AddComponent<Rigidbody>();
// establish physic-connection
T.hingeJoint.connectedBody = T.parent.rigidbody;
// cut the transform hierachy
T.parent = null;
}
}
// cut the root if it's not already ;)
// the root gets his rigidbody from one of his child-bones in the loop above
transform.parent = null;
}
I’ve just code that from scratch and haven’t tested it yet.
The 3 checks are necessary because you never know the order you process the bones (it could start with a finger and go backward or randomly spreaded).
You could do it hierarchically by starting at your root bone, but you can’t do a foreach-loop on transform due to the unparenting you change the collection of the children. That means you have to get a copy of each hierachy-level and iterate through that. It’s much more complicated
The only thing you can’t set that easy are custom colliders for each bone and the limits of the hinge.joints (if you need them). Maybe you can attach a little script on each bone which holds the limits and a prefablink to a special prepared bone-collider. There are countless ways for doing such quite advanced stuff and this one is for sure not the best.
The positioning you mentioned you don’t have to care about, because unity have this really nice feature:
when parenting or unparenting GOs the childs keep their worldpositions that means the localposition and rotation get corrected automatically.
As i said never worked with ragdolls or hinge joints but i think that’s “a” way to do it
I didn’t think of the possibility of using the current character setup, and just add the physics… since i will probably have colliders for all important parts, this should work perfectly…
There are often many solutions for one problem. Thanks for broadening my mind