0 votes

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.isactionpressed("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.

in Engine by (12 points)

3 Answers

+1 vote

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.

by (262 points)
0 votes

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
by (1,567 points)
+1 vote

Ok, so the way I solved this problem was by using
Input.isactionjustpressed("uix")
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 AnimationFinished from the AnimatedSprite to the Character's Script, and inside the function that's made, ensure that "isattacking" is made false.

So, back in my physicsprocess, "isattacking" 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!

by (16 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.