0 votes

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)
Godot version latest
in Engine by (46 points)

2 Answers

0 votes
Best answer

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
by (118 points)
selected by
0 votes

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. https://docs.godotengine.org/en/stable/tutorials/math/vector_math.html#dot-product

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?

by (643 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.