I believe the way to do this is to just put the code to start the animation at the same place you tell the program to do something. For example, say you have:
if Input.is_action_pressed("move_left"):
velocity.x = -1 * speed
elif Input.is_action_pressed("move_right"):
velocity.x = 1 * speed
elif Input.is_action_pressed("move_up"):
velocity.x = -1 * speed
elif Input.is_action_pressed("move_down"):
velocity.x = 1 * speed
Just add the code to animate it with the code to move it in the first place. So in this case, it'd be...
if Input.is_action_pressed("move_left"):
velocity.x = -1 * speed
$AnimatedSprite.play("walk_left")
elif Input.is_action_pressed("move_right"):
velocity.x = 1 * speed
$AnimatedSprite.play("walk_right")
elif Input.is_action_pressed("move_up"):
velocity.x = -1 * speed
$AnimatedSprite.play("walk_up")
elif Input.is_action_pressed("move_down"):
velocity.x = 1 * speed
$AnimatedSprite.play("walk_down")
else:
$AnimatedSprite.play("idle")
Obviously the code won't be exactly the same as what I put here, and you'll need to set some other stuff up (like the input map so the computer knows what "move_up"
, "move_left"
, etc. means, and the AnimatedSprite animations (which I'm assuming you've already done), or the velocity vector and use it for move_and_slide()
or whatever you're using to move) so if you need help with that just reply to this post or hit me up on Discord: Shawk#8857
Also, about the vectors, in Godot a vector is basically a variable but with two or three values (depending on if it's 2D or 3D) that is usually used for physical positions. Again, this can be a bit confusing so reply to this post or send me a Discord message if you need help.