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