Im trying to make a top down 2D shooter game and I want to add controller support to it but im not sure how to make it so that if someone pointed the right stick left the character looked left. or if the player pointed the stick right the character would look right.
If you are using Input.get_vector
for looking you can get an angle from the resulting vector2’s .angle()
function.
var direction := Input.get_vector("left", "right", "up", "down")
rotation = direction.angle()
I think we need a little more information about exactly what you want. Should the character be able to face in any direction, or eight, or only four? These are all common behaviors in 2D top-down games, and there are certainly more examples out there.
If you only need a few directions (8 or less) it probably makes more sense not to rotate the actual character at all, and just change the visual of the character (flipping the sprite if needed) to match the direction. This implementation will be quite simple compared to rotating the node, and it can make it easier to keep pixel art looking nice, if that matters to you.
If you want to rotate the Node2D, that’s also very doable, but more math will be involved, even if it’s just you calling rotate() or look_at().
The script above is a nice starting point for your intended behavior.