+1 vote

Hy, i don't know how to change the animation back to idle when the attack anim has finished. Here is the code:

func physicsprocess(delta):
motion.y+=GRAVITY

if Input.is_action_pressed("ui_right"):
    motion.x=SPEED
    $AnimatedSprite.flip_h=false
    if is_on_floor():
        $AnimatedSprite.play("walk")
elif Input.is_action_pressed("ui_left"):
    motion.x=-SPEED
    $AnimatedSprite.flip_h=true
    if is_on_floor():
        $AnimatedSprite.play("walk")
elif is_on_floor():
    motion.x=0
    $AnimatedSprite.play("idle")

if is_on_floor() and Input.is_action_just_pressed("ui_up"):
    motion.y=JUMP

if is_on_floor() and Input.is_action_just_pressed("attack"):
    $AnimatedSprite.play("attack1")

if !is_on_floor():
    $AnimatedSprite.play("jump")

motion=move_and_slide(motion,UP)
in Engine by (29 points)

Did you solve the problem?

3 Answers

0 votes

AnimationPlayer has a 'finished' signal you can connect to. It passes the animation that finished. You can check that and kick off a different animation.

by (143 points)
0 votes

Create a variable that refers to the string name of the current animation. Connect the AnimatedSprite's animation_finished signal to a function:

$AnimatedSprite.connect("animation_finished", self, "name_of_function")

The function can check if the variable == "attack1" and then change it to "idle" if so. This requires replacing the string literals referring to the animations with the variable, which will be changed whenever you want to change animations.

by (533 points)
0 votes

Below the walk inputs you can place...

else:
    motion.x = 0
    if is_on_floor():
        $AnimatedSprite.play("idle")
by (103 points)
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 Frequently asked questions and 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.