How do I use 2 directional Sprite in a Top Down Game

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

Hi I’m new in programming and I kinda wanna know how to use 2 Directional Sprite in a Top Down Game which gives a 4 or 8 direction controls.

I’m using this code to move my character but the things is I don’t know how to animate with only 2 Direction Sprite. Thank you!

func _physics_process(delta):

func move_state(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 input_vector != Vector2.ZERO:
	velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
	velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)

func move():
  velocity = move_and_slide(velocity)
:bust_in_silhouette: Reply From: MrEliptik

Please next time, paste your complete code and even node setup if needed. That way it’s much easier to reproduce.

I guess you could check your velocity vector normalized against the UP vector to see if you’re going up or not. You can then rotate your sprite accordingly. Vector math — Godot Engine (stable) documentation in English

var orientation = velocity.normalized().dot(Vector2.UP)
# rotate the sprite accordingly

This will return 1 when going up and -1 going down. Is it what you’re trying to achieve?

:bust_in_silhouette: Reply From: ClumsyDog

Stick this somewhere in your code (this is assuming your sprite faces right by default, if it faces left by default, change ‘<’ to ‘>’):

sprite.flip_h = input_vector.x < 0