Skip to content

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.

  • A Behavior script reaches the component with self.getComponent(AnimationClipComponent) when the script is on the model entity, or findEntity('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 signal set to a name such as hero. See Scripting for on, emit and lifecycle hooks.
  • The model loads asynchronously, so its clips may not exist yet when your script starts. Every example handles that.
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.

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?, repeatCount? }) 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().
setSpeed(speed) 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 no fade. Pass fade: 0 for 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 repeat uses the component’s repeat prop.

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.

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.
setSpeed(speed) Set the component’s speed at once; the clip plays on.
setInPlace(on, options?) 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.keepRootPosition 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.AnimationMixer, 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.
useClipLibrary(ref) 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.
loadClipLibrary(ref, names) Async. Read compatible clips by exact name from another glTF file, as independent copies.
installClips(clips) Register THREE.AnimationClip objects on the loaded model; returns their names. The model’s own clips win when names match. Pass [] to remove them.
retargetClips(motions) Install neutral humanoid motion on the model’s Skeleton rig; returns the installed names.

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.

The component sends these through the Signal Emitters on its entity:

Event EventType Data When
AnimationClip::ready EventType.AnimationClip.Ready { clips }, the clip names The model’s clips are ready and mixer exists; again after each reload.
AnimationClip::repeat EventType.AnimationClip.Repeat { clip } A repeating play wraps.
AnimationClip::finished EventType.AnimationClip.Finished { clip } A one-shot clip reaches its end.

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.

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.

IdleWalk.jx
#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);
}
}
  • The Animation Clip’s layer prop is Idle, 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')?.setSpeed changes 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.mixer at startup as well as listening for ready.

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.

ClipList.jx
#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();
});
}
  • anim is read once, at the top level, where self.parent is already available.
  • anim.mixer exists 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 checks eventType against the EventType constant and ignores the rest.
  • event.data.clips is the list of clip names: copy the exact names from the log into your scripts.
  • layer('Wave') returns undefined when there is no such clip, so the script checks it before calling play.

A clip that should play once, then hand back to a loop, uses repeat: 'once' and the finished event.

Jump.jx
#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' });
}
}
  • isKeyReleased is true for one frame per key release, so one tap starts one jump. isKeyPressed would be true every frame the key is held.
  • repeat: 'once' makes the jump hold its last frame and send AnimationClip::finished with data.clip set to 'Jump'.
  • The busy flag ignores new presses while the jump is playing.
  • Playing Idle with a fade crossfades 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.

WalkSpeed.jx
#pragma lifecycle(startup, update)
const CLIP_SPEED = 1.4; // metres a second the Walk clip was animated for
const 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 dt to get its speed in metres a second.
  • dt === 0 happens while the game is paused, so the script skips the frame instead of dividing by zero.
  • The moving flag switches between Idle and Walk only when the state changes.
  • setSpeed on the Walk layer changes that clip’s own multiplier at once, without a rebind. Math.min keeps it within a sensible range.

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.

ClipScrubber.jx
#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 Wave with fade: 0, then pauses it, so it shows the clip’s first frame at full weight.
  • Holding the arrow keys moves time by half a second per second. The script pauses the clip and seeks only on frames where a key is held: seek on 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.
  • Playing before the model loads. layer(name) is undefined until the clips are ready. Check anim.mixer and also listen for AnimationClip.Ready, as the examples do.
  • Keeping a layer across reloads. A model reload, or any setProps other than speed and rate, builds new layers. A layer kept from before does nothing. Fetch layers again after ready.
  • Calling play every frame. play starts from the beginning; on a one-shot clip that means it never gets past its first frame. Track a state and call play only when it changes.
  • Clip names that do not match. Names are case-sensitive and come from the model. Log event.data.clips once 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.