Course contentsShow
← Unity Game Development
Lesson 428 of 8286. Animation and CinemachineFree lesson

Creating Dynamic Look-At Constraints

Learners will configure Look-At constraints to make a character's head or torso track moving targets or points of interest.

In this lesson, you will learn how to use the Animation Rigging package to create dynamic Look-At constraints that allow your characters to naturally track moving targets or points of interest.

The Logic of Look-At Constraints

While Inverse Kinematics (IK) handles the positioning of limbs, Look-At Constraints handle the rotation of a specific transform (like the head or eyes) to face a target transform. Instead of manually animating every frame of a character looking around, we define a "source" (the object to follow) and a "constrained object" (the head or neck joint). The system then calculates the necessary rotation to keep the character's forward vector aligned with the target.

Setting Up the Rig

To implement this, ensure you have the Animation Rigging package installed.

  1. Create the Rig Builder: On your character root, add a RigBuilder component.
  2. Setup the Rig: Create an empty GameObject under your character named "Rig," add a Rig component to it, and assign it to the RigBuilder's Rig Layers list.
  3. Add the Constraint: Under your "Rig" object, create a child object, add a Multi-Aim Constraint component (which handles Look-At logic).
  4. Configure the Constraint:
    • Constrained Object: Drag your character’s head or neck bone here.
    • Source Objects: Add the target GameObject (e.g., an enemy or a floating point of interest) to the list.
    • Aim Vector: Set this to your character's forward axis (usually (0, 0, 1)).
    • Up Vector: Set this to (0, 1, 0) to maintain head orientation.

Fine-Tuning with Weight

The Weight property on the constraint is key to natural movement. If you want the character to stop looking at an object abruptly, setting the weight to 0 will snap them back to their original animation. To keep it smooth, you can script a simple transition:

// Smoothly transition the look-at weight
multiAimConstraint.weight = Mathf.MoveTowards(multiAimConstraint.weight, targetWeight, Time.deltaTime * speed);

By leveraging these constraints, your character appears "aware" of their environment, creating a much more immersive experience without the need for manual keyframing.

Key Takeaway: Look-At constraints leverage the Animation Rigging system to procedurally rotate bones toward a target, allowing for dynamic character awareness that updates in real-time.