+2 votes

I'm trying to run animation of walking on my player, if i press right arrow the animations plays but i want it to loop as long as i hold the key.

Does anyone know how to do this?

This is some of the code:

...
    func _process(delta):

        velocity.y += delta * GRAVITY
        if (Input.is_action_pressed("ui_left")):
            velocity.x = - WALK_SPEED
            anim.play("walk")

        elif (Input.is_action_pressed("ui_right")):
            velocity.x = WALK_SPEED
            anim.play("walk")

        else:
            velocity.x = 0

    ....

    func _ready():
        anim = get_node("AnimationPlayer")
        set_process(true)
in Engine by (17 points)
recategorized by

3 Answers

+1 vote
Best answer

The problem here is that you restart the animation at each frame, so you don't let it run fully, and it likely appears like it's not working until you stop pressing the button.

You should keep track of the currently playing animation, and only run anim.play() when the animation should change.

Check the platformer 2D demo, DynaDungeons, Minilens (simple ones) or Jetpaca (more complex, search for the new_anim string).

by (1,958 points)
selected by

Great, tnx :)

0 votes

Is the animation looped in the animation player?

There's a button to make it loop, walking animations should be looped. Maybe it would still work if you use while instead of if.

I made a character walk by just playing from idle to walking at keypress, then from walking back to idle.

by (372 points)

yes the animation is looped in the player.

The problem here is, I think, that function _process() is called every frame.
So because of that the animation always starts from the beginning, do you
know if there is the way to delay the play of animation until it hits the last
key frame?

0 votes

maybe you should use _input() instead of _process()

regards
-j

by (1,484 points)

Thank you so much my man. That was it :)

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 [email protected] with your username.