0 votes

So I am making a 2D game and I need a walking animation. So I am using an AnimatedSprite and there is just one HUGE problem. So the AnimatedSprite node only playes the animation if the player is pressing right. Here is my code so far:

var test = false

func process(delta):
if test == true:
$AnimatedSprite.play("walk")
if Input.is
actionpressed("uileft"):
test = true
if Input.isactionpressed("uiright"):
test = true
if Input.is
actionpressed("uiup"):
test = true
if Input.isactionpressed("ui_down"):
test = true
else:
test = false

Godot version 3.3.2
in Engine by (50 points)

1 Answer

0 votes
Best answer

Hello,
This is because your if s are all tested, so in the last one :

if Input.is_action_pressed("ui_down"):
    test = true
else:
    test = false

the test = false part will run if the ui_down action is not pressed, regardless of other actions.

You should use elif (shorthand for else and if combined) to prevent this:

var test = false

func process(delta):
    if test == true:
        $AnimatedSprite.play("walk")
    if Input.is_action_pressed("ui_left"): # ui_left ?
        test = true
    elif Input.is_action_pressed("ui_right"): # then, ui_right ?
        test = true
    elif Input.is_action_pressed("ui_up"): # then, ui_up ?
        test = true
    elif Input.is_action_pressed("ui_down"): # then, ui_down ?
        test = true
    else:
        # none of them is pressed
        test = false
by (728 points)
selected by
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.