Judging from your code-snippet, you can walk diagonally too, but you only assigned 4 directions to your spriteloop.
You either add additional conditions for diagonal movement or you completely switch to 4 direction movement.
Conditions for diagonal movement would be (keep everything as is, but add 4 extra conditions to your spriteloop):
if dir.right and dir.up:
sprite_dir = "up" # or right, depends on your preference
if dir.right and dir.down:
sprite_dir = "down" # or right, again your preference what you like more
Or you switch to 4 way movement instead of 8 with the following code example (just switch out input code, keep everything else as is):
func get_input():
velocity = Vector2()
if Input.is_action_pressed("ui_right"):
velocity.x += 1
elif Input.is_action_pressed("ui_left"):
velocity.x -= 1
elif Input.is_action_pressed("ui_down"):
velocity.y += 1
elif Input.is_action_pressed("ui_up"):
velocity.y -= 1
movement = velocity.normalized() * speed
and have your process set up like
func _process(delta):
get_input()
move_and_slide(movement)
The second version is what I have switched to in my game. It fits more to the retro arcade style I try to follow, where you only could walk in 4 directions.
Would be nice if you could report back if this helped.