Why won't my walking animation play?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By fartholomewTVG

Hello!

I’ve been slowing teaching myself how to use Godot and have run into an issue animating my sprite

func _process (delta):
     if Input.is_action_pressed("move_left"):
	      $AnimatedSprite.play("Move")
          $AnimatedSprite.flip_h = true
	 if Input.is_action_pressed("move_right"):
	      $AnimatedSprite.play("Move")
	      $AnimatedSprite.flip_h = false
     else:
		  $AnimatedSprite.play("Idle")

This is the code that I’m using to animate my character, without the “else”, it’ll act as intending, flipping the sprite and playing the walking animation, but the animation will just continuously play.

With the “else” statement at the end, only the right facing walking animation will play.

If I use the “else” statement on each “if Input.AnimatedSprite.Play()” line, the walking animation won’t play at all, just the Idle when the character isn’t moving

Any help would be appreciated, thank you!

:bust_in_silhouette: Reply From: jgodfrey

Yeah, the logic you have there is problematic and likely not what you intend…

You’re probably looking for this:

func _process (delta):
     if Input.is_action_pressed("move_left"):
          $AnimatedSprite.play("Move")
          $AnimatedSprite.flip_h = true
     elif Input.is_action_pressed("move_right"):
          $AnimatedSprite.play("Move")
          $AnimatedSprite.flip_h = false
     else:
          $AnimatedSprite.play("Idle")

That is basically "If left pressed, do the left animation. If right is pressed, do the right animation. If neither left or right are pressed, do the idle animation.

I see, that did the trick, thank you for the help!

fartholomewTVG | 2020-10-25 18:21