How do I fix my attack animation from only playing the first frame

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Pile_of_Ash

After the attack animation the character changes to another animation.

extends KinematicBody2D

var speed = 300
var max_speed = 75
var gravity = 450
var friction = 0.5
var jump = 200
var velocity = Vector2.ZERO
var resistance = 0.7
var jumpNumber = 1
var melee = false

var wallJump = 250
var jumpWall = 5

onready var sprite = $Sprite
onready var anim = $AnimationPlayer

func _ready():
pass # Replace with function body.

func _physics_process(delta):
var movement_x = Input.get_action_strength(“ui_right”) - Input.get_action_strength(“ui_left”)
if movement_x != 0:“Run”)
velocity.x += movement_x * speed * delta
velocity.x = clamp(velocity.x, -max_speed, max_speed)
sprite.flip_h = movement_x < 0
melee = false;
velocity.x = lerp(velocity.x, 0, friction)“Idle”)
melee = false

if Input.is_action_pressed("AttackHit"):"AttackSword")
	melee = true

if is_on_floor() or nextToWall():
	jumpNumber = 1
	if Input.is_action_just_pressed("ui_accept") and jumpNumber > 0:
		velocity.y -= jump
		jumpNumber -= 1"Jump")
		if $AnimationPlayer.current_animation == "Jump" and $AnimationPlayer.is_playing():
		if not is_on_floor() and nextToRightWall():
			velocity.x -= wallJump
			velocity.y -= jumpWall
		if not is_on_floor() and nextToLeftWall():
			velocity.x += wallJump
			velocity.y -= jumpWall
	if nextToWall() and velocity.y > 30:
		velocity.y = 30
		if nextToRightWall():
			sprite.flip_h = true"WallSliding")
		if nextToLeftWall():
			sprite.flip_h = false"WallSliding")
	if movement_x == 0:"Fall")
		velocity.x = lerp(velocity.x, 0, resistance)
		melee = false

velocity.y += gravity * delta
velocity = move_and_slide(velocity, Vector2.UP)

func nextToWall():
return nextToRightWall() or nextToLeftWall()

func nextToRightWall():
return $RightWall.is_colliding()

func nextToLeftWall():
return $LeftWall.is_colliding()

:bust_in_silhouette: Reply From: Bernard Cloutier

Go through your code again and ask yourself: what happens in the frame following an attack press?

What happens is you then check the movement and either switch to the move or idle animation, thus preventing the attack from playing more han one frame.

Your character controller is getting pretty complex, you should look into state machines as it would help managing youe character states. GDQuest has a tutorial on those, but state machines are extremely common in games so you can find many other good resources on those online.