movement and animation script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Fabius

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')
:bust_in_silhouette: Reply From: p7f

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.

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

Dlean Jeans | 2018-12-21 06:30