Animation Graph
Updated
The Animation Graph component (comp-animation-graph) is a state machine over the animation layers of the entity it is on. A state machine is a set of named situations, called states, of which exactly one is current at any time, plus rules, called transitions, that say how to get from one state to another. For a character, the states might be Idle, Walking and Waving; for a door, Closed and Open.
Each state runs actions on layers as it is entered and as it is left: play Walk, stop Idle. Transitions move between states when a bus signal arrives, and only then. Use a graph when animations change in response to something: a door that opens on a button, a character that waves when greeted, a panel that reveals and hides. The graph replaces the bookkeeping code you would otherwise write in a script; a script then only needs to send signals.
Before you start
Section titled “Before you start”- A graph plays layers of its own entity: the layers of an Animation Timeline and the clips of an Animation Clip. Read those pages first.
- Transitions listen for signals on the bus. Signals come from Signal Emitters, from a script’s
emit, and from timeline signal keys. See Scripting. - Scripts reach the component with
getComponent(AnimationGraphComponent).
Properties
Section titled “Properties”| Prop | Type | Default | Description |
|---|---|---|---|
entry |
string | '' |
The state it starts in; empty, the first state. |
signals |
array of { name } |
[] |
The signals the graph tool offers for transitions. No effect at runtime. |
states |
array of states | [] |
Its states. |
transitions |
array of transitions | [] |
Its transitions. |
| Field | Type | Default | Description |
|---|---|---|---|
name |
string | required | Unique within the graph. |
enter |
array of actions | [] |
Run as the state is entered. |
exit |
array of actions | [] |
Run as a transition leaves the state. |
x, y, locked, color |
Layout and appearance in the graph tool. No effect at runtime. |
Action
Section titled “Action”| Field | Type | Applies to | Description |
|---|---|---|---|
type |
'play' | 'stop' | 'pause' | 'resume' | 'seek' |
all | What it does to the layer. |
layer |
string | all | The layer’s name. The graph looks in the entity’s Animation Timeline first, then its Animation Clip. |
time |
number | seek |
Seconds from 0. |
fade |
number | play, stop |
Seconds to blend in or out. Absent, the layer’s default (0.2 s for a clip, none for a timeline layer). |
repeat |
'once' | 'repeat' | 'pingpong' |
play |
How this play repeats. Absent, once, or the Animation Clip’s repeat for a clip. |
repeatCount |
number | play |
How many times a repeating play plays in all, a whole number from 1. Absent, forever. |
Transition
Section titled “Transition”| Field | Type | Default | Description |
|---|---|---|---|
from, to |
string | required | The states it leaves and enters. At most one transition each way between two states. |
signals |
array of { name } |
[] |
The bus signals that take it: the only thing that does. |
delay |
number | 0 |
Seconds from its signal to taking it. During the delay the state carries on; another transition’s signal takes that one instead. |
Bad data (two states with one name, a transition to a state that does not exist, an unknown action type) is refused when the props are set, with a TypeError. A signal is always an object { name }; a plain string is refused.
How it runs
Section titled “How it runs”- The graph leads. While an enabled graph is on the entity, in play, only its states play the entity’s layers. The clip’s and the timeline’s
autoplayplay nothing. - Entering a state. As its entity starts, the graph enters
entryand runs its enter actions. Taking a transition runs the current state’s exit actions, then the next state’s enter actions, and sendsAnimationGraph::state-change. - Signals. A signal takes the transition out of the current state that lists it. With no such transition, it is ignored.
- One graph or all. A bus signal (from
emit) reaches every graph listening for it.graph.emit(signal)reaches this graph only. - Crossfades. An exit
stopand an enterplaywith the samefadeblend the two layers over one window. - Moving on when a layer ends. Transitions take signals only. For a timeline layer, put a signal key at its end. For a clip, listen for
AnimationClip::finishedin a script and callgraph.emit. - Late layers. A model’s clips exist only once it loads. When they arrive, the current state’s enter actions run again on them.
- Restarts. Switching the entity off leaves the state; switching it back on starts again from
entry.
Script members
Section titled “Script members”| Member | What it does |
|---|---|
graph. |
Sends signal to this graph only, as if heard on the bus. |
graph.state |
The name of the current state; undefined until the graph runs. |
The global emit(signal) reaches every graph listening for that signal.
Events
Section titled “Events”| Event | EventType |
Data | When |
|---|---|---|---|
Animation |
Event |
{ state, previous } |
The graph enters a state. previous is '' for the first state. |
It goes through the Signal Emitters on the graph’s entity.
Examples
Section titled “Examples”The examples share one character: a model with clips Idle, Walk and Wave, an Animation Clip, a Signal Emitter with signal: 'mike', and an Animation Graph:
{ "entry": "Idle", "states": [ { "name": "Idle", "enter": [{ "type": "play", "layer": "Idle", "fade": 0.3 }] }, { "name": "Walking", "enter": [{ "type": "play", "layer": "Walk", "fade": 0.3, "repeat": "repeat" }] }, { "name": "Waving", "enter": [{ "type": "play", "layer": "Wave", "fade": 0.2, "repeat": "once" }] } ], "transitions": [ { "from": "Idle", "to": "Walking", "signals": [{ "name": "mike/walk" }] }, { "from": "Walking", "to": "Idle", "signals": [{ "name": "mike/stop" }] }, { "from": "Idle", "to": "Waving", "signals": [{ "name": "greet" }] }, { "from": "Waving", "to": "Idle", "signals": [{ "name": "mike/done" }] } ]}1. Build a door graph in code
Section titled “1. Build a door graph in code”A script can add a graph like any component. This one gives a door with an open timeline layer a two-state graph, then opens and closes it on a shared signal.
#pragma lifecycle(startup)
function startup() { const graph = new AnimationGraphComponent({ entry: 'Closed', states: [ { name: 'Closed', enter: [{ type: 'stop', layer: 'open', fade: 0.8 }] }, { name: 'Open', enter: [{ type: 'play', layer: 'open' }] }, ], transitions: [ { from: 'Closed', to: 'Open', signals: [{ name: 'door/toggle' }] }, { from: 'Open', to: 'Closed', signals: [{ name: 'door/toggle' }], delay: 0.2 }, ], }); self.parent.addComponent(graph);
on('door/toggle', () => log('Door toggled'));}A door with an open timeline layer gets a two-state Animation Graph from a Behavior script, and one shared signal from a button opens and closes it.
Press Toggle door to open the door, and again to close it. Drag to orbit the camera, scroll to zoom.
- Both transitions listen for
door/toggle; only the one leaving the current state is taken, so one signal toggles the door. - Entering
Closedstops theopenlayer with a 0.8 second fade, which blends the door back to its pose from before the layer played. - The return transition waits 0.2 seconds after its signal.
- Anything can now open every door with this graph: a button’s Signal Emitter with
signal: 'door/toggle', oremit('door/toggle')from another script.
Snippets
Section titled “Snippets”1. Drive the graph from the keyboard
Section titled “1. Drive the graph from the keyboard”The script only translates input into signals. The graph decides what plays.
#pragma lifecycle(update)
const graph = self.getComponent(AnimationGraphComponent);let walking = false;
function update() { const held = Input.keyboard.isKeyPressed(Key.W); if (held !== walking) { walking = held; graph.emit(walking ? 'mike/walk' : 'mike/stop'); } if (Input.keyboard.isKeyReleased(Key.G)) graph.emit('greet');}graph.emitsends the signal to this character’s graph only. With the globalemit, every graph listening formike/walkwould react.- The script sends a signal only when the key state changes, not every frame.
- A signal with no transition out of the current state is ignored: pressing G while walking does nothing, because only
Idlehas a transition ongreet.
2. Return to idle when a clip finishes
Section titled “2. Return to idle when a clip finishes”Transitions take signals only, so a script turns the clip’s finished event into one. It also logs every state change.
#pragma lifecycle(startup)
const graph = self.getComponent(AnimationGraphComponent);
function startup() { on('mike', (event) => { if (event.eventType === EventType.AnimationClip.Finished && event.data.clip === 'Wave') { graph.emit('mike/done'); } if (event.eventType === EventType.AnimationGraph.StateChange) { log(event.data.previous || '(start)', '->', event.data.state); } });}- The
Wavingstate playsWavewithrepeat: 'once', so the clip sendsAnimationClip::finishedat its end. - The script checks
event.data.clip, because other one-shot clips would send the same event. state-changecarriespreviousandstate;previousis an empty string for the first state, so||shows a readable label.
3. Greet when the player comes near
Section titled “3. Greet when the player comes near”Any source can send a graph’s signals. This script, on a separate trigger entity, greets the character the first time the camera comes within three metres, and only when the character is idle.
#pragma lifecycle(startup, update)
const RADIUS = 3;let graph;let mike;let greeted = false;const eye = new THREE.Vector3();const spot = new THREE.Vector3();
function startup() { mike = findEntity('Mike'); graph = mike?.getComponent(AnimationGraphComponent);}
function update() { if (!graph || !camera) return; camera.getWorldPosition(eye); mike.getWorldPosition(spot); const near = eye.distanceTo(spot) < RADIUS;
if (near && !greeted && graph.state === 'Idle') { greeted = true; graph.emit('greet'); } if (!near) greeted = false;}findEntityruns once instartup, never inupdate, because looking up by name walks the scene.camerais the camera the player sees through; it isundefineduntil rendering starts, so the script checks it. Both positions are read in world space withgetWorldPosition, into vectors made once so nothing is allocated each frame.graph.statetells whether the character is idle, so the greeting never interrupts a walk.greetedresets once the player walks away, so the character greets again on the next approach.
Common mistakes
Section titled “Common mistakes”- Expecting autoplay with a graph. An enabled graph takes over in play: put the first animation in the entry state’s
enteractions. - Waiting for a layer to end on its own. Transitions take signals only. Add a signal key at the end of a timeline layer, or turn
AnimationClip::finishedinto a signal from a script. - Using
emitfor one character. The globalemitreaches every graph listening for the signal. Usegraph.emitto address one. - Signals written as strings. In props, a transition’s signals are objects:
signals: [{ name: 'greet' }]. - Layer names that do not exist. An action names a layer of the same entity. Check clip names with the
AnimationClip.Readyevent’sclips. - Testing in the editor. The editor never runs a graph.