
Blending Ragdoll physics and animation in Unity
This is a short techincal explanation of our system to blend between ragdoll physics and animation in unity 2019.
It came from the need of adding more robots to the scene by dropping them from the sky, so after being ragdolled on the floor they had to get back to an animated state:
[previewyoutube=M8ZjZjjrxTg;full][/previewyoutube]
The ragdoll of the robots is fairly standard, they have an almost humanoid skeleton but not exactly.
It can be achieved on humanoid models following this great Brackeys video: https://www.youtube.com/watch?v=DInV-jHm9rk
Once ragdolled on the floor, the first step is to figure out if the robot fell on its front or back.
To do that we just compare the vector up of the root bone of our character and the default vector up.
Vector3.Dot(rootBone.up, Vector3.up) > 0
We're only focusing on two cases, up or down but that could be improve to have better animations to get up at different angles.
We can then choose between the two animations:

At that point, we want to blend between the positions of the ragdolled skeleton and the animation.
That's done by calculating the position and rotation of each bone, using their current ragdolled position and their supposed animation position.
So on
LateUpdate() after the animation position is calculated:
To get the animated bone position, we have to look at the transform on
LateUpdate, but for a real humanoid rig, it should be accessible using
Animator.GetBoneTransform (https://docs.unity3d.com/ScriptReference/Animator.GetBoneTransform.html)
Interpolation of position and rotation for each bone, using a blendSpeed variable to be able to tweak the strenght of the blend:
foreach (Bones b in skeleton)
{
b.transform.position = Vector3.Lerp(b.transform.position, b.ragdolledBonePosition, blendSpeed);
b.transform.rotation = Quaternion.Slerp(b.transform.rotation, b.ragdolledBonePosition, blendSpeed);
}
I hope that short explanation helps some of you understand the logic, feel free to come and chat on twitter if you have questions!
https://twitter.com/unspottablegame
https://twitter.com/gwendle
And please wishlist on the store page to help us make more content! :)
https://store.steampowered.com/app/1243960/Unspottable/
[ 2020-02-20 12:30:29 CET ] [ Original post ]