Skip to content

Instantly share code, notes, and snippets.

@WolfgangSenff
Created March 29, 2019 12:35
Show Gist options
  • Save WolfgangSenff/de66c402d81b40fb96078a737afb66df to your computer and use it in GitHub Desktop.
Save WolfgangSenff/de66c402d81b40fb96078a737afb66df to your computer and use it in GitHub Desktop.
Get the direction you're moving in, in Godot
func get_direction(pos : Vector2) -> String:
var B := pos # next position
var A := global_position # current position
var direction := B - A
direction = direction.normalized()
var angle = direction.angle()
var cos_angle = rad2deg(cos(angle))
var sin_angle = rad2deg(sin(angle))
var abs_sin = abs(sin_angle)
var abs_cos = abs(cos_angle)
# up and down are switched for me because I'm actually playing an animation, and the animation points up or down when moving down or up, respectively
if abs_cos > abs_sin: # if direction is mostly to the right
if cos_angle > 0:
return "Right"
else:
return "Left"
else:
if sin_angle < 0:
return "Up"
else:
return "Down"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment