Animation Problem

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Noth
I have a problem with my animation. The program works but when I press left and right at the same time or up and down one of the two wins on the other. This is the code: 

extends KinematicBody2D

func _ready():
pass

func _process(delta):

var velocity = Vector2()

if Input.is_key_pressed(KEY_UP):
	velocity.y = -1
if Input.is_key_pressed(KEY_DOWN):
	velocity.y = +1
if Input.is_key_pressed(KEY_RIGHT):
	velocity.x = +1
if Input.is_key_pressed(KEY_LEFT):
	velocity.x = -1
	
var movement = velocity.normalized()*500*delta

self.move_and_collide(movement)
self.animations(velocity)

func animations(velocity):

if velocity.y == -1:
	$AnimatedSprite.play("walk_up")
if velocity.y == 1:
	$AnimatedSprite.play("walk_down")
if velocity.x == -1:
	$AnimatedSprite.play("walk_left")
if velocity.x == 1:
	$AnimatedSprite.play("walk_right")
	
if velocity == Vector2():
	if $AnimatedSprite.animation == 'walk_up':
		$AnimatedSprite.play("stand_up")
	elif $AnimatedSprite.animation == 'walk_down':
		$AnimatedSprite.play("stand_down")
	elif $AnimatedSprite.animation == 'walk_left':
		$AnimatedSprite.play("stand_left")
	elif $AnimatedSprite.animation == 'walk_right':
		$AnimatedSprite.play("stand_right")
:bust_in_silhouette: Reply From: flurick

Ah yes the old input vector. This is very specific to each project how it is needed to be set up. The minimal change is to change the absolute “=” with the relative “x -= 1” and “x += 1” etc. (with a relative value you also need to reset it to zero each frame).

Theres also the Input.get_action_strength() function that uses the projects InputMap, with that it would be something like

velocity.y = Input.get_action_strength("left") - Input.get_action_strength("right")

Thanks, I has resolve

Noth | 2019-12-21 15:12