How to "Animate" Joints

Moin Everybody!

I recently got asked if there are any good reads regarding "active ragdolls" in unity and since i couldn't find anything useful i thought I am gonna start a little tutorial series.

If you don't know what the above means i try to explain it a little bit. Ragdolls are the thing in GTA for example that are responsible that falling down looks so realistic. Ragdolls are usually built out of joints, rigidbodys and colliders

A Joint is a) a rolled cannabis cigarette or b) the connection made between bones in the body. for example the wrist is the connection between hand and lower arm including some constraints about how heavy you can bend it . Its up to you to figure out wich I am talking about now. A Rigidbody turns a static object in a game into a physical one. For example a simple sphere can be turned into a ball that is falling down by adding a rigidbody. And a collider is taking care that the ball wont fall threw the floor, well it collides with the world.

If you set up these 3 Components correct on a human mesh with a rig, you can so to say simulate a dead body moving around. But I wanna show you how to wake the dead now.

First, lets not start with a complicated human bone structure and a big amount of joints. Lets set up a simple robot . So create a few boxes and add some colliders and rigidbodys to it, and set up their mass values like this: 

hip: 5, upper Leg: 10 lower Leg: 1. Also, set the hip rigidbody to kinematic or freeze the Y motion for now so your robot will stay in the air.

Now comes the interesting part. Setting up the Joints. First you need to lock all Motion Axis, except the Angular X Motion since we wanna focus on creating a simple walk forward animation. you can unlock the other motion axis later if you wanna add sideways walking and so on.

Now we need to "stiffen" the joints since they are more like jelly now and don't have any strengths. what I am doing now is pretty much the same as flexing your muscles.

First, change the Rotation Drive mode from "X and YZ" to "Slerp" and then raise the Slerp Drive values to:

Position Spring: 10000
Position Damper: 100
Maximum Force: 100000

the connected body of the upper leg should be the hip, and the lower leg should be connected to the upper leg then. These values can be changed to whatever you like, the higher, the more you flex. weird but okay.

next thing we need to grab is the " Extension methods for working with Configurable Joints for Unity " by  Michael Stevenson - you can get it on his github repo right here

Just place that somewhere in your unity project and you are good to go.
Time for some code (you can get the snippet in the Attachment).

Save that script, drop the component on your robot root object and set it up like this.

and also the "Leg Curve" like this:

Whats happening here now is pretty simple, I am simply passing over the animation curve value using the Evaluate() Function of the Animation Curve to the X target rotation of the Joint. The Configurable Joints Extension is actually doing the hardest parts, its translating real world angles to Quaternions. And since i set the Animation Curve and to "Ping Pong" it will smoothly repeat as long as you want to. That's all.

That is all the magic for now and your robot should do something like this now and you can just make him walk by pushing him into the direction you want by using AddForce. But I wanna cover that part in the next tutorial.

By becoming a patron, you'll instantly unlock access to 5 exclusive posts
By becoming a patron, you'll instantly unlock access to 5 exclusive posts