Godot Version
4.6
Question
I can make my player move with correct animations in 4 directions, but when moving diagonally, the sprite will move, but only with a static frame, no animations. I’ll post the code I have (it’s copy/pasted from the tutorial).
func _process(delta):
var velocity = Vector2.ZERO # The player's movement vector.
if Input.is_action_pressed("move_right"):
velocity.x += 1
if Input.is_action_pressed("move_left"):
velocity.x -= 1
if Input.is_action_pressed("move_down"):
velocity.y += 1
if Input.is_action_pressed("move_up"):
velocity.y -= 1
I was trying to combine if statements as follows:
if Input.is_action_pressed("move_left") and Input.is_action_pressed("move_up"):
velocity.y += 1
Then I wanted to use the following code to trigger the animation:
if velocity.y < 0:
$AnimatedSprite2D.animation = "run_down"
elif velocity.y != 0:
$AnimatedSprite2D.animation = "run_up"
Basically, I wanted the run_up animation to trigger anytime the y velocity is greater than zero. I want run_down to trigger anytime the y velocity is less than zero. The run_right and run_left animations would follow the same idea.
The engine was not impressed.
Is there a quick way to do this, or do I have to combine a bunch of elif statements?