0 votes

The animation only triggers when I let go of the button and not while it is moving. Can not find a function for is_action_hold or something like that.

extends KinematicBody2D

const GRAVITY = 200
const WALK_SPEED = 250

var velocity = Vector2()
var sprite
var animPlayer

func _ready():
    sprite = get_node("Sprite")
    animPlayer = get_node("AnimationPlayer")
    set_fixed_process(true)

func _fixed_process(delta):

    velocity.y += delta * GRAVITY
    if (Input.is_action_pressed("ui_left")):
        velocity.x = -WALK_SPEED
        sprite.set_frame(0)
        animPlayer.play("walk_left")

    elif (Input.is_action_pressed("ui_right")):
        velocity.x = WALK_SPEED
        sprite.set_frame(4)
        animPlayer.play("walk_right")

    else:
        velocity.x = 0

    var motion = velocity * delta
    motion = move(motion)

    if (is_colliding()):
        var n = get_collision_normal()
        motion = n.slide(motion)
        velocity = n.slide(velocity)
        move(motion)
in Engine by (60 points)

1 Answer

+1 vote
Best answer

is_action_pressed does what you want, it stays true as long as the key is pressed. Because you put your code in _fixed_process, it will run every physics frame. Which means you ask Godot to play "walk_left" every 0.016 seconds. Playing an animation starts from its beginning, so it will look like it doesn't animate because you endlessly stick it at the beginning.

What you can do instead is to check if the animation is playing before, or do this part in _input rather than _fixed_process.

by (29,090 points)
selected by
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.