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!

I’m probably gonna try and restart the walk script and see what I did wrong because with the weird way I structured it, these solutions aren’t working… If it happens again with newly structured code, then I’ll probably come back and try these solutions again…

Well, this screenshot is not very helpful, since the nodes inside the player are hidden and I believe the animation issues are in that scene. Next time try to give as much useful information to let people understand the problem, so then they can try to solve it.

Anyways into your problem: I’m assuming you’re using an AnimatedSprite that constantly is executing an animation in loop, and in side your WALK state you’re changing the animation. With this implementation, you don’t need to call play/stop.
In my opinion, it’s a not-so-elegant code, but it can work, and that’s what matters at the end of the day.

What I think it’s the issue (most probably) is that in every physics_process iteration the animation is getting reassigned, so the visuals are restarting continously.
As an example: you press LEFT, then the animation is set to “walk_LR”. You expect the AnimatedSprite to advance to frame 2, but the physics_process calls again and executes the line ‘%sprite.animation = “walk_LR”’ once more, reseting the animation to frame 1 another time.

A way to fix this issue (if it’s because of this and not another thing) would be to change/assign the animation only if the current one is different from the one you want to put.

Something like:

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
			if %sprite.animation != "walk_LR"
				%sprite.animation = "walk_LR"
		elif character_direction.y < 0: 
			%sprite.flip_h = true
			if %sprite.animation != "walk_UD"
				%sprite.animation = "walk_UD"
		elif character_direction.x > 0: 
			%sprite.flip_h = false
			if %sprite.animation != "walk_LR"
				%sprite.animation = "walk_LR"
		elif character_direction.y > 0:
			%sprite.flip_h = false
			if %sprite.animation != "walk_UD"
				%sprite.animation = "walk_UD"

With this little patch and no re-coding, this might work.

Also, as a recommendation, I would recommend you assigning data to variables instead of calling them directly with %. I don’t really know if there’s a real performance issue in calling %Nodes so many times, but I would guess that it might be a little bit better and way cleaner to maintain and understand doing “var sprite: Sprite = %sprite”.