Convert joystick to 360 degree value?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AveryRe
:warning: Old Version Published before Godot 3 was released.

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!

Avery

:bust_in_silhouette: Reply From: vnen

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).

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.

AveryRe | 2016-12-02 21:11

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).

vnen | 2016-12-02 22:35