Created
March 29, 2019 12:35
-
-
Save WolfgangSenff/de66c402d81b40fb96078a737afb66df to your computer and use it in GitHub Desktop.
Get the direction you're moving in, in Godot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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