top of page

Procedural Animation

Engine : Unity

Tools : Unity

Created with animation rigging to make character have ik physics and have procedural placement for leg.

Script
Explanation
Setup
We’re going to build a cool procedural animation system for a spider-like robot. Imagine a robot that moves its legs dynamically using code — no pre-recorded animations needed!
Step 1: Install the Animation Rigging Package
Open Unity’s Package Manager, search for Animation Rigging, and install it.
Once that’s done, you’ll see a new “Animation Rigging” menu appear in the top menu bar — this gives you access to tools for setting up bones and IK (Inverse Kinematics).
Step 2: Visualize Your Bones
To make things easier, let’s see the bones in your character model.
  1. Select the main armature/root of your 3D model (the top-level GameObject of your imported rig).
  2. In the Animation Rigging menu, click “Bone Renderer Setup.”
This adds a visual overlay of all bones in your model, making it easier to understand how everything moves. You’ll now have “X-ray vision” of the skeleton!​
Step 3: Add a Rig and Create the IKs
Now it’s time to actually control the legs with IK.
  1. Go to the Animation Rigging menu again, and click “Rig Setup.”
    • This adds two things:​
      • A Rig Builder component to your armature​
      • A new Rig1 GameObject inside your character’s hierarchy
  2. Inside Rig1, create an Empty GameObject. Name it something like Foot.001-IK.
    • This will be the controller for one leg.​
  3. On that object, add a component: Two Bone IK Constraint.​
    • This component will help bend the leg in a realistic way.​
  4. You’ll see some reference fields: Tip, Mid, Root, etc.​
    • Only set the Tip (the final bone in the leg).​
    • Then, right-click the component title and select “Auto Setup from Tip Transform.”
    • Unity will automatically fill in the rest — super handy!
Step 4: Understand the “Target” and “Hint”
Unity also creates two helper objects:
  • A Target: this is where the foot will try to move to.
  • A Hint: helps determine how the leg bends (like a knee guide).
You can style them visually:​
  • Make the target look like a small green cube.
  • Make the hint a larger red circle.
  • Place the hint a bit above the leg to get natural bending.
Now, if you press Play and move the target, the leg follows it — the animation is live and procedural!​
Step 5: Repeat for All Legs
Repeat steps 3 and 4 for each of the spider’s legs.
Name them accordingly (e.g. Foot.002-IK, Foot.003-IK, etc.), and soon you’ll have a full procedural walking spider with all legs moving independently and realistically!
Coding
We’re creating a system that moves the robot’s limbs (like feet) automatically as it walks — without manually animating every step. This is called procedural animation.
The idea is:
  • The robot's body moves (by AI or player input).
  • The feet follow and adjust automatically based on how far they are from where they should be.
  • We also use raycasts to make sure the feet are always on the ground.
How It Works, Step by Step
  1. Structure Setup
    We make a ProceduralAnimator.cs script and attach it to the main robot GameObject.
    We also define a small helper class to keep track of each leg:


    class ProceduralLimb
    {
       
    public Transform IKTarget;       // Where the foot should be
       
    public Vector3 defaultPosition;  // Its original "resting" spot
       
    public Vector3 lastPosition;     // Where the foot was last frame
       
    public bool moving;              // Is this foot currently stepping?
    }


    Then we prepare an array of legs:

    [
    SerializeField] private Transform[] _limbTargets;
    private ProceduralLimb[] _limbs;

    In Start(), we fill this array with each leg’s info.
  2. Detect Movement
    In FixedUpdate(), we check how fast the body is moving:

    _velocity = transform.position - _lastBodyPosition;
    _lastBodyPosition = transform.position;


    If we’re moving, we calculate where each foot should be based on the new body position. If any foot is too far from this "ideal spot", we flag it to move.
  3. Move One Foot at a Time
    We pick only one foot to move per frame — the one that’s farthest from where it should be. The rest of the feet stay still to keep balance:


    if (limbToMove != -1)
    {
        Vector3 target = desiredPositions[limbToMove];
        _limbs[limbToMove].IKTarget.position = target;
        _limbs[limbToMove].lastPosition = target;
    }
  4. Keep Feet on the Ground (Raycasts)
    To make sure the foot touches the terrain (even if it's bumpy), we use a raycast:

    Vector3 _RaycastToGround(Vector3 pos, Vector3 up)
    {
        Ray ray =
    new Ray(pos + _raycastRange * up, -up);
       
    if (Physics.Raycast(ray, out RaycastHit hit, 2f * _raycastRange, _groundLayerMask))
           
    return hit.point;
       
    return pos;
    }


    Now we apply that to the target point, so the feet always land correctly on the floor.
  5. Return to Rest When Idle
    When the robot stops moving, we slowly return the legs to their original "resting" positions:


    if (_velocity.magnitude < smallValue)
    {
       
    for each leg:
           
    if it's not resting:
                move it back
    }
  6. Bonus Features
    You can improve realism by:
  • Overshooting the target position slightly, using the body’s velocity.​
  • Using coroutines to animate the leg movement over time instead of teleporting.

Imagine your robot is walking:​
  • Its feet should stay under its body.
  • If one foot stretches too far, it picks up and moves.
  • It always places the foot on the ground (thanks to raycasting).
  • When it stops, the feet go back to a natural stance.
  • All of this happens automatically using math and Unity’s physics.
  • Mail
  • telephone
  • LinkedIn
bottom of page