Animation Clip
Updated
The Animation Clip component (comp-animation-clip) plays the animation clips stored inside a loaded model: a character’s idle, walk, run or wave. A clip is a recorded piece of motion that the artist made in a modelling tool and exported with the model. You do not author clips in MIRA; you choose which one plays, when, how fast and how it blends with the others.
Add the component to a model entity: glTF, FBX, Collada, OBJ, USDZ or a kit piece. Once the model loads, the component prepares its clips and, with autoplay, starts one. Use it for motion that comes with the model file. For motion you author yourself, use an Animation Timeline; to switch clips on events without code, add an Animation Graph.
Before you start
Section titled “Before you start”- A Behavior script reaches the component with
self.getComponent(AnimationClipComponent)when the script is on the model entity, orfindEntity('Name').getComponent(AnimationClipComponent)from elsewhere. See Working with entities. - Component events reach scripts only through a Signal Emitter on the same entity. The examples on this page assume one with its
signalset to a name such ashero. See Scripting foron,emitand lifecycle hooks. - The model loads asynchronously, so its clips may not exist yet when your script starts. Every example handles that.
Properties
Section titled “Properties”| Prop | Type | Default | Description |
|---|---|---|---|
layer |
string | none | The clip to autoplay, by name. Empty or absent, the model’s first clip. |
autoplay |
boolean | true |
Start the clip as soon as the model loads. In play, an enabled Animation Graph on the entity overrides it. |
repeat |
'once' | 'repeat' | 'pingpong' |
'repeat' |
How a play that gives no repeat of its own repeats, autoplay included. once plays once and holds the last frame; repeat wraps to the start; pingpong goes back and forth. |
speed |
number | 1 |
Playback speed of every clip, from −100 to 100. Negative plays backwards; 0 holds the pose. |
rate |
'adaptive' | 'full' |
'adaptive' |
How often the model animates. adaptive animates less often far from the camera; full animates every frame. |
Change props with setProps. Every prop except speed and rate rebinds the model’s clips: new layers, a new ready event, and the autoplay clip from the start. Change them between plays, not every frame. speed and rate apply at once and the clip plays on.
Layers
Section titled “Layers”Each clip of the model is a layer, the same kind of object an Animation Timeline holds. Get one by its clip name with anim.layer(name).
| Call | What it does |
|---|---|
play({ fade?, repeat?, repeat |
Play from the start. fade: seconds to blend in. repeat: 'once', 'repeat' or 'pingpong'. repeatCount: how many times a repeating play plays in all before it holds its end; Infinity (the default) is forever. |
stop({ fade? }) |
Stop, and put the model back as it was before the clip played. fade: seconds to blend out first. |
pause(), resume() |
Freeze the clip where it is; carry it on. Other clips keep playing. |
seek(time) |
Move to time seconds, shown at once. If the clip is not playing, it waits there, paused, until resume(). |
set |
Change this clip’s own speed multiplier, from −100 to 100. The component’s speed still applies on top. |
playing |
false while paused, fading out, once stopped, or once a one-shot reached its end. |
name |
The clip’s name. |
Clip layers have three defaults worth knowing:
- Crossfade.
play()blends from the clip playing before it over 0.2 seconds when you give nofade. Passfade: 0for a hard cut. - No restart. Playing a repeating clip that is already playing lets it play on instead of restarting it.
- Repeat. A play that gives no
repeatuses the component’srepeatprop.
anim.layer() with no name is every clip as one: each call goes to all of them, and it is playing while any is. anim.layer().play() starts every clip together at full weight; anim.layer().pause() freezes the whole model.
Component members
Section titled “Component members”| Member | What it does |
|---|---|
layer(name) |
The clip layer name, or undefined before the model loads or when there is no such clip. |
layer() |
Every clip as one layer. |
set |
Set the component’s speed at once; the clip plays on. |
set |
Whether clips play on the spot. true (the default) drops the root bone’s travel, so a controller or physics moves the entity instead. options. lists clip names that keep their root position track, such as ['Idle']. Not saved and not sent over the network: set it from a script on each client. |
mixer |
The model’s THREE., read-only, for what layers do not cover, such as reading an action’s time. undefined until the clips are ready, and replaced on each reload. Never call its update(): the component does. |
isModelBound |
Whether a loaded model is ready to receive clips. |
observe(listener) |
Call listener() each time the layers are rebuilt; returns a function that stops listening. |
use |
Async. Bind the compatible clips of another glTF file to this model; resolves to the clip names now available to layer(name). Pass null to go back to the model’s own clips. |
load |
Async. Read compatible clips by exact name from another glTF file, as independent copies. |
install |
Register THREE. objects on the loaded model; returns their names. The model’s own clips win when names match. Pass [] to remove them. |
retarget |
Install neutral humanoid motion on the model’s Skeleton rig; returns the installed names. |
Root motion
Section titled “Root motion”By default, clips play in place: a walk cycle moves the legs, but the root bone’s forward travel is dropped, so a character controller or physics moves the entity. If you keep root motion and also move the entity with a controller, the character double-moves and shakes. Call anim.setInPlace(false) only when the clip itself should carry the model, as in a cinematic.
Events
Section titled “Events”The component sends these through the Signal Emitters on its entity:
| Event | EventType |
Data | When |
|---|---|---|---|
Animation |
Event |
{ clips }, the clip names |
The model’s clips are ready and mixer exists; again after each reload. |
Animation |
Event |
{ clip } |
A repeating play wraps. |
Animation |
Event |
{ clip } |
A one-shot clip reaches its end. |
Adaptive update rate
Section titled “Adaptive update rate”With rate: 'adaptive', a model far from the camera (the headset, in XR) animates only every few frames:
| Distance from the camera | Animates |
|---|---|
| under 20 m | every frame |
| from 20 m | every 2nd frame |
| from 40 m | every 4th frame |
| from 80 m | every 8th frame |
The distance is divided by the camera’s zoom. A skipped frame’s time is not lost: the model catches up by the time it missed, so it keeps its speed. Use rate: 'full' for a hero character or a close-up where a lower rate would show.
Examples
Section titled “Examples”1. Crossfade between idle and walk
Section titled “1. Crossfade between idle and walk”A character with Idle and Walk clips stands idle until Space blends it into the walk, and back. The arrow keys change the walk’s own speed.
#pragma lifecycle(startup, update)
const anim = self.parent.getComponent(AnimationClipComponent);let walking = false;let speed = 1;
function startup() { if (anim.mixer) log('The clips are ready'); on('hero', (event) => { if (event.eventType === EventType.AnimationClip.Ready) log('Clips:', event.data.clips.join(', ')); });}
function update() { if (!anim.mixer) return; // the model is still loading
if (Input.keyboard.isKeyReleased(Key.Space)) { walking = !walking; anim.layer(walking ? 'Walk' : 'Idle')?.play({ fade: 0.4 }); log(walking ? 'Walking' : 'Standing'); }
let change = 0; if (Input.keyboard.isKeyReleased(Key.ArrowUp)) change = 0.25; if (Input.keyboard.isKeyReleased(Key.ArrowDown)) change = -0.25; if (change !== 0) { speed = Math.min(3, Math.max(0.25, speed + change)); anim.layer('Walk')?.setSpeed(speed); log('Walk speed', speed); }}A character stands idle; Space crossfades it into its Walk clip and back through the Animation Clip layers, and the arrow keys change the walk's speed.
Click the scene, then press Space to start or stop walking, and the Up and Down arrows to change the walk speed. Drag to orbit the camera, scroll to zoom.
- The Animation Clip’s
layerprop isIdle, so autoplay starts the idle clip as soon as the model loads. play({ fade: 0.4 })crossfades from the clip playing before, over 0.4 seconds, in both directions.layer('Walk')?.setSpeedchanges only the walk’s own multiplier, so the idle clip keeps its speed.- The model may load before the script starts, so the script checks
anim.mixerat startup as well as listening forready.
Snippets
Section titled “Snippets”1. List the clips and play one
Section titled “1. List the clips and play one”The first thing to do with an unfamiliar model is find out what its clips are called. This script logs them and plays one as soon as they exist.
#pragma lifecycle(startup)
const anim = self.parent.getComponent(AnimationClipComponent);
function playWave() { const wave = anim.layer('Wave'); if (!wave) { log('This model has no clip named Wave'); return; } wave.play({ repeat: 'repeat', fade: 0.3 });}
function startup() { // Case 1: the model loaded before the script started. if (anim.mixer) playWave();
// Case 2: the model loads (or reloads) later. on('hero', (event) => { if (event.eventType !== EventType.AnimationClip.Ready) return; log('Clips:', event.data.clips.join(', ')); playWave(); });}animis read once, at the top level, whereself.parentis already available.anim.mixerexists only once the clips are ready, so it tells the script whether it can play now.- The
on('hero', …)listener hears every event the entity’s Signal Emitter forwards; the script checkseventTypeagainst theEventTypeconstant and ignores the rest. event.data.clipsis the list of clip names: copy the exact names from the log into your scripts.layer('Wave')returnsundefinedwhen there is no such clip, so the script checks it before callingplay.
2. A one-shot jump that returns to idle
Section titled “2. A one-shot jump that returns to idle”A clip that should play once, then hand back to a loop, uses repeat: 'once' and the finished event.
#pragma lifecycle(startup, update)
const anim = self.parent.getComponent(AnimationClipComponent);let busy = false;
function startup() { on('hero', (event) => { if (event.eventType === EventType.AnimationClip.Ready) { anim.layer('Idle')?.play(); } if (event.eventType === EventType.AnimationClip.Finished && event.data.clip === 'Jump') { busy = false; anim.layer('Idle')?.play({ fade: 0.2 }); } }); if (anim.mixer) anim.layer('Idle')?.play();}
function update() { if (busy || !anim.mixer) return; if (Input.keyboard.isKeyReleased(Key.Space)) { busy = true; anim.layer('Jump')?.play({ fade: 0.15, repeat: 'once' }); }}isKeyReleasedis true for one frame per key release, so one tap starts one jump.isKeyPressedwould be true every frame the key is held.repeat: 'once'makes the jump hold its last frame and sendAnimationClip::finishedwithdata.clipset to'Jump'.- The
busyflag ignores new presses while the jump is playing. - Playing
Idlewith afadecrossfades out of the jump’s last frame instead of snapping.
3. Match the walk speed to the movement speed
Section titled “3. Match the walk speed to the movement speed”When a character moves faster or slower than its clip was made for, its feet slide. Scaling the clip’s speed to the actual movement keeps them planted.
#pragma lifecycle(startup, update)
const CLIP_SPEED = 1.4; // metres a second the Walk clip was animated forconst anim = self.parent.getComponent(AnimationClipComponent);const last = new THREE.Vector3();let moving = false;
function startup() { last.copy(self.parent.position);}
function update(dt) { if (!anim.mixer || dt === 0) return;
const speed = self.parent.position.distanceTo(last) / dt; last.copy(self.parent.position);
const nowMoving = speed > 0.05; if (nowMoving !== moving) { moving = nowMoving; anim.layer(moving ? 'Walk' : 'Idle')?.play({ fade: 0.25 }); } if (moving) anim.layer('Walk')?.setSpeed(Math.min(speed / CLIP_SPEED, 3));}- The script measures how far the entity moved since the last frame and divides by
dtto get its speed in metres a second. dt === 0happens while the game is paused, so the script skips the frame instead of dividing by zero.- The
movingflag switches betweenIdleandWalkonly when the state changes. setSpeedon theWalklayer changes that clip’s own multiplier at once, without a rebind.Math.minkeeps it within a sensible range.
4. Scrub a clip to a pose
Section titled “4. Scrub a clip to a pose”seek with a paused clip turns a clip into a pose library: hold a frame, step through it, or show it at a chosen moment.
#pragma lifecycle(startup, update)
const anim = self.parent.getComponent(AnimationClipComponent);let time = 0;let clip;
function startup() { on('hero', (event) => { if (event.eventType === EventType.AnimationClip.Ready) clip = undefined; });}
function update(dt) { if (!anim.mixer) return; if (!clip) { clip = anim.layer('Wave'); if (!clip) return; clip.play({ fade: 0 }); clip.pause(); }
let step = 0; if (Input.keyboard.isKeyPressed(Key.ArrowRight)) step += dt * 0.5; if (Input.keyboard.isKeyPressed(Key.ArrowLeft)) step -= dt * 0.5; if (step !== 0) { time = Math.max(0, time + step); clip.pause(); clip.seek(time); }
if (Input.keyboard.isKeyReleased(Key.Enter)) clip.resume();}- The script plays
Wavewithfade: 0, then pauses it, so it shows the clip’s first frame at full weight. - Holding the arrow keys moves
timeby half a second per second. The script pauses the clip and seeks only on frames where a key is held:seekon a playing clip would make it play on from there. - Enter resumes playback from the current pose; the arrow keys pause it again.
- On
ready, the script forgets its layer (clip = undefined) and fetches a fresh one on the next frame, because a reload makes new layers.
Common mistakes
Section titled “Common mistakes”- Playing before the model loads.
layer(name)isundefineduntil the clips are ready. Checkanim.mixerand also listen forAnimationClip.Ready, as the examples do. - Keeping a layer across reloads. A model reload, or any
setPropsother thanspeedandrate, builds new layers. A layer kept from before does nothing. Fetch layers again afterready. - Calling
playevery frame.playstarts from the beginning; on a one-shot clip that means it never gets past its first frame. Track a state and callplayonly when it changes. - Clip names that do not match. Names are case-sensitive and come from the model. Log
event.data.clipsonce and copy them. - Autoplay that seems ignored. An enabled Animation Graph on the entity takes over in play: its states decide what plays.
- Events that never arrive. Without a Signal Emitter on the model entity, the clip’s events go nowhere.
- A sliding or jittering character. Root motion kept while a controller also moves the entity makes it double-move. Leave clips in place (the default).
- Calling
anim.mixer.update(). The component advances the mixer; updating it yourself plays the model at double speed.