You probably want to do something using the input_vector
code below at line 32 instead of checking each individual input, as this will allow you to do some processing to more easily detect diagonal movement. It will require some adjusting for idle animations though, but if you store the last non zero value of input_vector
then you can use it for telling which idle animation to choose.
There are many ways to do this. The easiest way is to do some conditions to tell if movement is diagonal or not, and then which direction. I have not tested it, but something like this should work:
onready var _animated_sprite = $AnimatedSprite
var last_direction = Vector2.ZERO
func _physics_process(delta):
var input_vector = Vector2.ZERO
input_vector.x = Input.get_action_strength(“ui_right”) - Input.get_action_strength(“ui_left”)
input_vector.y = Input.get_action_strength(“ui_down”) - Input.get_action_strength(“ui_up”)
Input_vector = input_vector.normalized()
# if we are moving...
if input_vector.length_squared() > 0:
# Store the direction for use later when needing
# to choose the idle animation
last_direction = input_vector
# Diagonal movement
if input_vector.x != 0 and input_vector.y != 0:
# going down?
if input_vector.y > 0:
if input_vector.x > 0:
_animated_sprite.play(“run_down_right”)
else:
_animated_sprite.play(“run_down_left”)
else:
if input_vector.x > 0:
_animated_sprite.play(“run_up_right”)
else:
_animated_sprite.play(“run_up_left”)
# Otherwise we are moving either horizontal or vertical, so its just a
# matter of finding out which.
# horizontal movement
elif input_vector.x != 0:
if input_vector.x > 0:
_animated_sprite.play(“run_left”)
else:
_animated_sprite.play(“run_right”)
# vertical movement
else:
if input_vector.y > 0:
_animated_sprite.play(“run_down”)
else:
_animated_sprite.play(“run_up”)
# if we are not moving, use the last known movement
else:
# Diagonal movement
if last_direction.x != 0 and last_direction.y != 0:
# going down?
if last_direction.y > 0:
if last_direction.x > 0:
_animated_sprite.play(“idle_down_right”)
else:
_animated_sprite.play(“idle_down_left”)
else:
if last_direction.x > 0:
_animated_sprite.play(“idle_up_right”)
else:
_animated_sprite.play(“idle_up_left”)
# Otherwise we are moving either horizontal or vertical, so its just a
# matter of finding out which.
# horizontal movement
elif last_direction.x != 0:
if last_direction.x > 0:
_animated_sprite.play(“idle_left”)
else:
_animated_sprite.play(“idle_right”)
# vertical movement
else:
if last_direction.y > 0:
_animated_sprite.play(“idle_down”)
else:
_animated_sprite.play(“idle_up”)
Hopefully this helps a bit :smile: