01 Jul

Character Movement AI – Self-balancing Ragdolls

Today we want to show you how we combined Unity’s tools to create a very unique core mechanic in Deadly Labs. This post is supposed to show that thinking outside the box can have some funny results, and that every standard tools can be used in a non-standard way. Because that’s what makes a thing interesting, right?

A video tells more that 1000 words, so here is what we targeted:

View post on imgur.com

Our goal was to switch between normal walking creatures, animated by Unity’s Mecanim system – and once they were hit, they should be on their own with the physics engine, adjusting forces to its limbs in order to keep balance. Also move the legs like a human would do, to avoid falling down and gain stability.

As you see in this pic, the Animation setup is pretty basic. Walk and attack. We also added an animation controller for facial Animation, because that’s more comfortable than coding everything, but it doesn’t control the body, only morphing mesh values for the face.

Very minimalistic Animation Controller. We wanted to let most of the animation be completely controlled by physics, so you never have the same result. We thought randomness would make the gaming experience much more interesting, as you never know what’s going to happen when you shoot enemies. Sometimes they stay on their feet, they fall down somewhere or they are smashing against objects that are also physics controlled. So you can play the game more than once and it will be different every time.

Basic workflow for a broodle is:

  • Throwing fluid objects into the barrel (walk and attack animation)
  • Get new objects out of the container in the back (also walk and attack)
  • Repeat that until the barrel explodes

When the broodle was hit by a shot, it disables the Mecanim Controller and enables the Ridigbodies of all limbs. It’s now up to the broodle what happens next. Since this is a mobile game, it has only box colliders to keep the performance as high as possible. Luckily this didn’t affect the natural movement of the character at all.

You may notice the StandUpFromBack and StandUpFromBelly animation clips in the Inspector above. This is because if the character can’t stay on its feet, it falls down to the ground, so it has to get up while keeping the movement naturally. I mean, we could just create a flash, vanish it and put it back on its feet. But this seemed like cheating to us, so here is what we did – workflow for beeing shot down and standing up:

  • Ragdoll falls to the ground, all limbs somehow rotated
  • Blending Animation Coroutine: Rotate all limbs to default over 1 sec duration
  • If he is on its back -> trigger StandUpFromBack, if he is on the belly -> trigger StandUpFromBelly.

Here is the result:

View post on imgur.com

Finally here is a quick preview of the code that keeps the creature on its feet while the ragdoll system is active. It is triggered during the FixedUpdate event, because that’s when the physics system also refreshes. This is important to keep in mind when working with different modules, you don’t want to execute code redundantly as it would affect performance and cause errors.

If you think this is burning down a mobile phone, it really isn’t. I still have an Android phone from 2013 and even there it is running without issues. Physics in Unity are very optimized, if you use a lot of box colliders, you shouldn’t have problems. The most important thing to consider in terms of mobile performance in my opinion are shaders. But more about that in another blog post.

I hope you liked this post and hopefully it helps boosting your creativity for using tools in an unorthodox way.