The problem is that it gets overwritten by you idle or run animation. you should add a variable to keep track of your animation:
var attacking: bool = false
func _physics_process(delta):
var direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
velocity = direction * 600
move_and_slide()
if velocity.length() > 0.0 and not attacking:
%Bluwee.play_run()
elif not attacking:
%Bluwee.play_idle()
if Input.is_action_just_pressed("attack") and not attacking:
attacking = true
%Bluwee.play_attack()
and then you need to connect the animation_finished signal of the animation_player to the script to detect when the attack animation is finished:
func _on_animation_finished(animation: String):
if animation == "side_attack":
attacking = false
extends CharacterBody2D
var attacking: bool = false
func _physics_process(delta):
var direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
velocity = direction * 600
move_and_slide()
if velocity.length() > 0.0 and not attacking:
%Bluwee.play_run()
elif not attacking:
%Bluwee.play_idle()
if Input.is_action_just_pressed("attack") and not attacking:
attacking = true
%Bluwee.play_attack()
func _on_animation_finished(animation: String):
if animation == "side_attack":
attacking = false
if Input.is_action_pressed("attack"):
%Bluwee.play_attack()
elif velocity.length() > 0.0:
%Bluwee.play_run()
else:
%Bluwee.play_idle()
My Code Explaination:
i this way the whole if else structure become ones first it check for Input for attack button if it happen it end the if else structure after running the attack.
if attack button was not pressed it check for velocity length. if this is true then it run the player until the 2nd condition is false if nothing is doing it becomes idle.
Issue in your code
the problem in your code is you are using two if else structure one for running and one for attack you code stuck after one attack because you are check for run or idle and attack at same time. so after every frame event if you press the attack button your attack animation is over-ridden by ilde or run animation.
The problem with your code is that when he stops pressing attack but is still inside attack animation he stops the animation and continues with walk or idle
he stops the animation and continues with walk or idle what does this mean I did not get it. I guess this is what you want. or if you share the screenshot may be it explain well.
this method gets called every frame, so as soon as the attack button is released the animation will change because this attack-condition is no longer met, eventhough the attack-animation might still be playing.
Thats why you need to wait for the attackanimation to finish before you change the animation