How to make Active Ragdolls in Unity
I’ve always loved how some games use animations that seem to generate naturally depending on the environment. Like how in Uncharted Nathan uses his hands to explore nearby objects when you pass by, or how in GTA you get these amazing reactions to collisions.
So, I started investigating procedural animation systems and came across a technique called physical animations, also known as active ragdolls. The concept is actually pretty simple, make a ragdoll try to match an animation by applying forces to its body parts. This way the animation is physically simulated, which allows for more realistic interactions with the environment.
This post is specifically about how to do this in Unity, but I’ve also made a more distended video where I show you how I made my own active ragdoll system, which you can find here as well. Feel free to use it as you please.
Before we start, I have to say that making good active ragdolls is not that hard, but it’s quite messy. You don’t need to be a genius or have amazing programming skills, but being tidy and keeping your project under control will save you a lot of headaches.
If you find it interesting, just try to make it work! In the worst case, you’ll probably end up with something cool to show to your friends.
Model & Skeleton
Any model can work with physical animations, that won’t be a problem per se. If there’s a problem, it will be with the skeleton. I recommend avoiding very complex skeletons. Fingers, for example, add a lot of unnecessary complexity.
Also, only export deform bones and don’t export leaf bones, it will simplify the structure in Unity. If you’re using Blender, it will look somewhat like this:
I recommend having at least these 3 basic animations for your model:
- The T-Pose: it’s useful to have a T-Pose as the default pose/animation to later configure the joints of the ragdoll in Unity.
- A standing pose: unless you want to use the T-Pose as your standing pose, which looks a bit funny.
- A walking animation: with active ragdolls, movement comes from the animation itself, the physical contact between feet and floor is what propels the character forward. So make sure you have a symmetrical and loopable walking animation, or else your character might move in weird ways.
Configuring the physics is as important as it can be. If you skip this step, literally nothing will work.
Finding the exact positions and velocities of objects after they interact with each other is a very computationally expensive process. That’s why physics engines use iterative solvers, which make approximations that get better and better after each iteration.
We’ll be creating a complex physical system, with many Rigidbodies connected by joints. If we do not get good approximations, our active ragdoll won’t work at all. Unity’s solver doesn’t do many iterations by default, so we need to pump those values up.
This can be done by changing the Solver Iterations and Velocity Solver Iterations, which basically tells the engine how many times to let the solvers iterate when the physics are updated. Of course, more iterations demand more on the hardware. I’ve seen that values above 8 for both of them achieve good results.
It’s also useful to boost the maximum angular speed, this won’t be as noticeable but it will make the ragdoll more agile in some situations. The default for Unity is 7, I have seen that any value over 20 is enough for active ragdolls, but it depends on how fast you want it to move.
You can change both the solver iterations and the maximum angular speed directly for all the rigid bodies in your game, by going into the physics section of the project settings. Or you can set them from code for each Rigidbody independently, which is very useful to only spend processing power on the ragdolls, and let the rest of the simulation be less precise.
Unless you are using this in a commercial or big project, just change them in the project settings, you won’t feel the difference.
Setting up the bodies
Here’s where it gets messy. We need to have two separate bodies, one for animation and one for the actual active ragdolling. I recommend grouping those two under one parent object, so you can easily move it around, make prefabs, put scripts on it…
The Animated Body
This will be a standard animated character, nothing fancy going on here. We need this as a reference that our ragdoll will try to match. Unity makes a Game Object for each bone in the model’s skeleton, this will come in handy for later matching motion.
You should generally disable this body’s model, you only want your active ragdoll to be visible, not the reference animation. But this causes the animator not to move the skeleton bones, since it detects that it will have no visible effect. To change that, you need to go to the Animator component and change the culling mode to
The Physical Body
I’m going to throw a detailed explanation of how to configure an active ragdoll properly. So this section is going to be quite long, and if you’ve never done something like this, it might be also hard to understand.
I say this not to scare you, but so you won’t get frustrated if it doesn’t work right away and you have to go back multiple times, or even do some research on your own. I also had major issues and misunderstandings the first time I did it, but I learned a lot and got it working in the end.
To make the active ragdoll per se, you need to duplicate the animated body and set the
Animator Controller to
None. You can also disable or delete it, but I prefer to leave it there for some of the functionality it provides.
Now, we need to configure this as a ragdoll, the first step is adding a Rigidbody to each body part and setting an adequate weight, I use this as a reference of weight per body part for humanoids. Maybe you’ll want to avoid adding Rigidbodies to some bones, such as the fingers, but that will depend on your model and your goals, so do what goes best for you.
After you’ve set up all of your rigid bodies, you need to connect them through joints. Joints are just physical entities that link two rigid bodies together, making them rotate and move around each other in a certain [configurable] way.
For active ragdolls in Unity, we need to use Configurable Joints. Those are the more abstract type of joints, meaning that you can achieve the same behavior as any other joint with it. But we use them primarily because of one function, the Target Rotation. This is used to tell the joint to rotate towards a specific rotation, using the spring forces defined by its angular drives.
First, we need to add a ConfigurableJoint component to each body part and set the connected body to its parent. For example, we should add a ConfigurableJoint to the forearm that connects it to the upper arm, and one to the upper arm that connects it to the chest/ribs.
Of course, since one joint connects two body parts, you will have one less joint than body parts. Generally, you’ll want the joint-less Game Object to be the root of your character, which is usually the hips or the torso.
If you hit the play button now, you won’t see a difference between the body with and without joints. That’s okay, we still need to configure them. First, let’s lock the motion on all axis for every joint, since we want body parts only to rotate around each other, not to slide along. Also, set each axis angular motion to
Limited, this will be useful later.
If you play now, you should see that your body is already kind of a ragdoll. I say ‘kind of’ because ragdolls limit the rotation of their joints, and this one still doesn’t.
Setting up the rotational limits of joints is a very specific process for each model, as well as counterintuitive in most cases, so I can’t help you much here.
You’ll need to set the axis and secondary axis properly for each joint, by tweaking the
Secondary Axis vectors in the inspector. The X-axis should represent the main movement of a particular joint since it is more configurable than the other two. This is impossible to explain with words, so let’s see it with an image better.
You can hit the
Edit Angular Limits button to see the axis visually. But this is just to see it more clearly, this won’t allow you to actually change them visually, it’s intended to modify the limits, which we’ll see later.
Still, I don’t think you can understand this well enough with an image either, you’ll need to play with those axis values and see how that affects the joint. If you can’t get it working in your project, I recommend you to download mine, which is already configured, and fool with it to get a hold of how joints work.
After getting each joint right, you need to lock those axes that you don’t want movement on. For example, you only want the knee to rotate around one axis (the one that allows the extension of the leg), so lock the other two.
Let’s now configure the limits. You can hit the
Edit Angular Limits button we talked about before, and manipulate them visually in the editor. You can also change the numbers directly in the component’s configuration, but that won’t be as easy.
Finally, you just need to configure the Angular Drives so joints can apply torque to match the target rotation. If you just want to get it working without more delay, you can set a high spring for every joint, and worry about polishing it later. The movement might look a bit rough, but it will work anyway.
You should now have a character GameObject with two bodies, one animated and hidden, and the other the active ragdoll itself. We just need to tell the active ragdoll to replicate the movement of the animated one, and we will do so with the
Target Rotation feature I talked about before.
We need a script that sets the target rotation of each joint to that of its equivalent animated body part. Of course, nothing is easy with active ragdolls, and we can’t just set the target rotation to the rotation of its equivalent animated body part, because Quaternions are evil monsters.
I have to admit that I have no idea how to do this, that’s why I used this Configurable Joints Extension made by Michael Stevenson, it works flawlessly. The only extra thing we need is to save the initial local rotation of each joint, since it is required to calculate the target rotation.
So to summarize:
- Add the Configurable Joints Extension to your project.
- Make a script that will be responsible for controlling your Active Ragdoll movement.
- In that same script, make an array to store all of the animated transforms and another one to save the joints. There should be one less joint than animated transforms.
- Save to an array all of the joints initial local rotations on the
- Set the target rotation of each joint to the local rotation of its animated equivalent on the
FixedUpdatefunctions. Since there is one less joint than animated transforms, you will have to ignore one of them, which as I said before should be the root.
And this is it, your active ragdoll should be physically playing an animation now!
A few more notes here.
Remember that you need to play an animation with your animated body, make this one visible again and see if it’s moving properly. If the animated is moving but the ragdoll isn’t, then the problem lays in the active ragdoll implementation.
I don’t think the fixed update improves anything here, since the animation changes at the frequency of the Update loop, but I find it clearer to do physics things on the fixed update.
And last, as I said a couple of times already in this post, if you can’t get it working, I highly recommend you download my implementation, play around with it and see what’s different from yours. You can find the details of motion matching in the
Animation Module script.
You probably noticed a big flaw of active ragdolls, it just falls. It’s useless unless we balance it in some way. Natural balance is an incredibly hard problem, which has been investigated for years in the field of robotics. I’m very passionate about the topic, but it’s way out of the scope of this tutorial.
So, we’ll have to just force our active ragdoll to stay up by adding some artificial forces into the mix. I’ve found 3 ways to achieve artificial balance, each with its pros and cons:
- Lock the rotation of the hips on the X and Z axes. This means the active ragdoll can’t fall, but it can still rotate on the Y-axis to turn around. It also means that it will behave rigidly, never getting out of balance, and producing huge artifacts and glitches on many occasions. This method is completely unrecommended unless you really want to use it for some specific reason.
- Connect the hips to a kinematic rigid body. Create a rigid body with the direction you want your hips to have. Then make a joint that connects that body to the hips (or root) of your active ragdoll. Leave the default target rotation and set the angular drives to be much stronger than any individual part of the body. Your active ragdoll will ‘magically’ try to stabilize itself constantly. It still looks unnatural, especially because of the springiness, but it works quite well in general.
- Apply a manual torque to the hips towards an upright direction. For this, you will need to have a high maximum angular speed, or it won’t work at all because the torque you apply will be clamped. It’s quite easy to implement, just apply a torque that rotates the hips of your ragdoll towards an upright direction. This actually is what the joint of the last method does internally. But doing it manually makes it possible to define another torque output function (instead of the spring), and get custom behaviors.
For me, the manual torque solution is the best, but the joint works well too, and it’s easier to set up. I see little reason to lock the rotations, it works horribly in my experience. For very small projects it could be enough, it’s actually quite stable if you don’t do anything fancy. But who makes active ragdolls not to do anything fancy with them?
If you know any other method to artificially balance, please let me know in a comment, I’ll appreciate it.
This topic would require another extended post, so I’ll just explain what Inverse Kinematics is about and why it can be useful with active ragdolls.
Standard animations have one major flaw, they cannot be dynamically adapted depending on the context. Active Ragdolls solve part of the problem, by making animations interact with the physical environment. But there are more ways to make animations more dynamic.
One of the most common ways to improve animations is to generate movement that targets certain objects in space. For example, making the hand target something you want the character to grip. To do this, you need to define a target position and rotation for a certain body part, and then find a way to set up all of its parents to allow for the target to be reached.
That means solving a group of equations to find how each parent rigid body should be positioned and rotated, this mathematical process is called Inverse Kinematics. But there’s a problem, the number of solutions to those equations is infinite, you can reach the same spot with your hand in infinitely different ways.
To solve this uncertainty, inverse kinematics solvers need hints, which are objects in space that tell where a certain rigid body in the kinematic chain should point to. Following the previous example, we need a hint to tell the elbow where to point. That constrains the solver, though reducing the solution space and allowing it to find a solution.
Unity has an Inverse Kinematics built-in, I’ve run into some problems while using it, so I can’t say I’m a big fan. But if you just want to mess around with IK, it’s easy to use right away.
Thank you for reading all until the end! I hope you learned how to make active ragdolls in Unity, feel free to make any suggestions in the comments or to contribute to the repository.