Search Unity

Dynamic Ragdoll Example Package

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

  1. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    16
    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
  2. the_0dium

    the_0dium

    Joined:
    Nov 29, 2018
    Posts:
    7
    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:
    16
    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:
    16
    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:
    83
    Does this work with IK?
     
  7. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    16
    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:
    83
    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:
    16
    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:
    83
    Yes, but it complains there are not enough bones (15 needed).
     
  11. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    16
    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:
    83
    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:
    16
    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:
    83
    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:
    16
    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:
    83
    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:
    83
    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:
    16

    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:
    83
    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. Lolerz

    Lolerz

    Joined:
    May 24, 2017
    Posts:
    21
    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. Lolerz

    Lolerz

    Joined:
    May 24, 2017
    Posts:
    21
    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:
    83
    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:
    846
    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:
    16
    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:
    16
    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:
    47
    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:
    846
    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:
    16
    Thanks!
    I just added a new update actually, some new videos on the way.
     
  29. tiredamage42

    tiredamage42

    Joined:
    Oct 26, 2016
    Posts:
    16
    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.