Search Unity

Noticeable T-Pose when using a ragdoll

Discussion in 'Editor & General Support' started by AzraelKans, Mar 7, 2011.

  1. AzraelKans

    AzraelKans

    Joined:
    Oct 29, 2009
    Posts:
    135
    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
    Code (csharp):
    1.    
    2.     static void CopyTransformsRecurse (Transform src, Transform dst) {
    3.     dst.position = src.position;
    4.     dst.rotation = src.rotation;
    5.     dst.gameObject.active=true;
    6.    
    7.     foreach (Transform child in dst) {
    8.         // Match the transform with the same name
    9.         var curSrc = src.Find(child.name);
    10.         if (curSrc)
    11.             CopyTransformsRecurse(curSrc,  child);
    12.         }
    13.     }
    14.  
    But is no use the ragdoll goes to a t-pose then falls down after a second.
     
  2. AzraelKans

    AzraelKans

    Joined:
    Oct 29, 2009
    Posts:
    135
    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.
     
  3. Carwash

    Carwash

    Joined:
    Nov 12, 2009
    Posts:
    95
    Thanks for posting your fix, solved my headache :)
     
  4. PharmacyBrain

    PharmacyBrain

    Joined:
    Sep 8, 2020
    Posts:
    6
    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.


    Code (CSharp):
    1.     private void KillEnemy( Vector3 velocityToRagdoll )
    2.     {
    3.         _animatedModel.SetActive( false );
    4.         _ragdollModel.transform.position = _animatedModel.transform.position;
    5.         _ragdollModel.transform.rotation = _animatedModel.transform.rotation;
    6.         _ragdollModel.SetActive( true );
    7.  
    8.         CopyTransformsRecursive( _animatedModel, _ragdollModel );
    9.  
    10.         Rigidbody[] ragdollBody = _ragdollModel.GetComponentsInChildren<Rigidbody>();
    11.  
    12.         foreach ( var bodyPart in ragdollBody )
    13.         {
    14.             bodyPart.velocity = velocityToRagdoll;
    15.         }
    16.  
    17.         _alive = false;
    18.     }
    19.  
    20.     private void CopyTransformsRecursive( GameObject source, GameObject destination )
    21.     {
    22.         for ( int i = 0; i < source.transform.childCount; ++i )
    23.         {
    24.             var currentSourceTransform = source.transform.GetChild( i );
    25.  
    26.             for ( int j = 0; j < destination.transform.childCount; ++j )
    27.             {
    28.                 var currentDestTransform = destination.transform.GetChild( j );
    29.                 if ( currentDestTransform.name == currentSourceTransform.name )
    30.                 {
    31.                     currentDestTransform.position = currentSourceTransform.position;
    32.                     currentDestTransform.rotation = currentSourceTransform.rotation;
    33.  
    34.                     if ( currentDestTransform.childCount > 0 )
    35.                     {
    36.                         CopyTransformsRecursive( currentSourceTransform.gameObject, currentDestTransform.gameObject );
    37.                     }
    38.                 }
    39.             }
    40.         }
    41.     }