how to aim right game pad analog stick

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By connozac

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.

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.

GodotUser | 2019-05-26 08:56

:bust_in_silhouette: Reply From: unwiseone

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 :slight_smile:
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

cmzx | 2020-08-07 22:20

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:

float joystickAngle = Input.GetVector("aimLeft", "aimRight", "aimUp", "aimDown").Angle();
Rotation = joystickAngle;