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():

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


func animations(velocity):

if velocity.y == -1:
if velocity.y == 1:
if velocity.x == -1:
if velocity.x == 1:
if velocity == Vector2():
	if $AnimatedSprite.animation == 'walk_up':
	elif $AnimatedSprite.animation == 'walk_down':
	elif $AnimatedSprite.animation == 'walk_left':
	elif $AnimatedSprite.animation == 'walk_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