Help with diagonal animation

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DarlesLSF

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?

:bust_in_silhouette: Reply From: whiteshampoo

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("...")

Thx so much, u solved my problem :smiley:

DarlesLSF | 2020-07-18 12:05