Search Unity

Dynamic Ragdoll Example Package

Discussion in 'Physics' started by tiredamage42, Mar 30, 2019.

  1. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    19
    Dynamic Ragdoll

    Try it out and let me know what you think, any suggestions, questions, or feedback is welcome.

    Unity package:
    https://drive.google.com/drive/folders/1mm56CNDJKBYyabisi0AzHdq0o3aUDIzK?usp=sharing
    on GitHub (probably the most up to date version):
    https://github.com/tiredamage42/DynamicRagdoll

    So recently I've been messing around with getting some functional ragdolls working in Unity, and I felt like sharing the results with the community :)

    The standard Unity ragdolls created by the ragdoll wizard were super unstable for me, and unaffected by whatever animation they were coming out of. Which was really jarring when a running ragdoll would drop straight down.

    I found the following thread and package and it gave me some really good results!
    https://forum.unity.com/threads/animfollow-active-ragdoll-is-now-free-to-download.220812/

    Unfortunately, I found myself spending too much time tweaking the values to keep it stable, while trying to balance not doubling up on fixed time steps, as well as tweaking values that weren't even doing anything

    So instead of trying to make the follow algorithm super stable enough to mimic an animation, I changed it to toggle between the animated model and the ragdoll only when it's needed.
    That way the physics are still affecting it as it's 'falling', and when it's fully animated, it just renders the character.

    By calculating the velocity of the animation bones in late update and setting them in fixed update (as opposed to adding forces like in AnimFollow), the velocity can be maintained for a longer period of time in a more stable manner, which gives the illusion of the ragdoll 'attempting to stay upright'

    The effect was good enough since i didnt really need a fully physics driven character (unless it was actually ragdolled) and now I can keep the project's fixed time step unchanged.

    When it came to getting back up the joints and rigidbodies kept getting snagged and becoming unstable again, so I implemented the blend transition i found here:
    http://perttuh.blogspot.com/2013/10/unity-mecanim-and-ragdolls.html
    which turns off the physics and blends the rigidbodies on the ragdoll to the animated position.
    Now both transitions look really good in my opinion.

    The AnimFollow package from above used a specific character and ragdoll set up that was specific to that
    "Ethan" character it comes with, which had an extra collider.

    I'm no 3d modeller, when it comes to humanoid characters I use the mixamo pipeline myself.
    So I made some scripts that would let me quickly add a ragdoll to a humanoid mixamo rigged model at runtime, or in the editor at the push of a button.

    The ragdolls they make are easily tweaked using scriptable object behaviors, so changes to joint limits, rigidbody masses, or other values can be easily made per ragdoll bone in the editor, without having to constantly drag 10+ transforms into the ragdoll wizard every-time, or dig through the hierarchy finding a lower arm joint component!

    And thanks to the beauty of Scriptable Objects, changes made during play mode are saved afterwards

    With this system, making a ragdoll is as easy as:
    1. adding the Ragdoll component (included in the package below) to the model
    2. assign a ragdoll profile scriptable object (with all the settings per bone)
    3. either press the button in the inspector to prebuild or let it build on Awake​



    -Syd


    Some things I still have to figure out:

    - [x] rag dolling on the stairs going downwards doesn't exhibit normal gravity...
    - figure out some way to incorporate the arms or extra leg forces to mimic someone trying to stay balanced
    - a better dismemberment system, (actaullly cut off the limb instead of making it disappear)

    Update 9/9/19
    • Added several custom components that act as physical objects that can "impale" rag dolls and other rigidbodies. Showcased as ammo types, where you can add damage modifications to them
    • implemented "complex" rigidbody grabbing that allows for freedom along certain axes intuitively.


    Update 9/2/19
    • Implemented a simple dismemberment system, and per limb damage multiplier system (showcased in ActorRagdollLink.cs)
    • Added per bone data, that lets us keep any custom information per bone, showcased in the damage multiplier system mentioned above
    • Edited demo scripts, so interaction with the ragdoll system is contained within: ActorRagdollLink.cs, PlayerControl.cs, and CharacterMovementRagdollLink.cs
    • Ragdoll on collisions is incorporated as a feature in the system, no longer a demo script
    • Implemented a system to “grab” ragdolls (showcased in PlayerControl.cs)


    Update 4/5/19
    • removed the AnimFollow pd control method
    • removed the physics step delay when going ragdoll
    • refactored some demo scripts in order to show a how a character controller can interact with the ragdoll controller (CharacterController.cs)
    • added a script showing how to deal with ragdoll on collisions ( CharacterCollisions.cs )
    • Fix for 'falling up' stairs included in the charcter controller script
    • Fixed 'gliding forward' issue when ragdoll isnt upright
    Here's a demo of the collisions controller:


    Update:
    I updated the package to include a different method for following the animation.
    By calculating the velocity of the animation bones in late update and setting them in fixed update (as opposed to adding forces), the velocity can be maintained for a longer period of time in a more stable manner, which gives the illusion of the ragdoll 'attempting to stay upright'
     
    Last edited: Sep 10, 2019
    Acttogreen, Arthur-A, koirat and 7 others like this.
  2. the_0dium

    the_0dium

    Joined:
    Nov 29, 2018
    Posts:
    15
    Wow, nice. I am doing the exact same thing as you right now. Used animfollow, it didn't work as I wanted it to. So I started making my own solution and lol I also had an idea of swapping animated model with ragdoll :D I will definitely check our your work when get back home
     
    tiredamage42 likes this.
  3. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    19
    Thanks!

    Swapping was definitely the way to go for me as well, following with the ragdoll every frame didn't play well with IK aiming (In a previous attempt, I didn't include any aiming in this package yet). and I found that plain old animation movement was smoother anyways.
     
  4. Morderkaine

    Morderkaine

    Joined:
    Oct 22, 2016
    Posts:
    11
    I notice that all the times the model goes ragdoll its falling down all the way, but blending back into the animation to get back up. Can this work for a model taking a hit and flinching back or something and then coming back to the animation pose without falling over? Like if the model takes force to the shoulder, that shoulder will be pushed back, and then blend back into the animation?
     
    tiredamage42 likes this.
  5. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    19
    Unfortunately not as of now. Maybe that same method of blending back to the animated model could work, by transitioning back after a set interval instead of waiting for the ragdoll to be still (and bypassing playing the 'getup' animation).

    But the transition might be a bit jarring when starting from "natural motion", since it's just a linear interpolation. Maybe adding some noise to that would work though.

    Thanks for the thought! Ill definitely be experimenting with this for the next update
     
    the_0dium likes this.
  6. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    Does this work with IK?
     
  7. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    19
    It should. The velocities and rotations for each bone are calculated in late update, which happens after the animator's IK step.
     
  8. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    I just tried it in Unity 2019.1 now, imported the GitHub-files into my project, and the .fbx couldn't be imported. How can I see a demo of this? I tried it on my own .fbx but I couldn't quite get it to work so far as I couldn't figure out exactly how the different skeletal hierarchies relate...
     
  9. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    19
    The model i uded was the ybot model from mixamo. So any mixamo rigged model should work. Theres a link on the post to the unity package version. https://drive.google.com/drive/folders/1mm56CNDJKBYyabisi0AzHdq0o3aUDIzK?usp=sharing

    Have you made sure that the import Settings on the model are set to humanoid?
     
  10. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    Yes, but it complains there are not enough bones (15 needed).
     
  11. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    19
    You might have to go into the configure avatar window in unity and specify which bones are which. Its in the import settings. I havent had to do this for any mixamo models. Which one are you using? Ill download it and check if it's compatible?
     
  12. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    These are the issues I am getting when importing the latest GitHub-repository version into a fresh Unity 2019.1 project:
    emptie2.PNG emptie.PNG dolio1.PNG
     
  13. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    19
    Do you have git lfs installed? It might be that judging by the errors on all the assets (not just the model)

    Try using the unity package version. Or installing git lfs and reimporting the repo
     
  14. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    I finally got it to work after reimporting and deleting the Library-folder. However, when I try it on my own imported model it seems the rotation is off (I tried reusing the code you had for the specific .targetRotation-part in my own code).

    I see now that the imported Mixamo-rigs' joints all start at Vector3.zero in rotation, where mine sadly has a few offset angles of 90 and 180 degrees. It's a rigify-rig from Blender that is based on a model I made myself.
     
  15. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    19
    Cool. Glad to hear. Im not much in the 3d modelling/rigging field so ive been pretty reliant on those pre rigged models. could you send an image of the offset rotations? Or the model itself. I could use it to try and generalize the scripts a bit more. Is the trouble in the ragdoll building stage or when trying to follow animations after ragdolling?
     
  16. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    ruher.gif Above is doing the chain with transform.localRotation = targetRotation * startLocalRotation. It jitters because it is currently fighting the ConfigurableJoints.
    ruhroha.gif Above is using the targetRotation of the ConfigurableJoints. As you can see, the transform.localRotation-method works, but the targetRotation-method is off by a lot. Below is the code. Left is my code and right is the code you did that I based it on: targitur.PNG segmentes.PNG
     
  17. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    Oh my, it just works now. I decided to continue trying to implement what you did into my own code for a custom solution, and now I finally managed to get decent results!!! :3

    The IK-testrig isn't the best (I have a better system but I just needed to test something), and it matches up surprisingly well with the active ragdoll:
    workenings.gif
    As you can observe, it respects the limits like it should, and minus the small physical inaccuracies that I need to sort out, it is exactly what I wanted it to be. The best part is that I can control the strength per joint directly through the JointDrive, so I get direct control to the strength of the segments:
    offe.gif

    This is very most excellent. Thank you very much for helping me out and providing me with this source code. :3333
     
  18. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    19

    Looks awesome! Glad I could help out, would you mind posting your code to check it out?
     
  19. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    I am still working out some minor adjustments, but it is not much different from what I have already posted, except I added an extra offset calculation for the start rotation if needed (which was the main issue with my rig), and did the IK calculation before the rotation calculation (however it seems I can still do it in the same for-loop, but I am investigating stability).
     
  20. Will_Dev

    Will_Dev

    Joined:
    May 24, 2017
    Posts:
    27
    This looks very interesting. Is it possible for me to make the character follow an animation in ragdoll state? For example I have an enemy character which turns into a ragdoll when it dies. But the knockback usually causes the arms to flop around and doesnt look realistic. Can i use this so the enemy tries to replicate an animation when its a ragdoll already?
     
  21. Will_Dev

    Will_Dev

    Joined:
    May 24, 2017
    Posts:
    27
    I also have a blender rigged model and I cant seem to get it working properly. Can you share your code?
     
  22. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    As stated above:

    "I am still working out some minor adjustments, but it is not much different from what I have already posted, except I added an extra offset calculation for the start rotation if needed (which was the main issue with my rig)..."

    I am doing this in my spare time and have frightfully little time for this, so you can be patient or do what I wrote.
     
  23. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    976
    been following this, looks like an interesting project.

    in the last update on github, the character doesn't show up (2048.4)


    also wanted to ask, is there any particular reason to use scriptableobjects? is there something in which they are better than simple cs script? to me they look like need to be drag-and-dropped into slot, while cs can be accessed/changed runtime.
     
  24. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    19
    Do you mean it doesnt render or is there a problem importing the asset?

    I chose to use scriptable objects in order to have the ability to make different profiles for ragdolls. And its easier making changes in editor. As changes made to scriptable objects during playmode are saved. Just a preference of mine.
     
  25. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    19
    Sorry for the late reply. Yes this is what this solution is for. If the arms flop around too much. Try increasing the angular drag on the ragdoll profile for the arms
     
  26. FM-Productions

    FM-Productions

    Joined:
    May 1, 2017
    Posts:
    72
    I just want to say thank you for providing such a great asset! I haven't figured it out yet, but the demo looks really good, and I'm gonna inspect it the next few days
     
  27. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    976
    you can simply save changes if you click on the icon in the component's right-up corner, and save the values in any script.
    it's quite a big drawback if as I know, they cannot be accessed/changed runtime.

    easiest way to make profiles is just keep all values in an array, and maybe keep that as .profile[] in profileControl.cs
    Code (CSharp):
    1. [System.Serializable]
    2.     public class Profiles
    3.     {
    4.         //public bool.float,KeyCode,AnimationCurve,etc
    5.         public variable..
    6.     }
    7.     public Profiles[] profiles;
    8.  
     
  28. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    19
    Thanks!
    I just added a new update actually, some new videos on the way.
     
  29. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    19
    I revisited the code with this in mind, but didn't see any reason why any of the values would need to be changed at runtime (like the neighbors per bone, or rigidbody masses...), so I left the profiles as scriptable objects, if anyone really wants to they could just reorganize it as a nested class like you suggested.

    That being said, in the last update I did include a system in which the end user can have any custom data per bone they want, that can be updated at runtime if needed.
     
  30. renat_k

    renat_k

    Joined:
    Mar 22, 2016
    Posts:
    8
    Cool asset! I really like how your ragdoll is acting, but when i try to build my own ragdoll i recieve weird results, even using mixamo other characters...
    Can you help me please? upload_2020-7-11_23-30-10.png
     
  31. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    19
    Hey, sorry for the late reply.

    To be honest I've been super busy with school so I haven't checked on this project in a while and updated it to the latest unity version, so there might be some compatibility issues, either that or maybe the naming convention for mixamo rigged models has changed. Are there any errors showing in the console?

    judging by the picture it looks like the direction of the leg capsules is perpendicular to what it's supposed to be. Maybe mixamo has changed the direction their leg bones are facing since i created the project. If that's the case, then maybe some experimentation with the RagdollBuilder class (where the leg bones are being built) can help.
     
  32. Mena99

    Mena99

    Joined:
    May 30, 2017
    Posts:
    5
    Hello @tiredamage42
    thanks a lot for sharing this package with us, Although there are few things that aren't clear yet

    first of all the version on getHub is definitly not the latest
    upload_2020-10-3_18-30-14.png

    it's 13 - 24 months old.

    Secondly on the scene i can see that the character has 2 game objects/prefabs
    upload_2020-10-3_18-32-4.png

    both of them are very different

    Ybot
    upload_2020-10-3_18-32-41.png

    yBotRagdoll

    upload_2020-10-3_18-33-24.png


    I'm trying to find out how i can convert my own player into ragdoll when certain event happens then back to normal when timer is up

    Could you walk me throw that ?
    which component do I need to add and which is not necessary for my need?
    why both of them have animator components ?



    what is also confusing is that both yBot and yBotRagdoll has the meshes ?! ... why do we need meshes on both of them ?
     
    Last edited: Oct 3, 2020
    IndieFist likes this.
  33. Mena99

    Mena99

    Joined:
    May 30, 2017
    Posts:
    5
    NEW UPDATE:

    in the package whether on Github or Google Drive, I couldn't find the tool that create Ragdoll and separate the Skelaton from the Ragdoll ??

    where is that ???
    dose anyone knows ??

    this video says the script was called RAGDOLL ...
    in the google Drive version is totally different

     
  34. Mena99

    Mena99

    Joined:
    May 30, 2017
    Posts:
    5
    I finally found the Ragdoll script ...

    I DONT KNOW WHAT MADE U THINK THAT THIS PROFILE IS GOOD FOR MIXAMO LOL

    look what happen when I tried to use it on a freshly downloaded Mixamo
     

    Attached Files:

  35. IndieFist

    IndieFist

    Joined:
    Jul 18, 2013
    Posts:
    520
    I just found this package and trying to configure in my character controller, anyway, there is no guide really? the most basic i can i see is, ybot acting in the mesh object, and the ybotragdoll, this is the rigg from mixamo with ragdoll script on each joint really?
     
  36. laurent_quark

    laurent_quark

    Joined:
    Jan 10, 2021
    Posts:
    9
    hi, please, im searching a good ik system with configurablejoint since a lot of monthes, my ik with joints don't work, transform is ok, but joint.... i see sames errors of u in 2019, please could u share your actually code?
    my ik use a world rotation.
     
    Last edited: May 4, 2021
  37. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    I've moved to ArticulationBodies now, and had to sort of overstart again...
     
  38. laurent_quark

    laurent_quark

    Joined:
    Jan 10, 2021
    Posts:
    9
    me too, i tried by Drive.Target method, that work for the rotations, but i win anothers problems, (collisions are bad detected, no collision with any object wich use meshcollider with convex),if i make a arm with any cubes and my ik system, the collide is good, but if a object enter in collision with my ik, i lost the Target values (no come back to original position of arm) and the system panic.

    then... i come back to configurablejoint.
     
    Last edited: May 4, 2021
  39. laurent_quark

    laurent_quark

    Joined:
    Jan 10, 2021
    Posts:
    9
    but i'll like try with a ik system wich use localrotation (not world rotation) to hope use a correctly rotation joint with your pictures in up.
    i galley on this since November. my project is blocked because of this.
     
  40. Blan3X1

    Blan3X1

    Joined:
    May 29, 2019
    Posts:
    28
    so is there a tutorial?
     
  41. SoloDevCsharp

    SoloDevCsharp

    Joined:
    Mar 5, 2021
    Posts:
    7
    Is it possible to apply custom falling animation?Thanks!
     
  42. Blan3X1

    Blan3X1

    Joined:
    May 29, 2019
    Posts:
    28
    Yes,this asset contains animation blending too so it will work out
     
  43. stor314

    stor314

    Joined:
    Apr 26, 2019
    Posts:
    20
    hey did you ever make any progress with articulation bodies and arm ik? i'm unable to get it to work really with spherical articulation joints.
     
  44. radiantboy

    radiantboy

    Joined:
    Nov 21, 2012
    Posts:
    1,633
    looks good but the files are corrupt and will not open even in unity 2018.3.0f2 - did anyone get this to load up?