Procedural Animation in Unity

By Saif Jouda | June 10, 2024

Introduction

What is it?

Procedural animation is a technique in computer graphics where animations are created algorithmically rather than being manually crafted by animators.

How does it work?

Procedural animation generates movements using algorithms and mathematical functions instead of manual keyframes. It works by:

Animation Rigging in Unity

Animation rigging in Unity involves setting up a skeleton (rig) for a character or object, which consists of bones and joints. This rig allows animators to create and control movement. Key components include:

Using Unity's Animation Rigging package, developers can create complex animations by defining these bones, joints, and constraints, facilitating more dynamic and realistic character movements.

Constraint Components

Blend

Function: Blends the transformations of multiple sources (position, rotation, scale) to a single object.

Use Case: Smoothly transitioning an object between different states or animations.

Chain IK

Function: Controls a chain of bones to follow a target position, typically for limbs.

Use Case: Creating natural limb movements, such as a character’s arm reaching for an object.

Damped

Function: Applies a damping effect to an object's transformation, smoothing out sudden movements.

Use Case: Reducing jitter or oscillations in animations, such as camera shake or soft body dynamics.

Multi-Aim

Function: Orients an object to aim at multiple targets, blending between them based on weights.

Use Case: Ensuring an object (like a head or eye) smoothly tracks multiple points of interest.

Multi-Parent

Function: Allows an object to inherit transformations from multiple parent objects.

Use Case: Complex parenting setups where an object needs to follow multiple influences, such as holding an object with both hands.

Multi-Position

Function: Constrains an object’s position to multiple targets, blending their influence.

Use Case: Positioning an object relative to several control points, useful in multi-character interactions.

Multi-Referential

Function: Switches an object’s reference between different targets seamlessly.

Use Case: Changing the parent or reference of an object dynamically, such as switching which hand holds an object.

Multi-Rotation

Function: Blends the rotational influence of multiple targets on an object.

Use Case: Smoothing out rotational transitions between different animations or control points.

Override

Function: Overrides the transformation of an object with specified values or influences.

Use Case: Temporarily changing an object’s transformation for specific actions or states.

Twist Chain

Function: Distributes a twist along a chain of bones evenly.

Use Case: Creating natural twisting motions in body parts like the forearm or spine.

Twist Correction

Function: Corrects unwanted twisting in a bone, ensuring proper deformation.

Use Case: Maintaining the correct shape of a mesh during complex rotations, such as twisting an arm.

Two Bone IK

Function: Controls two connected bones (like upper and lower arm) to reach a target position.

Use Case: Animating joints such as elbows and knees with natural bending and reaching motions.

Blender Setup

Create a Shape

Create the desired shape you want to animate.

Add Armature

Press Shift+A then click on Armature to add the base bone.

Change Viewport Display (Recommended)

To see the bone structure easier, go to Data > Viewport Display, then check the In Front box.

Build the Armature

With the base bone selected in Object Mode, go into Edit Mode. You can:

Press E to create a new bone at the selected bone.

Press G to move and rotate the selected bone.

Connect the Object with the Armature

Shift-click the armature then the object, then press CTRL/CMD+P. Finally, select "With Automatic Weights".

Weight Paint (Optional)

To improve how the bones bend the objects, you can customize how each bone affects the mesh. To do this, select the mesh/object in Object Mode then switch to Weight Paint. Here you can:

Get weight from Blender Manual

Export

Export the project as a .fbx file and place it in your Unity assets folder.

Unity Setup

Get the Package

Get the "Animation Rigging" package from the Unity Registry.

Set Up the Object

The .fbx file you imported should be in a prefab state. Drag and drop it into your scene. While the "Armature" child object is selected, click on "Animation Rigging" > "Rig Setup". This will add some important components to the object. Additionally, if you would like to see the bones, go to "Animation Rigging" > "Bone Renderer Setup".

Animation Rigging Component

In Armature, a Rig child object should have been created. Create a child object for "Rig" for the constraint. Add the desired constraint component on the new object we just created. Each constraint has its own requirements. For this example, we will use the Two Bone IK Constraint. Every constraint requires references to bone objects and source objects. Bone objects are simply the bones in the armature. In this case, we require Root (the base bone), Mid (the second bone), and the Tip (the object at the end of the last bone). You will have to think about the constraint you want and create a bone structure in Blender that aligns with it. In this case, I only need two bones.

We also need source objects which allow us to control our rig. These are typically empty game objects created in the same hierarchy as the object with the constraint. In this case, the target is where we want the tip to be and the hint is where we want the joint to be angled towards.

Additionally, we can visualize any rig object we want with various shapes. Select a rig object then at the bottom right of the scene window, there should be an option to add a visualization. You can change the shape, color, and size. These are only visible in the scene view.

Results

Thanks for Reading!

Check out my blog on Volumetric Lighting