Godot Version
4.2.1.stableQuestion
I need help adding an attack animation/state machine this code. Anytime before ive tried it only plays the first frame of the animation. Here’s the code. `extends CharacterBody2D
@export var speed: int = 35
@onready var animations = $AnimationPlayer
@onready var animationTree : AnimationTree = $AnimationTree
enum State {
MOVE,
ROLL,
ATTACK
}
var state: State = State.MOVE
func _physics_process(delta):
match state:
State.MOVE:
move_state(delta)
move_and_slide()
State.ROLL:
# Handle roll state logic (if needed)
pass
State.ATTACK:
# Handle attack state logic (if needed)
pass
func move_state(delta):
var moveDirection = Input.get_vector(“ui_left”, “ui_right”, “ui_up”, “ui_down”)
velocity = moveDirection * speed
if velocity.length() == 0:
animations.stop()
else:
var direction = getMovementDirection(moveDirection)
animations.play("walk" + direction)
func getMovementDirection(moveDirection: Vector2) → String:
var direction = “Down”
if moveDirection.x != 0 or moveDirection.y != 0:
if abs(moveDirection.x) > abs(moveDirection.y):
if moveDirection.x < 0:
direction = “Left”
else:
direction = “Right”
else:
if moveDirection.y < 0:
direction = “Up”
else:
direction = “Down”
return direction
`