Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 36 ยท Animation and Audio

Animation Clips and Animation Events in Unity

Create animation clips in Unity's Animation window, edit curves, add animation events that call C# methods, and learn easing and tweening with runnable curve stand-ins.

Not everything needs a skeleton. A door that swings open, a coin that spins, a button that pulses or a platform that moves back and forth are all animation clips you can make inside Unity with the Animation window. This lesson shows how to record keyframes, shape them with curves, call your code from the middle of a clip with animation events, and use code-driven tweening when a clip would be overkill.

The curve math runs here as plain C#; the editor steps and Unity API code are read-only.

Recording a clip

  1. Select a game object, open Window > Animation > Animation, and click Create. Unity asks for a clip name and creates an Animator and a controller for you.
  2. Click Add Property and choose what to animate: Transform position, rotation, scale; a SpriteRenderer color; a Light's intensity; any public field of a script on the object.
  3. Move the playhead to a time and change the value in the Inspector (record mode adds a keyframe automatically).
  4. Use the Curves tab to see each property as a graph, and the loop checkbox on the clip asset (Loop Time) to repeat it.

Keyframes are not positions of every frame. Unity interpolates between them using the curve. A tangent at each key controls the shape: Auto gives a smooth flow, Linear a constant speed, and Constant (stepped) holds the old value then jumps. A flat tangent at both ends gives an ease in / ease out feel.

flowchart LR
  A["Keyframe 0:00<br/>y = 0"] -->|"Auto tangent"| B["Keyframe 0:30<br/>y = 2"]
  B -->|"Linear"| C["Keyframe 1:00<br/>y = 0"]

The curve between keys decides the feel, not just the key values.

Curves in code

AnimationCurve is a serializable type you can edit in the Inspector and sample in a script: curve.Evaluate(t). It is perfect for damage falloff, difficulty ramps or custom easing. The stand-in below samples the classic easing functions so you can see how they differ at the same time values.

C# Stand-in: easing functions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

EaseOutBack overshoots 1 before settling, which is why it makes pop-up menus feel lively. A clip with these curves and the Unity function Mathf.SmoothStep or Mathf.Lerp give the same results.

Animation events

Sometimes something must happen at an exact moment of a clip: a footstep sound when the foot touches the ground, a damage hitbox on frame 12 of a sword swing, a particle burst when a door finishes opening. Animation events are markers on the clip's timeline that call a method on a script attached to the same game object.

In the Animation window, move the playhead, click Add Event and choose the function. The method must be public-ish (Unity finds it by name) and may take one parameter: a float, int, string, an object reference or an AnimationEvent.

C#
using UnityEngine;

public class SwordAttack : MonoBehaviour
{
    [SerializeField] Collider2D hitbox;
    [SerializeField] AudioSource swing;

    // called by an animation event at frame 8
    public void EnableHitbox()  => hitbox.enabled = true;

    // called by an animation event at frame 14
    public void DisableHitbox() => hitbox.enabled = false;

    // called at the start; the event's float parameter is the volume
    public void PlaySwing(float volume) => swing.PlayOneShot(swing.clip, volume);
}

Animation events are fragile in two ways. If you rename a method, the event silently becomes an error message at runtime ("has no receiver"). And if a transition blends out before the event time, the event may never fire. For that reason, keep events for tightly synchronized effects and drive everything else from state changes. The sample below shows the idea: a timeline of events fires as the playhead passes each one.

C# Stand-in: events fired by a playhead

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The loop checks "events between the previous time and now", exactly like Unity does, so a low frame rate never skips an event.

Tweening from code

For UI polish, damage numbers and camera shakes, many developers skip clips and use code. The simplest tool is a coroutine with Mathf.Lerp or Mathf.SmoothStep:

C#
using System.Collections;
using UnityEngine;

public class Pop : MonoBehaviour
{
    public IEnumerator PopIn(float duration)
    {
        float t = 0;
        while (t < 1)
        {
            t += Time.deltaTime / duration;
            float s = Mathf.SmoothStep(0, 1, t);
            transform.localScale = Vector3.one * s;
            yield return null;
        }
        transform.localScale = Vector3.one;
    }
}

Start it with StartCoroutine(PopIn(0.25f)). Libraries such as DOTween and LitMotion provide chained tweens with ease names, but the idea is exactly this: a normalized time t from 0 to 1, shaped by an easing function, applied to a value.

Which tool when?

  • Animator + clips: characters, anything an artist authors, anything with transitions.
  • Single clip played from code (Animator.Play("Open")) or the Timeline: cutscenes, doors, platforms; see Timeline and Cinemachine.
  • Code tweens: UI feedback, procedural motion, values that depend on gameplay.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does an animation event do?

    Events are markers that invoke a named method, optionally with one parameter.
  2. 2. Which curve tangent holds a value and then jumps to the next key?

    A constant tangent gives a step: no interpolation between keys.
  3. 3. Why can animation events be unreliable?

    If the state is left early the event never fires, and renamed methods cause runtime errors.
  4. 4. What does EaseOutBack do compared with EaseOutQuad?

    The back easing goes past 1 and returns, giving a springy feel.

Summary

  • The Animation window records keyframes of any property; curves and tangents shape the motion.
  • AnimationCurve and easing functions shape values in code too.
  • Animation events call script methods at exact clip times; use them sparingly and keep names in sync.
  • A normalized time shaped by easing powers code tweens when a clip is too heavy.

Next: Audio Sources and the Mixer