+1 vote

I have got the left analog stick to move the player around but I am now struggling to aim with the right analog stick.

I have found documentation for the sprite to look in the direction of a mouse but cannot find anything for aiming an analog stick.

    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(abs(xAxisRL), abs(yAxisUD)).angle()
        $Gun.rotate(controllerangle)

I get pulling in the joystick x and y but not the rotation to a point direction.

asked Nov 1, 2018 in Projects by connozac (13 points)

Hi,

Sorry, I don't have an answer to this question.

I am also trying to figure out how to aim with the Xbox Gamepad right analog stick.

I only figured out how to simply set the input map to move with the left analog stick, which is simple and requires no coding.

Aim requires some code.

Hope this question gets answered, I have search for a long time.

1 Answer

0 votes

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.

answered Oct 7, 2019 by unwiseone (20 points)
edited Oct 7, 2019 by unwiseone

helped me months later.

thanks

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