Ragdoll: Animated Physics of character - Gr8-Tools/game-developer-roadmap-doc GitHub Wiki
Ragdoll: Animated Physics of Character
Total time: 11 hours
Key Definitions
Ragdoll - A type of character animation that uses physics simulation to create realistic movement and collisions. Ragdolls allow characters to move and react in a life-like, floppy way after being hit or falling over.
Rigidbody - A Unity component that allows a GameObject to be affected by physics forces like gravity. Rigidbodies are essential for creating ragdolls as they enable physics simulation.
Joint - A connection between two Rigidbodies that limits their movement relative to each other. Common joint types used in ragdolls include Hinge, Slider and Fixed. Joints are what "stitch together" the pieces of a ragdoll character.
Animation - The process of generating realistic motion for a character over time. Ragdoll animation blends between procedural physics-based motion and traditional animation clips to create a seamless transition.
Kinematic - A type of rigidbody that is not affected by physics and is directly controlled by animation or code. Kinematic rigidbodies are useful for driving ragdoll movement in a controlled manner.
Blend Tree - A type of animation controller that allows blending between multiple animation states based on a single input value. Blend trees are commonly used to blend ragdoll physics with normal animation states.
Trigger - An event that can be used to signal a change in an object's state. Triggers are useful for initiating ragdoll physics when a character dies or falls over.
Example Usage
1: Creating a Ragdoll Character
-
Create a new empty GameObject to serve as the parent for the ragdoll parts.
-
Add child GameObjects for the body parts (torso, head, arms, legs etc) and position them appropriately.
-
Add Rigidbody components to all parts and configure their mass properties.
-
Add FixedJoint or HingeJoint components between connected parts to form the skeleton.
-
Add a Collider (usually a MeshCollider) to control collisions.
-
Create a script that toggles the rigidbodies to kinematic on start for normal animation, then sets them to dynamic on trigger to activate ragdoll.
2: Blending Ragdoll with Animation
// Script to blend ragdoll with animation
public Animator anim;
public bool isRagdoll;
void Update() {
if(isRagdoll) {
// Blend weight to ragdoll physics
anim.SetFloat("Blend", 1);
}
else {
// Blend weight to animation
anim.SetFloat("Blend", 0);
}
}
Extended Learning Topics
Time: 20 minutes
Time: 1 hour
Time: 10 minutes
Time: 20 minutes
Time: 30 minutes
Time: 20 minutes
Time: 40 minutes
Time: 40 minutes
Tasks
Cube-game
Time: 7 hours
In this lesson we would like totally change the behaviour of our hero.
Now it will have some different states:
- Idle: when it’s standing at the same position and don’t moving
- Walking and Running: when it’s moving. The character should have an opportunity to change its movement speed from normal to fast and in reverse order.
- Jumping: from any state above (idle/walk/run) the character should have an opportunity to jump and save its speed and direction movement.
- Die: when hero is damaged (in our case it’s fall on the floor or was collided with pendulums from the previous task)
Now some remarks of encoding this behaviour. First of all it’s really difficult to encode the movement animations with physics and it’s redundant. So we will change the move-control from Dynamic Rigidbody ones to Character-control or Kinematic Rigidbody (in cace of Kinematic rigidbody you will need to move the pelvis-bone). Now the movements will more accurate and easier to control. The concept of movement stays the the same: we move only forward and backward and rotate around on the left/right side. In this state we can switch between idle, walk/run and jumping animations. As our hero is always ready to shoot we will add the aiming animations like in the video above. So, our hero should always aiming while it’s staying idle, walking/running or jumping.
You can add fire animation state if you want.
Now about dying. The hero can dye (now) in two cases:
- It’s fall on the floor (which now leads to the game over)
- It was collided with pendulums from the last lesson. When one of this event is occurred we need:
- Enable 3rd-view camera
- Enable RagDoll (enable Dynamic physics for our bones).
As it’s explained in the video when we use kinematic rigidbody or character controller we can only detect collisions but the don’t affects on us. So the pendulums will collide but won’t enforce us. So I suggest you the next behaviour: detect with trigger the collision state with pendulums and enable in this moment Dynamic rigidbody. The detection trigger size should be a bit bigger than real bones: in this case you can enable the Dynamic rigidbody and pendulum will collide them in Dynamic state.