0 votes

Hello to everyone.
I have an NPC that walks forward but without the animation. Is there a mistake in the script?
Thanks!

extends KinematicBody2D
var movement = Vector2()
var speed = 150

func _ready():
    pass

func _process(delta):
    var velocity = Vector2()
    move_and_collide(Vector2(0,+1))

    if velocity.x == 1:
        $RayCast2D.cast_to = Vector2(0, 50)

    var movement = 250*velocity.normalized()*delta

    self.move_and_collide(movement)
    self.update_animated_sprite(velocity)

func update_animated_sprite(velocity):
    if velocity.x == -1:
        $AnimatedSprite.flip_h = false
        $AnimatedSprite.play('walk_left')
    elif velocity.x == 1:
        $AnimatedSprite.flip_h = true
        $AnimatedSprite.play('walk_left')
    elif velocity.y == -1:
        $AnimatedSprite.play('walk_up')
    elif velocity.y == 1:
        $AnimatedSprite.play('walk_down')
in Engine by (12 points)

1 Answer

+1 vote

I think the problem is that you are calling $AnimatedSprite.play on each process call, so animation restarts and it seems to be without animation. You shoud try for example
$AnimatedSprite.animation = "walk_down" instead.

by (3,388 points)

Same for AnimationPlayer if you're using one:
$AnimationPlayer.current_animation = 'Walk'

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.