Ragdoll physics for the win

Ragdoll physics is fun to see in action. 
Ragdoll physics is NOT fun to implement in a full game. 

For sure, I wanted to have ragdoll physics in Artillery Royale.
It’s fun and goes well with the explosions and destructible map. 

As always I checked online for tutorials and I found many of them.
It seemed easy with unity: add a bunch of rigid bodies and capsule colliders linked with hinge joints and… voilà

But what if you already have some animations going on?
Using some anima2D inverse kinematic or similar?
Those tutorials fall short. 

So this is how I did, it’s not that hard but there is a lot of going on. 

But first, please enjoy the result

See the ragdoll physics in action, even pushing the opponent in the way

We have two states: Playing (using idle/run/jump animations), and Receiving damages (ragdoll part).

Playing: the animation (via animator) is active and your anima2D bones move. Great. 
Receiving damages (i.e.: from an explosion): 
– You stop the animation and disable anima2D.
– Save all bones local positions and rotations.
– Activate all your ragdoll related rigid bodies (that will activate the associated colliders etc).
– Add some force to the main bone’s rigid body and let the physics engine do the magic.
– When the main rigid body ends moving you deactivate all the ragdoll rigid bodies.
– Extrapolate the current bones positions/rotations back to their saved state.
– Eventually you restart the animation and you’re back to Playing state. 

OMG this is some intense coding but it works quite well!