Im trying to turn an animated character into a ragdoll but when I create the Ragdoll (with instantiate) it goes into a t-pose, I am using the fps-tutorial sample code in c# with function this
static void CopyTransformsRecurse (Transform src, Transform dst) {
dst.position = src.position;
dst.rotation = src.rotation;
dst.gameObject.active=true;
foreach (Transform child in dst) {
// Match the transform with the same name
var curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
But is no use the ragdoll goes to a t-pose then falls down after a second.
Ooops! never mind, I just realized the reason why this happens. The Animation component was still in the ragdoll, hence the t-pose
Im leaving this here though, in case someone has the same problem.
Thanks for posting your fix, solved my headache 
Resurrecting this because it was a top Google search result and solved my problem. I will add some value though.
Problem: switching to a ragdoll model on death resulted in the ragdoll being in a default t-pose. This looked pretty bad. The above post inspired me to iterate though all of the transforms in each model and copy the transform from the current, animated model to the hidden ragdoll model.
Basically what I am doing is taking the impact from a projectile (like an arrow) and applying that force to the ragdoll as well, so it not only crumples but also flies in the direction it was hit. It creates a satisfying death.
I used for loops because I did not like the original use of “Find” above. I also learned that copying a transform from one object to the other does not seem to work; you have copy the position and rotation.
The below is messy and needs to be refactored, but I am zooming along just focusing on getting things working for the moment.
private void KillEnemy( Vector3 velocityToRagdoll )
{
_animatedModel.SetActive( false );
_ragdollModel.transform.position = _animatedModel.transform.position;
_ragdollModel.transform.rotation = _animatedModel.transform.rotation;
_ragdollModel.SetActive( true );
CopyTransformsRecursive( _animatedModel, _ragdollModel );
Rigidbody[] ragdollBody = _ragdollModel.GetComponentsInChildren<Rigidbody>();
foreach ( var bodyPart in ragdollBody )
{
bodyPart.velocity = velocityToRagdoll;
}
_alive = false;
}
private void CopyTransformsRecursive( GameObject source, GameObject destination )
{
for ( int i = 0; i < source.transform.childCount; ++i )
{
var currentSourceTransform = source.transform.GetChild( i );
for ( int j = 0; j < destination.transform.childCount; ++j )
{
var currentDestTransform = destination.transform.GetChild( j );
if ( currentDestTransform.name == currentSourceTransform.name )
{
currentDestTransform.position = currentSourceTransform.position;
currentDestTransform.rotation = currentSourceTransform.rotation;
if ( currentDestTransform.childCount > 0 )
{
CopyTransformsRecursive( currentSourceTransform.gameObject, currentDestTransform.gameObject );
}
}
}
}
}
2 Likes