Walk animations not playing

Godot Version 4.6

Question

So none of my walk animations are playing

func _physics_process(_delta: float) -> void:
	if Input.is_action_pressed("left") or Input.is_action_pressed("right") or Input.is_action_pressed("up") or Input.is_action_pressed("down"):
		walk()

func walk():
	current_state = States.WALK
	character_direction.x = Input.get_axis("left", "right")
	character_direction.y = Input.get_axis("up", "down")
	if character_direction:
		velocity = character_direction * movement_speed
		if character_direction.x < 0:
			%sprite.flip_h = true
			%sprite.animation = "walk_LR"
		elif character_direction.y < 0: 
			%sprite.flip_h = true
			%sprite.animation = "walk_UD"
		elif character_direction.x > 0: 
			%sprite.flip_h = false
			%sprite.animation = "walk_LR"
		elif character_direction.y > 0:
			%sprite.flip_h = false
			%sprite.animation = "walk_UD"

I know that the way I have the walk code is definitely not the most efficent, but it works, besides this issue…

Also, is there any way I can easily condense my code for animation/flipping the sprite? The way I have it now feels really repetitive…

Edit: I know that the issue probably has to do with the walk function being called in the physics_process function, but I don’t know what I should change to fix that…

Are your animations baked into a spritesheet?
Have you at least managed to get them playing separately?

Outside of the game playing, just in the player scene, the animations play.

Have you tried debug with print()?
Specially inside your walk function .

The first frame of animation always shows up when given the input, but the rest of the animation doesn’t play, so I know the code is at least running. Sorry, I should’ve specified a bit more in my original post…

Well, using print, it printed out the phrase as long as I held the button. Like I said in the edit, it just confirmed that the problem has to do with the walk function being called within the physics process function. I just don’t know where else I’d call it.

Usually it’s issue of condition not be triggered or function not called .

Try use print() to figure out what and why is not working.

The walk function is being called and so it the animation. The animation is just constantly restarting because it keeps being reset because the walk function is called by the physics process function. I just don’t know where else I should call it to fix this.

func _process()

Maybe ?

Just out of curiosity why don’t You use

func _input() it be cleaner


func get_input():
direction_x = Input.get_axis("left", "right")

Since you have this walk state, I assume you also have an Idle state right? You should only play the animation at the start of the walk state.

This code right here will start the walk animation over and over.

You could do something like this to fix it.

func physics_process(_delta) -> void:
  match current_state:
    States.IDLE:
      If Input.is_action_pressed("left") or Input.is_action_pressed("right") or Input.is_action_pressed("up") or Input.is_action_pressed("down"):
      walk()
      current_state = States.WALK
  
    States.WALK:
      ##Your walk code
      pass

This way, the animation in the walk() function only plays once when transitioning to the walk state.

With that said, you should also read about using _input(event) instead of polling Input every frame in the physics process.

Changing the animation property won’t automatically start playing the new animation if the AnimatedSprite is paused. Either use the play() method instead of changing the animation property, or unpause it once at the beginning (e. g. by selecting “Autoplay on Load” for one of the animations).

This still isn’t working…

func _physics_process(_delta: float) -> void:
	match current_state:
		States.IDLE:
			%sprite.animation = "idle"
			if Input.is_action_just_pressed("up") or Input.is_action_just_pressed("down") or Input.is_action_just_pressed("left") or Input.is_action_just_pressed("right"):
				current_state = States.WALK
		States.WALK:
			walk()

I might have just not done it right though?

Might help to share your scene setup.

Like this?

Change this (and the similar lines) to %sprite.play(“walk_LR”) (replacing the “_LR” with “_UD”, etc.). Check to make sure you aren’t playing the animation you’re about to play already before playing it:

%sprite.flip_h = ?
if %sprite.animation != “walk_?”:
    %sprite.play(“walk_?”)

Hope this helps!