+1 vote
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")
in Engine by (13 points)

1 Answer

0 votes

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.getactionstrength() 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")
by (911 points)

Thanks, I has resolve

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.