Dynamic Ragdoll Example Package

Dynamic Ragdoll

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

Unity package:
__DynamicRagdoll - Google Drive
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 :slight_smile:

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:

  • [×] 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’

10 Likes

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 :smile: I will definitely check our your work when get back home

1 Like

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.

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?

1 Like

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

1 Like

Does this work with IK?

It should. The velocities and rotations for each bone are calculated in late update, which happens after the animator’s IK step.

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…

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?

Yes, but it complains there are not enough bones (15 needed).

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?

These are the issues I am getting when importing the latest GitHub-repository version into a fresh Unity 2019.1 project:

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

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.

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?

4442566--406696--ruher.gif Above is doing the chain with transform.localRotation = targetRotation * startLocalRotation. It jitters because it is currently fighting the ConfigurableJoints.
4442566--406699--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:

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:
4446823--407455--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:
4446823--407461--offe.gif

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

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

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).

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?