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!

Avery

asked Dec 2, 2016 in Engine by AveryRe (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).

answered Dec 2, 2016 by vnen (773 points)
selected Dec 2, 2016 by AveryRe

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 How to use this Q&A? before posting your first questions.