Animation only plays frame 0 constantly but not other frames

Godot Version

4.2.2

Question

so I’m making an attack and trying to get the animations working first, the animation is good in the player but when I play and press the input to play the animation it starts but only stays on frame 0, when I let go of the key it stops playing the animation (as intended) and all my other animations work fine.

here is the entirety of the script for my player node (the problem is at the bottom)

extends CharacterBody2D
#gravity and movement speed and jump height
@export var speed = 300
@export var gravity = 18
@export var jump_force = 260

#jump count and max jumps for doublejump
@export var jump_count = 0
@export var max_jumps = 2


@onready var animated_sprite = $AnimatedSprite2D


func _physics_process(delta):
	#applying gravity and limiting gravity
	if !is_on_floor():
		velocity.y += gravity
		if velocity.y > 500:
			velocity.y = 500
	#resetting jump count when landing
	if is_on_floor():
		jump_count = 0
	
	#jump action
	if Input.is_action_just_pressed("jump") and jump_count < max_jumps:
		velocity.y = -jump_force
		jump_count += 1
	
	
	# Get the input direction: -1, 0, 1 and move left and right
	var horizontal_direction = Input.get_axis("move_left", "move_right")
	velocity.x = 100 * horizontal_direction
	#flip sprite depending on direction
	if horizontal_direction > 0:
		animated_sprite.flip_h = false
	elif horizontal_direction < 0:
		animated_sprite.flip_h = true
	
	
	#play run and idle animation
	if is_on_floor():
		if horizontal_direction == 0:
			animated_sprite.play("idle")
		else:
			animated_sprite.play("run")
	
	#play jump and fall animation
	if velocity.y > 0:
		animated_sprite.play("fall")
	elif velocity.y < 0:
		animated_sprite.play("jump")
		
		#this is the problem
	if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and velocity.y == 0:
		animated_sprite.play("attack")
		velocity.x = 0
	else:
		pass
	
	
	
	
	move_and_slide()
	
	print(velocity)

I bet the attack animation is being overriden by the other animations playing, try adding another condition like so

var is_attacking: bool = animated_sprite.animation == "attack"

if is_on_floor() and not is_attacking:
	if horizontal_direction == 0:
		animated_sprite.play("idle")
	else:
		animated_sprite.play("run")

new problem, so when I added that code now only the attack animation plays till I jump, so I fixed that with this code

if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and velocity.y == 0:
		animated_sprite.play("attack")
		velocity.x = 0
        #this is what I added
	elif Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) == false and velocity.x == 0:
		animated_sprite.play("idle")
	elif Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) == false and velocity.x != 0:
		animated_sprite.play("run")

now itll stop playing the attack animation when I let go of the button and allow the run and idle animations to play, however the jump animation now no longer plays when I jump neither the fall animation when falling

I can’t tell without proper code formatting, try pasting again between three ticks like so

```
paste here
```

You can use the code block </> button to help you

Also I do not see the jump code pasted here, was it changed?

i edited my previous msg to have the right format

and no the jump code is the same as before hand which is in my initial post

1 Like

I believe you still need to keep the is_on_floor() check.

This if/elif chain will always execute at least one condition, thus overridding a animation.

where do I put the floor check?

probably about where it was at the top of movement animations.

if is_on_floor():
	if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
		animated_sprite.play("attack")
		velocity.x = 0
	else: # shorter elifs
		if velocity.x == 0:
			animated_sprite.play("idle")
		else:
			animated_sprite.play("run")

thank you ill try this as soon as I can

this worked amazingly thank you, I’ve got some other things I need to tweak out but ill try to do them on my own again thank you for all the help

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.