Skip to content

Latest commit

 

History

History
234 lines (171 loc) · 21.1 KB

Animation.md

File metadata and controls

234 lines (171 loc) · 21.1 KB

Animation

The Animator component plays keyframe animations on an entity and its children. A clip can animate any component property the Inspector shows (position, rotation, scale, colors, light intensity, UI rects, sprites ...) and the visible fields of C# scripts. An Animator Controller switches between clips with a state machine driven by parameters that scripts set.

For flip-book sprite animation, assign a looping clip directly to the Animator and switch slices with a discrete Sprite Renderer > Texture track.

The pieces

Asset / component What it is
Animation Clip (.anim) Tracks of keyframes over time, plus animation events.
Animator Controller (.animctrl) States (each plays a clip), parameters and transitions with conditions.
Animator (component) Plays a controller, or a single clip when it has no controller. Its entity is the root that track paths start from.

Create clips and controllers from the Project panel (Create > Animation > Animation Clip / Animator Controller), or from the Animation window: select an entity and click Create Clip... (or New Clip... in the clip selector). A save dialog asks where to put the .anim; it opens next to the clip being edited, or in Assets/, and only accepts folders inside Assets/. The window then adds an Animator if needed and assigns the clip. A second clip on the same Animator creates a controller with one state per clip, saved next to the new clip. Create Controller... in the Animator window works the same way for .animctrl files.

Animator fields

Field Meaning
Controller The .animctrl to play. Wins over Clip.
Clip A .anim played on its own (looping per its Loop setting) when there is no controller.
Speed Playback speed multiplier. 0 freezes the pose.
Update Mode Normal follows Time.TimeScale; Unscaled Time keeps playing while the game is paused; Fixed steps in the fixed update, in sync with physics.
Apply In Edit Mode Plays the animation in the editor without pressing Play. The scene keeps its authored values: saving never stores an animated value.
Culling Mode What happens outside the game camera's view: Always Animate (default), Cull Pose or Cull Completely. See Culling and distance rate.
Distance Rate Fewer updates far from the camera: Project Setting (default), Off or Custom with the component's own Half Rate From and Quarter Rate From.

The Animator runs after the scripts each frame, so parameters a script sets in OnUpdate take effect in the same frame, and before 3D physics reads kinematic bodies.

What can be animated

  • Component properties: every property with a field in the Inspector. Numbers, booleans, vectors and colors use curves, and each vector or color channel is its own track (Position.x, Color.a). Booleans and integers step from key to key.
  • Assets, text and enums (a sprite texture, a material, a text) use discrete keys that switch the value at each key time.
  • Sprite-sheet slices: the Texture property of a Sprite Renderer or Image holds the texture together with its slice, so a Texture track switches between slices of one sheet. With Record on, pick another slice in the Inspector's Texture field (it shows Sheet > Slice); each pick becomes a key.
  • Rect Transform 2D: Anchored Position, Size, Pivot, anchors, Rotation (degrees) and Scale.
  • C# script fields: the public or [ShowInEditor] fields of scripts on the animated entities (float, int, bool, Vector2/3/4, Color). The script must be on the entity when the Animator starts.
  • Rotations use explicit Euler Multi-turn, Shortest Angle or Quaternion interpolation (see below).

Tracks address their entity by a path of child names from the Animator's entity ("Arm/Hand"; empty for the Animator's own entity). Renaming a child breaks the path: the Animator then skips that track and logs one warning, and the Animation window shows the track in the warning color.

Animation window

Tools > Animation, or double-click a .anim in the Project panel. It edits the clips of the selected Animator, or of the nearest parent that has one. Choosing a time in the window previews that pose in the Editor View and the Inspector without changing the scene.

Toolbar: Record, previous key, preview playback (Space), next key, the current time and frame, the clip selector (with New Clip...), Dopesheet / Curves, the clip's Length, Samples (the frame rate of the key grid) and Loop.

Creating keys

  • Record (the red dot): while it is on, every change to an entity below the Animator becomes a key at the current time. The change can come from the Inspector or a gizmo drag. A property that is keyed for the first time also gets a key with its old value at time 0. Record changes go into the clip; the scene keeps its values.
  • Add Property... lists the animatable properties and script fields of the Animator's entities and adds tracks with keys at the start and end.
  • Drag the value next to a track to key it at the current time.
  • Double-click a track row to add a key there; K keys the selected tracks (or all) at the current time.

Editing keys: click to select (Ctrl adds), drag to move them (they snap to the sample grid; hold Shift to move freely), drag on empty space for a box selection, Delete removes them. Right-click a key for Smooth, Flat, Linear and Constant interpolation.

Curves shows the curves of the selected tracks, or all of them when no track is selected. Drag keys in time and value. Drag a smooth key's tangent handles to shape it; hold Ctrl to break the two tangents apart. F frames everything. The mouse wheel zooms time, Shift+wheel zooms values, and the middle mouse button pans.

Navigation: Ctrl+wheel (Cmd+wheel on macOS) zooms the dopesheet, Shift+wheel scrolls in time, a click on the ruler moves the playhead, , / . step one frame, and Home / End jump to the start / end. Ctrl+Z / Ctrl+Y (Cmd+Z / Cmd+Shift+Z on macOS) undo and redo clip edits while the window has focus. Every edit is saved to the .anim file at once.

Animation events

The row above the tracks holds events. Double-click it (or right-click > Add Event) to add one, then drag it in time. Double-click or right-click an event to set its Function and Argument. During playback the event calls that public method on every script of the Animator's entity:

using Funke;

public class Footsteps : EntityScript
{
    public Audio? Step;

    // Event: Function "OnFootstep", Argument "left"
    public void OnFootstep(string foot) => Step?.PlayOneShot();
}

The method may take no parameter or one string, int, float or bool; the Argument text is converted to it. An event whose method no script has logs one warning. Events fire on every loop, and during a crossfade they fire for both clips.

Animator window

Tools > Animator, or double-click a .animctrl. It shows the controller of the selected Animator, or the one you opened.

  • States: right-click the canvas > Create State, or drop a .anim from the Project panel onto it. Select a state to edit its name, clip, speed and loop mode (From Clip, Loop, Once) on the right. The orange arrow from Entry marks the default state; change it with right-click > Set as Default State. Double-click a state to edit its clip in the Animation window.
  • Transitions: right-click a state (or Any State) > Make Transition, then click the target. Select a transition to edit it:
    • Has Exit Time / Exit Time: waits until the source state has played that far, in clip lengths (0.75 = three quarters through; with looping states it fires each time the loop passes that point).
    • Duration: crossfade length in seconds. Properties that both clips animate blend; properties only one of them animates blend from or to their current value.
    • Conditions: all must hold. Floats and ints compare with Greater / Less (ints also Equals / Not Equal); bools test True / False; triggers fire when set and are reset by the transition that used them.
    • Can Transition To Self (Any State only) lets the transition restart the state that is already playing.
    • A transition without conditions fires at its exit time, or immediately when it has none.
  • Any State transitions are checked before the current state's own, every frame, except while a crossfade is running.
  • Parameters (left): + adds Float, Int, Bool or Trigger; the value next to each is its start value. Renaming a parameter updates the conditions that use it; deleting it removes them.
  • In Play mode, select the playing entity: the window marks the active state with its progress, highlights a running transition, and the parameter column shows live values you can change.

Drag nodes to arrange them, drag the empty canvas (or use the middle mouse button) to pan, use the wheel to zoom, and press F to frame everything. Delete or Backspace removes the selection; Ctrl+Z / Ctrl+Y (Cmd+Z / Cmd+Shift+Z on macOS) undo and redo.

Scripting

using Funke;

public class Player : EntityScript
{
    private Animator? m_Animator;

    public override void OnStart() => m_Animator = GetComponent<Animator>();

    public override void OnUpdate()
    {
        m_Animator!.SetBool("IsRunning", Input.GetAxis().X != 0f);
        if (Input.GetKeyDown(KeyCode.Space)) m_Animator.SetTrigger("Jump");
    }
}
Member
SetFloat/GetFloat, SetInteger/GetInteger, SetBool/GetBool Parameters by name. A name that doesn't exist (or has another type) logs one warning.
SetTrigger(name), ResetTrigger(name) A trigger stays set until a transition that checks it fires.
HasParameter(name)
Play(state, normalizedTime = 0) Jumps to a state without blending.
CrossFade(state, duration) Blends from the current pose to a state.
CurrentStateName, NextStateName The playing state, and the target of a running crossfade (empty when none).
NormalizedTime Time in the current state in clip lengths (1.5 = halfway through the second loop).
IsInTransition, TransitionProgress The running crossfade and its 0..1 progress.
Speed, Controller, Clip The component's fields; Controller / Clip take AnimatorController / AnimationClip assets.
CullingMode, DistanceRate, HalfRateDistance, QuarterRateDistance See Culling and distance rate (AnimationCullingMode, AnimationDistanceRate).

Script fields of type AnimationClip and AnimatorController show as asset slots in the Inspector, so a script can swap controllers (e.g. per character skin): GetComponent<Animator>().Controller = Skins[i];. AssetManager.LoadByGUID<AnimationClip>(guid) and FindAll<AnimatorController>(...) work as for other assets.

Culling and distance rate

Animated characters cost time every frame, also when they are off screen or far away. Two options on the Animator, and the same two on the Skinned Mesh Renderer, let them skip that work. Both are off by default, so existing scenes animate exactly as before.

Culling Mode decides what happens while the animation is outside the game camera's view:

Mode Off screen
Always Animate (default) Animates as usual.
Cull Pose The clock, transitions and animation events keep running (at most every 4th frame), but no pose is written and no bones are computed. Back in view, the current pose is written at once, as if it had played all along.
Cull Completely Pauses. Back in view it continues where it stopped.

"In view" means that a renderer on the Animator's entity or its children (Mesh Renderer, Skinned Mesh Renderer, Sprite Renderer) is inside the view of the main camera: the first Camera 3D of the loaded scenes for 3D entities, the main Camera 2D for 2D entities. The test uses the renderers' bounds plus a margin for limbs that swing out between two checks, and it is repeated every few frames. An Animator without such a renderer below it (UI, lights) always counts as visible.

Distance Rate updates animations less often the farther they are from the camera:

Distance from the camera (world units) Updates
below Half Rate From every frame
Half Rate From (default 20) up to Quarter Rate From every 2nd frame (30 per second at 60 fps)
Quarter Rate From (default 40) and farther every 4th frame
  • The time of the skipped frames is added to the next update, so the tempo, transition times and exit times stay the same, and no animation event is lost: an event that falls between two updates fires with the next one.
  • The updates are spread over the frames, so far animations don't all update in the same frame.
  • Project Setting (the default) follows Project Settings > Graphics > Animation: the Distance Rate switch (off by default) and its two distances (20 and 40 units). Off always updates every frame. Custom uses the component's own Half Rate From and Quarter Rate From.
  • Animators in Fixed update mode ignore the distance rate: fewer, larger steps would change how the bodies they move behave in physics.

Tips:

  • Turn the project's Distance Rate on for games with many animated characters, or from a quality menu: RenderSettings.AnimationDistanceRateEnabled = true;.
  • Keep Always Animate where an animation matters off screen: a character whose shadow falls into view, a platform a physics body stands on, an Animator that moves the camera.
  • In the editor the game camera decides, also while you watch the scene in the Editor View.
  • The Profiler's Animation rows show the time of the Animators and Skinned Mesh Renderers and how many of them updated their pose in a frame.

Sprite animations played by an Animator (Texture tracks) also use its culling and distance rate.

Rotation interpolation

Right-click a rotation track in the Animation window and choose Rotation Mode. The choice applies to every channel of that property in the clip, including undo/redo and saving.

Mode Key sampling and crossfades Use
Euler Multi-turn (eulerMultiTurn) Numeric interpolation of each degree channel, including values outside ±360°. Spinning wheels, full turns, and existing clips. A 0→1080° segment completes three revolutions.
Shortest Angle (shortestAngle) Each degree channel uses its shortest signed difference modulo 360°. Independent axes: 350→10° passes through 360°; -179→179° passes through -180°.
Quaternion (quaternion) Rotation channels form one orientation; normalized, shortest-arc SLERP samples segments and blends poses. Coupled 3D rotations with mixed axes. Full-turn counts are discarded.

All rotation channels within one property/clip must select the same mode. During a crossfade, each clip samples its own mode; the destination clip chooses how the two sampled orientations blend. If only one clip animates the property, its mode blends against the property's current value. Unanimated Euler channels use their current values. Quaternion interpolation may change all three displayed Euler angles because they represent one orientation.

Quaternion mode requires all channels (Euler xyz, quaternion xyzw, or the single 2D angle) with matching key times and matching Linear or Constant interpolation. Add Key, the K shortcut, and a timeline double-click insert all quaternion channels together using the sampled orientation. Bezier keys are rejected with a binding diagnostic; their independent tangents do not define a quaternion curve. Shortest Angle supports Bezier tangents in degrees/second, so deliberately large tangents can still overshoot. Exact 180° ties follow the signed remainder; Shortest Angle values may wrap numerically at a key boundary while retaining the same orientation.

Transform2D.Rotation, RectTransform2D.Rotation and Transform3D.Rotation carry PropertyMetadata::WithRotation() (RotationProperty::EulerDegrees). Native component authors can use this metadata for Float/Double degrees or a Vec3 of Euler degrees. A Vec4 containing quaternion x,y,z,w must use WithRotation(RotationProperty::Quaternion): all four keyed channels are required, normalized and SLERPed, including when old tracks omit a mode. Zero-length or non-finite quaternions fall back to identity. Ordinary Vec4 properties retain numeric interpolation. Non-default rotation modes on properties without rotation metadata are rejected; C# script fields currently have no rotation metadata attribute.

Old .anim files omit rotationMode and keep Euler Multi-turn for Euler properties. Missing or unknown mode values load with that same default; no asset rewrite or format-version change is required. Skeletal animation and imported glTF clips keep their separate quaternion interpolation and crossfade implementation.

File formats

Both are JSON with a GUID in their .meta, like other assets, and can be written by hand or by tools. funke validate checks them: tracks need a component and a property, controller states must point at .anim files, transitions at existing states, and conditions at parameters. It also checks that an Animator's controllerAsset / clipAsset point at the right kind of file.

{
  "type": "AnimationClip", "version": 1,
  "length": 1.0, "loop": true, "sampleRate": 60,
  "tracks": [
    { "path": "", "component": "Transform3D", "property": "Position.y",
      "keys": [ { "t": 0, "v": 1, "interp": "bezier" }, { "t": 0.5, "v": 1.6, "interp": "bezier", "in": 0, "out": 0 }, { "t": 1, "v": 1 } ] },
    { "path": "Arm", "component": "SpriteRenderer", "property": "Texture",
      "discrete": [ { "t": 0, "v": "7300000000000000001|slice|arm_0" }, { "t": 0.5, "v": "7300000000000000001|slice|arm_1" } ] }
  ],
  "events": [ { "t": 0.5, "function": "OnFootstep", "argument": "left" } ]
}
  • component is the serialized component name (Transform3D, Transform2D, RectTransform2D, SpriteRenderer, MeshRenderer, Image, PointLight ...) or a C# script class name. property is the Inspector property name (case-insensitive), with .x/.y/.z/.w or .r/.g/.b/.a for one channel.
  • interp is linear (default), step or bezier; in / out are the bezier tangents (value per second) and default to 0 (flat).
  • A numeric rotation track may add "rotationMode": "shortestAngle" or "rotationMode": "quaternion". The default "eulerMultiTurn" is omitted when saving.
  • Discrete values are written as in the Inspector's text form: asset GUIDs as decimal strings, a sprite-sheet slice as "<texture GUID>|slice|<slice name>" (a bare GUID shows the whole texture), enum values as numbers, vectors as "x,y". The value field next to a discrete track in the Animation window shows and edits the same text. Sprite Renderer and Image have no separate SpriteName property; the slice is part of Texture.
{
  "type": "AnimatorController", "version": 1,
  "parameters": [ { "name": "IsRunning", "type": "bool", "default": false }, { "name": "Jump", "type": "trigger" } ],
  "states": [ { "name": "Idle", "clip": "7200000000000000001", "speed": 1, "position": [0, 0] },
              { "name": "Run", "clip": "7200000000000000002", "speed": 1, "loop": "loop", "position": [260, 0] },
              { "name": "Jump", "clip": "7200000000000000003", "speed": 1, "loop": "once", "position": [260, 160] } ],
  "defaultState": "Idle",
  "transitions": [
    { "from": "Idle", "to": "Run", "conditions": [ { "parameter": "IsRunning", "mode": "if" } ], "hasExitTime": false, "duration": 0.2 },
    { "from": "Run", "to": "Idle", "conditions": [ { "parameter": "IsRunning", "mode": "ifNot" } ], "hasExitTime": false, "duration": 0.2 },
    { "fromAny": true, "to": "Jump", "conditions": [ { "parameter": "Jump", "mode": "if" } ], "duration": 0.1 },
    { "from": "Jump", "to": "Idle", "hasExitTime": true, "exitTime": 1, "duration": 0.1 }
  ]
}
  • Parameter type: float, int, bool, trigger. Condition mode: greater, less, equals, notEquals (with threshold), if, ifNot.
  • State loop: clip (default, use the clip's Loop), loop, once.
  • Transitions reference states by name; "fromAny": true makes an Any State transition.

In a scene the component is written as "Animator": { "controllerAsset": "<guid>", "clipAsset": "<guid>", "speed": 1, "updateMode": 0, "applyInEditMode": false }. cullingMode (0 Always Animate, 1 Cull Pose, 2 Cull Completely), distanceRate (0 Project Setting, 1 Off, 2 Custom), halfRateDistance and quarterRateDistance are only written when they differ from the defaults. Game builds ship the clips and controllers that scenes, controllers and scripts reference.

Limits

  • One layer per controller; there are no blend trees, avatar masks or root motion yet.
  • Fields of C# IComponent structs can't be animated (fields of EntityScripts and managed Components can).
  • Euler Multi-turn deliberately preserves the numeric rotation path. Choose Shortest Angle or Quaternion explicitly for shortest-arc transitions; quaternion tracks require aligned Linear/Constant keys.