When a key is pressed the attack animation only plays one frame

Godot Version

Godot 4.3

Question

Hi, I’m new in Godot and I don’t know too much. So recently I have a problem with the attack animation, when I press the U (the button fot the attack) the attack animation only reproduce 1 frame. How can I solve it?

Code:

extends CharacterBody2D

#Variables
const speed:int = 300
const jump_velocity:int = -500
var x:int = 1
var jumpCount:int = 0
var maxJump:int = 2


@onready var Jugador2: Node2D = $Node2D
@onready var Hitbox: Area2D = $Hitboxes

func _ready():
	pass

#Movimiento
func _physics_process(delta: float) -> void:
	
	if not is_on_floor(): #Gravedad
		velocity += get_gravity() * delta
		
	if is_on_floor():
		jumpCount = 0

	if Input.is_action_just_pressed("jump2") and jumpCount < maxJump:
		velocity.y = jump_velocity #Saltar
		jumpCount += 1

	var direction := Input.get_axis("move_left2", "move_right2")
	
	if direction:
		velocity.x = direction * speed #Movimiento
		$AnimationPlayer.play("ParadoJ2")
		
		while x == 1:
			$SonidoDeCaminar.playing = true
			x = 0
	else:
		velocity.x = move_toward(velocity.x, 0, speed)
		$AnimationPlayer.play("RESET")
		$SonidoDeCaminar.playing = false
		x = 1
	
	if Input.is_action_just_pressed("weak_attack2"):
		$AnimationPlayer.play("Ataque debil")
		
	move_and_slide()
	
	if direction != 0:
		Jugador2.scale.x = direction
		Hitbox.scale.x = direction

You resetting your animation with this line:

$AnimationPlayer.play("RESET")

You can add additional check if no other action is currently active.
I also recommend to cache AnimationPlayer reference like this:

@onready var animPlayer : AnimationPlayer = $AnimationPlayer

And call it like this:

animPlayer.play("animation_name")

You should redesign your code using GPT.

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