+1 vote

Hello, when I walk with the player to the left, right, up and down directions, my animations work fine, but when I try to walk in the diagonal direction, dont play any animation, just stay in the idle frame.

enter image description here

And here's the code:

    var mover : Vector2

mover.x = Input.get_action_strength("direita") - Input.get_action_strength("esquerda")
mover.y = Input.get_action_strength("baixo") - Input.get_action_strength("cima")

if abs(mover.x) == 1 and abs(mover.y) == 1:
    mover = mover.normalized()
var movendo = speed * mover

# sprites da movimentaĆ§Ć£o
if mover.x == 1:
    $Sprite/anims.play("direita")
    frame = 4
if mover.x == -1:
    $Sprite/anims.play("esquerda")
    frame = 10
if mover.y == 1:
    $Sprite/anims.play("baixo")
    frame = 7
if mover.y == -1:
    $Sprite/anims.play("cima")
    frame = 1
if mover.x == 0 and mover.y == 0:
    $Sprite/anims.stop()
    $Sprite.frame = frame
move_and_slide(movendo)

How can I fix that?

in Engine by (171 points)

1 Answer

+3 votes
Best answer

if your mover-variable is (1.0, 1.0) and you normalize it, it becomes approx.: (0.7, 0.7)
You check for x/y to be exactly 1.0
You should replace == 1/-1 with >= 0.0 / <= 0.0

EDIT: At the moment it would also not work with analog-input, because x/y can be less 1.0


Tip:
Don't use $Sprite/anims more then 1 time! if you decide to rename it, or make it a child of another node, you have to track down every line with that.
It's better you add

onready anim : AnimationPlayer = $Sprite/anims

So you can use anim instead of $Sprite/anims. And if you have to change name/path you only have to change it in 1 line.

EDIT:

$Sprite/anims.play("...")

becomes:

anim.play("...")
by (1,448 points)
selected by

Thx so much, u solved my problem :D

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.