+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:
    frame = 4
if mover.x == -1:
    frame = 10
if mover.y == 1:
    frame = 7
if mover.y == -1:
    frame = 1
if mover.x == 0 and mover.y == 0:
    $Sprite.frame = frame

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

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.




by (1,520 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 Frequently asked questions and 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 web[email protected] with your username.