Animator
Plays the states of an animation controller and blends between them when its parameters change. The graph, the blends, root motion and gameplay events run on fixed steps, so replays match at any frame rate.
component animation.Animator
Art Studio
Try it
Advance an Animator state
A script sets the controller's Speed parameter to select Run. The separate Animator Speed field starts at 0, holding the state time.
Change Animator Speed from 0 to 8. The real Run clip advances and the runner turns orange after its state time moves. Reset holds it again.
Scene3 nodes
Scriptmain.aes
package demo
// The graph's Speed parameter selects Run independently of the Animator's
// playback Speed field. The color marks actual advancement in that state.
script RunIndicator {
var advanced bool
func FixedUpdate(dt float) {
a := node.Get<Animator>()
a.SetFloat("Speed", 3)
if a.State == "Run" && a.Time > 0.05 {
advanced = true
}
if advanced {
node.Get<SpriteRenderer>().Tint = Color{1, 0.2, 0.1, 1}
}
}
}
Fields
| Name | Type | Default | Description |
|---|---|---|---|
| Controller | AnimController | none | Controller with the states, parameters and transitions. |
| Play On Start | bool | true | Enters the entry state when Play starts. |
| Speed | float | 1 | Multiplies the speed of every state; 1 is normal speed. |
| Time Domain | enum | game | game follows game time and pauses with it; unscaled runs in real time while paused, for presentation only. one of: game, unscaled |
| Root Motion | enum | none | What the root track moves: none ignores it, toMotion moves the node through Motion 2D so walls stop it, toTransform sets its position. one of: none, toMotion, toTransform |
| Root Fallback | bool | false | toMotion moves the transform instead when the node has no Motion 2D. |
| Root Scale | Vec2 | (1, 1) | Multiplies root motion in the node's parent space; set X to -1 while the character faces left. |
Runtime state
Values the component has while the game runs. They are not saved with the scene.
| Name | Type | Default | Description |
|---|---|---|---|
| State | string | "" | Current state. |
| Time | float | 0 | Seconds in the current state. |
| Progress | float | 0 | Time in the current state divided by the length of its clip; 1 is one full clip. |
| Blending | bool | false | Whether a transition is blending. |
Methods
CrossFade method
func (Animator) CrossFade(state string, seconds float)
Blends into a state over some seconds, starting on the next fixed step.
GetBool method
func (Animator) GetBool(name string) bool
Reads a bool or trigger parameter.
GetFloat method
func (Animator) GetFloat(name string) float
Reads a float parameter.
GetInt method
func (Animator) GetInt(name string) int
Reads an int parameter.
IsState method
func (Animator) IsState(state string) bool
Reports whether the animator is in a state; during a blend that is the state it blends into.
Play method
func (Animator) Play(state string)
Switches to a state on the next fixed step, without a blend.
ResetTrigger method
func (Animator) ResetTrigger(name string)
Clears a trigger that has not fired yet.
SetBool method
func (Animator) SetBool(name string, value bool)
Sets a bool parameter; transitions read it on the next fixed step.
SetFloat method
func (Animator) SetFloat(name string, value float)
Sets a float parameter.
Example
anim.SetFloat("Speed", math.Abs(motion.Velocity.X))
SetInt method
func (Animator) SetInt(name string, value int)
Sets an int parameter.
SetTrigger method
func (Animator) SetTrigger(name string)
Sets a trigger for the next fixed step; a transition that uses it, or the end of that step, clears it.
StateName method
func (Animator) StateName(index int) string
Returns the name of a state index, as StateEntered and StateExited report it.
Signals
Event signal
signal Event(id int, payload float)
Runs when a state reaches an event of its clip; during a blend the state's event policy decides which side reports.
StateEntered signal
signal StateEntered(state int)
Runs when the animator enters a state, at the start of the transition.
StateExited signal
signal StateExited(state int)
Runs when a state stops playing, at the end of the transition.
Script events
Scripts on a node with this component receive these events from the Animation module.
OnAnimEvent— Runs on the scripts of a node whose Animation Player reaches an event of its clip; anim.EventName(id) gives the name.