Mastering Unity AnimationCurve for Procedural Motion

Overview

The AnimationCurve class in Unity provides a powerful way to define custom data transformations over time or space. It is widely used for creating non-linear movement, custom easing functions, terrain modulation, and parametric animation behaviors. By utilizing the built-in editor, developers can visually design curves that influence game logic, such as modifying gravity values or scaling colliders dynamically.

Editor Integration

To use an AnimationCurve in you're component, declare it as a public or serialized field:

public AnimationCurve velocityProfile;

Once assigned in the Inspector, clicking the curve icon opens the dedicated editor window. Key operations include:

  • Key Management: Double-click to insert a new key; delete keys via the context menu or by selecting a point and pressing the Delete key.
  • Tangent Modes: Right-click a key to set its interpolation style. Modes include Auto for smooth interpolation, Free Smooth for independent tangent handles, Flat for locked horizontal tangents, Broken for independent entry and exit slopes, and Constant for discrete step-based values.
  • Wrap Modes: Define behavior outside the defined time range using PreWrapMode and PostWrapMode, such as Loop (cycling) or PingPong (bouncing).

To apply the curve data, use the Evaluate(float time) method:

void Update() {
    float currentOffset = velocityProfile.Evaluate(Time.time);
    transform.position = new Vector3(Time.time, currentOffset, 0);
}

Programmatic Implementation

Curves can be generated or modified at runtime. A curve consists of an array of Keyframe objects and wrap mode settings.

using UnityEngine;

public class DynamicCurveGenerator : MonoBehaviour {
    private AnimationCurve customCurve;

    void Start() {
        Keyframe[] frames = new Keyframe[] {
            new Keyframe(0f, 0f, 0f, 0f),
            new Keyframe(2f, 5f, 2f, 2f),
            new Keyframe(4f, 0f, 0f, 0f)
        };
        
        customCurve = new AnimationCurve(frames);
        customCurve.postWrapMode = WrapMode.Loop;
    }

    void Update() {
        float yPos = customCurve.Evaluate(Time.time % 4f);
        transform.position = new Vector3(transform.position.x, yPos, 0);
    }
}

Key API Methods

  • Evaluate(float time): Returns the interpolated value at the specified point.
  • AddKey(float time, float value): Dynamically appends a new keyframe.
  • MoveKey(int index, Keyframe key): Updates an eixsting keyframe's properties.
  • RemoveKey(int index): Deletes a keyframe at the specified index.
  • SmoothTangents(int index, float weight): Automatically adjusts tangent handles to create a smooth transition around a specific keyframe.

Tags: unity AnimationCurve C# game development Procedural Animation

Posted on Thu, 04 Jun 2026 16:39:30 +0000 by lorne17