Aego Русский

Animator

An Animator plays the states of a controller, an .aectrl file: each state has a clip, and transitions switch between states when parameters change. Scripts set the parameters; the graph does the rest.

In Try it, use A/D to walk, Space to jump and J to trigger Hit. The script sets controller parameters and shows the current state.

func FixedUpdate(dt float) {
  m := node.Get<Motion2D>()
  a := node.Get<Animator>()
  a.SetFloat("Speed", math.Abs(m.Velocity.X))
  a.SetBool("Grounded", m.Grounded)
  if input.Pressed("Attack") {
    a.SetTrigger("Attack")
  }
}
Method
SetBool, SetInt, SetFloat(name, value) set a parameter; transitions read it on the next fixed step
SetTrigger(name), ResetTrigger(name) a one-shot parameter: the transition that uses it, or the end of that step, clears it
GetBool, GetInt, GetFloat(name) read a parameter
Play(state) switch to a state on the next fixed step, without a blend
CrossFade(state, seconds) blend into a state
IsState(name) bool, StateName(index) string the state, or the name of a state index
Signal
StateEntered(state int) a transition into the state started
StateExited(state int) the state stopped playing, at the end of the blend
Event(id int, payload float) a state reached an event of its clip

State, Time, Progress and Blending show the state during Play. Create → Gameplay → Platformer Character comes with an Animator on the built-in controller engine/anim/platformer: Idle, Run, Jump and Hit, with the parameters Speed, Grounded and Attack.

Set parameters in FixedUpdate when they come from the game state, as above: the graph runs on fixed steps, so it then sees the same values on every replay at any frame rate.

On this page

Animator and Motion 2D together

The platformer example drives its player from one FixedUpdate: the controller moves, the animator shows it. Both read the same step, so a replay at any frame rate gives the same states and the same hits.

func FixedUpdate(dt float) {
  m := node.Get<Motion2D>()
  m.Move(input.Axis("Move"))
  if input.Pressed("Jump") {
    m.Jump()
  }
  a := node.Get<Animator>()
  a.SetFloat("Speed", math.Abs(m.Velocity.X))
  a.SetBool("Grounded", m.Grounded)
  a.SetFloat("VerticalSpeed", m.Velocity.Y)
  if input.Pressed("Attack") {
    a.SetTrigger("Attack")
  }
}

The values describe the previous step: scripts run before Motion 2D moves. Damage reaches the animator through a signal, Health.Damaged → SetTrigger("Hurt"), and an any transition with interrupt=true shows it even in the middle of a blend. The sprite's Flip X stays with the script: it follows the input, not a clip.

In examples/topdown the work is split the other way: a State Machine decides what an enemy does (Patrol, Chase, Dead) and its hooks only set Animator parameters (Chasing, Die); the clips decide how it looks.

Try it

Change the example parameters and compare the result. Reset restores the initial scene.

Starts the scene; open the example parameters to experiment.
Open in AegoDownload example project
Scene3 nodes
Floor
Transform
position[0, 1.1]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")tintColor#4b5563fforiginVec2[0.5, 0.5]sizeVec2[12, 0.2]
Runner
Transform
position[0, 0.5]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000b00")tintColor#a5b4fcfforiginVec2[0.5, 0.5]sizeVec2[1, 1]filterenumnearest
Animator
controllerAnimControllerasset("ae900000000000000000000000000b20")playOnStartbooltruespeedfloat1timeDomainenumgamerootMotionenumnonerootScaleVec2[1, 1]
Camera
Transform
position[0, 0]rotation0scale[1, 1]
Camera 2D
zoomfloat64viewportRect[0, 0, 1, 1]maskint-1clearbooltrueclearColorColor#111827ffscaleenumfitVirtualvirtualVec2[960, 540]activebooltrue
Scriptmain.aes
package demo

import (
	"aego/debug"
	"aego/input"
)

// Runner walks with A/D on the built-in platformer controller; Space jumps (Grounded is false in the air), J triggers Hit.
script Runner {
	var vy float
	var floor float
	var air bool

	func Update(dt float) {
		a := node.Get<Animator>()
		dir := 0.0
		if input.Down(input.KeyA) || input.Down(input.KeyLeft) {
			dir -= 1
		}
		if input.Down(input.KeyD) || input.Down(input.KeyRight) {
			dir += 1
		}
		speed := dir
		if speed < 0 {
			speed = -speed
			node.Get<SpriteRenderer>().FlipX = true
		} else if speed > 0 {
			node.Get<SpriteRenderer>().FlipX = false
		}
		a.SetFloat("Speed", speed * 3)
		p := node.Position()
		if !air {
			floor = p.Y
			if input.KeyPressed(input.KeySpace) {
				air = true
				vy = -7
				a.SetBool("Grounded", false)
			}
		}
		if air {
			vy += 20 * dt
			p.Y += vy * dt
			if p.Y >= floor {
				p.Y = floor
				air = false
				a.SetBool("Grounded", true)
			}
		}
		if input.KeyPressed(input.KeyJ) {
			a.SetTrigger("Attack")
		}
		node.SetPosition(Vec2{p.X + dir * 3 * dt, p.Y})
		label := a.State
		if a.Blending {
			label = label + " (blending)"
		}
		debug.Text(Vec2{p.X - 2, p.Y - 1.4}, label + "   " + string(a.Progress), Color{0.85, 0.85, 0.85, 1})
		debug.Text(Vec2{-4.5, 2}, "A/D walk   Space jump   J hit", Color{0.6, 0.6, 0.6, 1})
	}
}