Attacking breaks movement (IsAttacking state)

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

I am making an 8 directional movement project.

So far I have been able to store each direction as separate animations in an animatedsprite2d node.

Movement is working with a walk and a run animation depending on the distance the mouse is from the player character, but I am having issues with my “isAttacking” state.

In the physics process I check if isAttacking is false before I run any other animation. When the attack input is pressed it sets isAttacking to true. I used a signal from the AnimatedSprite2d that when the animation is finished if the animation is one of the attack animations then set isAttacking to false.

The end result is the attack animation plays but after that the character is stuck in one frame of animation for walking and running. If I attack one more time then the character gets stuck in place and doesnt animate.

The intended behavior is that if I attack the character will stay facing the same direction and finish their attack animation before being allowed to move again.

extends CharacterBody2D

var current_animation = "idle"
var speed = 200
var runspeed = 400
var ang = 0
var click_position = Vector2(0, 0)
var isAttacking = false

#const SPEED = 300.0
const JUMP_VELOCITY = -400.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
#var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

func _ready():
	click_position = Vector2(position.x, position.y)
	$AnimatedSprite2D.play()
	current_animation = "idle"
	

func _physics_process(delta):
	#mouse input
	var mouse = get_local_mouse_position()
	if isAttacking == false:
		ang = snapped(mouse.angle(), PI/4) / (PI/4)
		ang = wrapi(int(ang), 0, 8)
		
	if isAttacking == false:
		current_animation = "idle"
	
	if Input.is_action_pressed("left_mouse") and mouse.length() > 10 and mouse.length() < 200 and isAttacking == false:
		current_animation = "walk"
		velocity = mouse.normalized() * speed
		move_and_slide()
		
	elif Input.is_action_pressed("left_mouse") and mouse.length() > 200 and isAttacking == false:
		current_animation = "run"
		velocity = mouse.normalized() * runspeed
		move_and_slide()
		
	elif Input.is_action_just_pressed("right_mouse") and isAttacking == false:
		current_animation = "attack"
		isAttacking = true
	$AnimatedSprite2D.animation = current_animation + str(ang)
	

func _on_animated_sprite_2d_animation_finished():
	if $AnimatedSprite2D.animation == "attack" + str(ang):
		isAttacking = false
		print(isAttacking)