How to animate my attack without having to hold the press button and to be able to walk while attacking

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

Good evening,
I’m Roy, and a friend recommended Godot to me a week ago and since then I’m hooked on it.

I’ve been working on my 2d project a lot and I’ve got an attack animation which doesn’t seem to want to make my life easy.

I want to be able to press the CTRL button and let my AnimatedSprite do the whole Attack animation , even if I’m walking (by Input.is_action_pressed(“ui_right \ left”) ) and trying to trigger the walk animation.

How can I do that?
Or does anyone have a demo project I can learn from?

Thank you very much,
Roy.

:bust_in_silhouette: Reply From: coffeeDragon

Add a condition before playing the walk animation, where you check wether or not the attack animation is playing and if it is, just don’t play the walk animation, until the attack animation is finished.
Or use 2 diffrent animatedSprites for the movement and attack and let function together or alone, like Mario moving his legs for running and throwing a fireball with his hand, allà Super Mario World.

:bust_in_silhouette: Reply From: Diet Estus

Use a condition to block changing to the walk animation when you press left or right when the attack animation is playing. For example,

if Input.is_action_pressed("left"):
   # animation code
   if animation_player.current_animation != "attack":
        animation_player.play("run")
   # movement code
   vel.x = -100
:bust_in_silhouette: Reply From: Aathish

Ok, so the way I solved this problem was by using
Input.is_action_just_pressed(“ui_x”)
to change the state of a variable “is_attacking”.

This is placed within the main input “if” statement as an elif. I then have another elif check if “is_attacking” is true, and if it is, play the attack animation.

I then connect a signal for Animation_Finished from the AnimatedSprite to the Character’s Script, and inside the function that’s made, ensure that “is_attacking” is made false.

So, back in my physics_process, “is_attacking” will be true for one full animation cycle of the attack animation, and the animation stops after that.

Here’s the code that I used. Declare a var is_attacking outside of the process :

func _physics_process(delta):
       if Input.is_action_pressed("ui_right"):
	     #motion code
        elif Input.is_action_pressed("ui_left"):
	       #motion code

        elif Input.is_action_just_pressed("ui_x"):
	       attacking = true
        elif attacking==true:
	       $char_sprites.play("Attack")

       else:
	      motion.x=0
	      $char_sprites.play("Idle")

     #Other stuff

    func _on_char_sprites_animation_finished():
               is_attacking=false

Hope this helps!

This was so very helpful, thank you!

Now I’m having trouble if I move while attacking it interrupts the animation and after the move animation plays it replays the attack animation until it’s complete.

Note, I am totally new to this and made it a goal to setup the move, idle, jump, fall and attack animations from scratch and need to implement motion now.

kabrewme | 2019-05-06 12:37