0 votes

Hey there! For a top down twin-stick shooter I am developing I was wondering if it was possible to convert the right sticks current input direction to one I could face my node towards? For example, if the player tilts the right stick right, the player looks right. But I require it to be the specific direction of the stick (360 degrees).

Thanks in advance!


in Engine by (45 points)

1 Answer

+2 votes
Best answer

You can convert it to Vector2 and get the angle from there:

var direction = Vector2(Input.get_joy_axis(0, JOY_ANALOG_1_X), Input.get_joy_axis(0, JOY_ANALOG_1_Y))
var angle = direction.angle()

Note that this angle is in radians. 0 is pointing down and it increases when going counterclockwise and decreases when going clockwise (making negative angles).

by (800 points)
selected by

This worked perfectly for me! Thank you!

Side question, if you would happen to know a method to allow the player to retain his facing direction after the stick is released? Right now I assume the angle is reverting to the neutral offset of the stick, which can appear buggy.

I usually implement a "dead zone" for joystick axes. You can either check each axis individually or check the length of the vector you made, then ignore if it's below a certain threshold (something around 0.3 depending on the joystick).

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.