Search Unity

Replacing Enemy with Ragdoll

Discussion in 'Scripting' started by maca6918, Jun 10, 2012.

  1. maca6918

    maca6918

    Joined:
    Jun 10, 2012
    Posts:
    2
    Hi all,
    At current I've setup an enemy using the soldier from the bootcamp demo that just casually walks around. When I shoot at it and his "health" equals zero the model is replaced with a ragdoll prefab that I created. The only problem is when the ragdoll is instantiated it is in the "T" position. How can I make the ragdoll into the position that the soldier is standing when the character gets replaced with the ragdoll?

    Hope everyone understands what I'm trying to get across :D

    Thanks in advance.
    Jack
     
  2. Myx

    Myx

    Joined:
    Nov 29, 2010
    Posts:
    196
    Hello there!

    You'll have to go through each joint and position it correctly.

    Code (csharp):
    1.  
    2. Transform[] ragdollJoints = ragdoll.GetComponentsInChildren<Transform>();
    3. Transform[] currentJoints = solider.GetComponentsInChildren<Transform>();
    4.  
    5. for(int i = 0; i < ragdollJoints.Length; i++)
    6. {
    7.     for(int q = 0; q < currentJoints.Length; q++)
    8.     {
    9.         if(currentJoints[q].name.ComperaTo(ragdollJoint[i].name) == 0)
    10.         {
    11.             ragdollJoint[i].position = currentJoints[q].position;
    12.             ragdollJoint[i].rotation = currentJoints[q].rotation;
    13.             break;
    14.         }
    15.     }
    16. }
    17.  
    This code should gather all joints, then go through each joint in the ragdoll and search for joint in the current model with the same name. If the same name is found, the ragdoll's joint is positioned like it. Note that this code is written in browser, so I can't guarantee it will work from scratch.

    There's more optimized ways to do this, but this should give you a general idea of what needs to be done.
     
  3. TwisterK

    TwisterK

    Joined:
    Oct 26, 2010
    Posts:
    110
    maybe you can make the ragdoll play the standing animation before replace it?

    --TwisterK
     
  4. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    You don't need to seperate objects (Solider Model and Ragdoll). You can make both in one model, just disable the ragdoll colliders and mark then kinematic when your player is moving. When he dies, disable the normal collider box (of the player/solider model) and enable the ragdoll colliders and put then non-kinematic and it will colapse in it's current position
     
    erebel55 likes this.
  5. maca6918

    maca6918

    Joined:
    Jun 10, 2012
    Posts:
    2
    Thanks guys, I'll give each of those ideas a go and see how it goes.

    Cheers,
    Jack
     
  6. lexen1

    lexen1

    Joined:
    Dec 4, 2013
    Posts:
    10
    I modified the code a little bit and it worked for me:

    Transform[] ragdollJoints = ragdoll.GetComponentsInChildren<Transform>();
    Transform[] currentJoints = transform.GetComponentsInChildren<Transform>();

    for(int i = 0; i < ragdollJoints.Length; i++){
    for(int q = 0; q < currentJoints.Length; q++) {
    if(currentJoints[q].name.CompareTo(ragdollJoints.name) == 0){
    ragdollJoints.position = currentJoints[q].position;
    ragdollJoints.rotation = currentJoints[q].rotation;
    break;
    }
    }
    }

    For me, this is all running on a health script attached to the player. If you did something different, you would have to change the line "Transform[] currentJoints = transform.GetComponentsInChildren<Transform>();" to "Transform[] currentJoints = soldier.GetComponentsInChildren<Transform>();"