Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Puppet Master Mapping Ragdoll

Discussion in 'Physics' started by Holzhax, Aug 22, 2023.

  1. Holzhax

    Holzhax

    Joined:
    Apr 9, 2020
    Posts:
    4
    Hello!
    I'm very fascinated by physically based character movement and now I am trying to combine a ragdoll character with animations (Like the Root-Motions "Puppet Master"-Asset). I experimented with configurable joints before, but now I am struggling with "mapping" the animations to the ragdoll.

    My plan was to take the two limbs around a certain "imagined" joint of the animated character and calculate it's angle with those two limbs. I tried to use the Transform component but i noticed that I can't get enough information out of world space. I would need to get the rotation of each axis between the two limbs, create a quaternion and set the "target rotation" of the certain configurable joint.

    Maybe I can use IK to get around this?

    Any Ideas would be appreciated!
     
  2. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    734
    You don’t need IK, unless you want to overwrite some animations for e.g. look direction or limbs reaching out. Regarding the setting of rotations: there are several projects on GitHub which should give a good idea about how to combine physics with animation. Just search for active ragdoll. In short, you need to do two things: set the rotations of joints according to animator transforms, and fix the limbs with force to position of animator transforms.
     
  3. Holzhax

    Holzhax

    Joined:
    Apr 9, 2020
    Posts:
    4
    Okay,
    The ragdoll is following the animation now, thank you. I used this to match the target rotation of the joints to the animated transforms (Maybe it's helpful for others too).
    So, as I want the character to behave in a realistic way, the method I use to hold the character in position is to apply a force to the rigidbodies:

    Code (CSharp):
    1. private void FixedUpdate()
    2.     {
    3.         for (int i = 0; i < joints.Length; i++)
    4.         {
    5.             Vector3 direction = transforms[i].position - joints[i].GetComponent<Transform>().position;
    6.             joints[i].GetComponent<Rigidbody>().AddForce(direction.normalized * pinningForce, ForceMode.Force);
    7.         }
    8.     }
    Here, "transforms[]" are the transforms of the animated character.

    But there are still two problems so far:
    1. The joints are not completely accurately following the target. That's probably because not the whole rig is integrated in the ragdoll (shoulder-, spine-,... bones) and the animation changes their position. So the anchors of the configurable joints are not always accurate and need to get updated. But that's not a big problem so far.
    2. The character stabilizes expectedly with the above code. But as the force increases the limbs start to wobble and with a force of 1000 the ragdoll flies through the air. I'm not sure what causes this conflict but it's not the joints.

    Thank you in advance.
     
  4. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    734
    for 1) yes, you should have joints everywhere mecanim does rotation updates.
    for 2) sounds you need some damping to avoid the wobble. either on the rigidbody, or if possible on the joint itself. the wobbling comes from overshooting the target a bit in both directions.
     
  5. Cloudwalker_

    Cloudwalker_

    Joined:
    Jan 3, 2014
    Posts:
    133
    Animations generally are rotations. Not translations.
     
  6. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    734
    For physical animation you need both; you also need to move the individual physic bones towards the position of the animated bones. Else the physic ragdoll will just fall over. The animation itself is of course mostly rotation, plus root translation.
     
  7. Cloudwalker_

    Cloudwalker_

    Joined:
    Jan 3, 2014
    Posts:
    133
    Yes. But if you're doing that then it's not physics based walking. And even then you only need to pin the hip or root. Not every bone.
     
  8. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    734
    depends on. if you closely want to follow the animation / aim for realism, you need to pin every bone. And yes, then the walking is "fake" and not driven by physics. If you want a more stylized physics animation, like in some fun games, your approach would work fine.
     
  9. Holzhax

    Holzhax

    Joined:
    Apr 9, 2020
    Posts:
    4
    Yeah, I aim for a realistic movement. Looking like animated but still a ragdoll, like in GTA, or I'm not exactly sure how Euphoria works. It would simply work with only pinning the hips or add a force to the hips joint, but then the movement would be wobbly and not what I want. So i have still trouble with the AddForce command. Also with dampening (Rigidbody drag, AddForce based on distance to target, splitting the force over several frames,...), the rigidbodies get out of hand
     
  10. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    734
    For the damping of AddForce you need a PID. Look at the implementations on Github, like this one here:
    https://github.com/ashleve/ActiveRagdoll
     
  11. Holzhax

    Holzhax

    Joined:
    Apr 9, 2020
    Posts:
    4
    Thank you! It works now with the help of a PID
     
    Qleenie likes this.