I was also trying to figure out a similar issue I was having with twin-stick aiming controls, and the code in your question helped me make a breakthrough. These are the changes I made that got it to work:
var deadzone = 0.5
var controllerangle = Vector2.ZERO
var xAxisRL = Input.get_joy_axis(0, JOY_AXIS_2)
var yAxisUD = Input.get_joy_axis(0 ,JOY_AXIS_3)
if abs(xAxisRL) > deadzone || abs(yAxisUD) > deadzone:
controllerangle = Vector2(xAxisRL, yAxisUD).angle()
$Gun.rotation = controllerangle
The abs()function was removing sign info (positive/negative) needed to correctly set the directions in the controllerangle’s Vector2, and the rotate()function you were using “Applies a rotation to the node, in radians, starting from its current rotation”, so was rotating to a relative direction rather than the absolute one you wanted.
I know it’s way late, but I hope this helps.
helped me months later.
thanks
ryanscott | 2020-03-28 03:25
Totally amazing ! Thanks to you
I just pasted the code and modified it with two lines and it worked !!
For those who wants to aim with an arm, you have to flip the texture if you aim toward left so here’s the code :
func rotate_arm():
var deadzone = 0
var controllerangle = Vector2.ZERO
var xAxisRL = Input.get_joy_axis(0, JOY_AXIS_2)
var yAxisUD = Input.get_joy_axis(0 ,JOY_AXIS_3)
if abs(xAxisRL) > deadzone || abs(yAxisUD) > deadzone:
controllerangle = Vector2(xAxisRL, yAxisUD).angle()
arms.rotation = controllerangle
#Your sprite won't be upside down thanks to those two lines. Replace arms by your own node
if xAxisRL < 0 and arms.scale.y > 0:arms.scale.y *= -1
elif xAxisRL > 0 and arms.scale.y < 0:arms.scale.y *= -1
pass
This is indeed the correct answer! After much faffing about and looking everywhere I found this one and made a couple small changes, works beautifully. I use this: