Animation Priority

Godot Version

Godot 3.5.3

Question

I have a slight problem about animation, it’s quite simple really, i have two animation, one is walking, and one is attacking, now the problem is whenever i want to attack but still pressing the arrow button, the attack animation only showing the first frame until i release the arrow button, what i want is when i press the attack button, i want to game to ignore all key pressed until the animation is finished. This is my code

var velocity = Vector2.ZERO
	attacking = false
	if Input.is_action_pressed("ui_left") and attacking == false:
		velocity.x -= 1
		$Sprite.flip_h = true
		$AnimationPlayer.play("Walk_Right")
		facing = "Left"
	if Input.is_action_pressed("ui_right") and attacking == false:
		velocity.x += 1
		$Sprite.flip_h = false
		$AnimationPlayer.play("Walk_Right")
		facing = "Right"
	
	if Input.is_action_just_released("ui_left") or Input.is_action_just_released("ui_right"):
		$AnimationPlayer.stop(true)

		
	if Input.is_action_pressed("ui_accept"):
		attacking = true
		$AnimationPlayer.current_animation = "Attack"
		velocity = Vector2.ZERO

thank you

	var velocity = Vector2.ZERO
	# Set attacking here doesn't make sense, after that you check if 
	# attacking == false that always will be false because you're
	# setting to be false here
	#attacking = false
	if Input.is_action_pressed("ui_left") and attacking == false:
		velocity.x -= 1
		$Sprite.flip_h = true
		$AnimationPlayer.play("Walk_Right")
		facing = "Left"
	if Input.is_action_pressed("ui_right") and attacking == false:
		velocity.x += 1
		$Sprite.flip_h = false
		$AnimationPlayer.play("Walk_Right")
		facing = "Right"
	
	if Input.is_action_just_released("ui_left") or Input.is_action_just_released("ui_right"):
		$AnimationPlayer.stop(true)

		
	if Input.is_action_pressed("ui_accept"):
		attacking = true
		$AnimationPlayer.play("Attack")
		velocity = Vector2.ZERO
		# yield is a function that receive two arguments, an object 
		# and a signal to wait, until the object emit this signal, 
		# the code will stop. Use that to wait the attack animation finish
		yield($AnimationPlayer, "animation_finished")
		# There is the correct point to set attacking to false again
		attacking = false
1 Like

it worked, thanks