Hi, how can I make my character aim to right stick angle of the gamepad? I'm trying to make a top down shooter

In 2D or 3D? In 2D its a little easier, as you can just use the angle of the Vector2 from the gamepad on the rotation or rotation_degrees property. The harder part with 2D is just knowing how much of an angle offset you need to get the sprite looking in the right direction.

For 3D, its a little harder because of how Godot handles rotation in 3D, especially rotation over 180 degrees. I would suggest converting the Vector2 from the gamepad joystick axis to an angle, and then making a new Basis and rotating it by the angle. You could also try manually altering the rotation property of the Spatial node, which may work. Another possible solution for 3D is converting the gamepad axis to a Vector3 offset (one axis will need to be 0), then use look_at to rotate the player to look at the offset. Something like look_at(global_transform.origin + joypad_vector2, Vector3.UP)

@TwistedTwigleg said: In 2D or 3D? In 2D its a little easier, as you can just use the angle of the Vector2 from the gamepad on the rotation or rotation_degrees property. The harder part with 2D is just knowing how much of an angle offset you need to get the sprite looking in the right direction.

For 3D, its a little harder because of how Godot handles rotation in 3D, especially rotation over 180 degrees. I would suggest converting the Vector2 from the gamepad joystick axis to an angle, and then making a new Basis and rotating it by the angle. You could also try manually altering the rotation property of the Spatial node, which may work. Another possible solution for 3D is converting the gamepad axis to a Vector3 offset (one axis will need to be 0), then use look_at to rotate the player to look at the offset. Something like look_at(global_transform.origin + joypad_vector2, Vector3.UP)

it's 2d

it's about a 2d top down shooter with 360° aim

For 2D, you can just use something like this to look at the mouse (untested):

func look_at_mouse():
	var mouse_pos = get_global_mouse_position()
	var mouse_direction = global_position.direction_to(mouse_pos)
	var mouse_angle = atan2(mouse_direction.y, mouse_direction.x)
	# you can also use the following instead of the line above
	# var mouse_angle = mouse_direction.angle()
	# its the same code internally either way
	
	rotation = mouse_angle

@TwistedTwigleg said: For 2D, you can just use something like this to look at the mouse (untested):

func look_at_mouse():
	var mouse_pos = get_global_mouse_position()
	var mouse_direction = global_position.direction_to(mouse_pos)
	var mouse_angle = atan2(mouse_direction.y, mouse_direction.x)
	# you can also use the following instead of the line above
	# var mouse_angle = mouse_direction.angle()
	# its the same code internally either way
	
	rotation = mouse_angle

i need a script for the controller, where you aim with the right stick

Oh, my bad. For a joystick, its more or less the same (untested):

var joystick_vector = Vector2(Input.get_joystick_axis(2), Input.get_joystick_axis(3))
var joystick_angle = atan2(joystick_vector.y, joystick_vector.x)
rotation = joystick_angle

@TwistedTwigleg said: Oh, my bad. For a joystick, its more or less the same (untested):

var joystick_vector = Vector2(Input.get_joystick_axis(2), Input.get_joystick_axis(3))
var joystick_angle = atan2(joystick_vector.y, joystick_vector.x)
rotation = joystick_angle

it says "Invalid call. Nonexistent function 'get_joystick_axis' in base 'InputDefault'."

4 months later

I have the same issue but i figured it out!

Use this code:

var controllerangle = Vector2.ZERO
var xAxisRL = Input.get_joy_axis(0, JOY_AXIS_2)
var yAxisUD = Input.get_joy_axis(0 ,JOY_AXIS_3)
controllerangle = Vector2(xAxisRL, yAxisUD).angle()
rotation = controllerangle

Thanks @gerson0410 for posting a working solution!

Also: Sorry @MagicFool64 for not replying, I forgot or missed the reply entirely. Either way, my apologizes.

a year later

@gerson0410 thx for your reponse <3 ! :+1:

Hi @Aroun you might have to verify your account with the email we sent (if it's been a while, search your email).

10 months later
5 months later

Hello all! I'm trying to use the solution that @gerson0410 put but I get the following error:
Identifier "JOY_AXIS_2" not declared in the current scope.
Identifier "JOY_AXIS_3" not declared in the current scope.
Where do I declare these? Do they need to be assigned in Input Manager?

    a year later

    pogi-dev You can use JOY_AXIS_RIGHT_X to replace JOY_AXIS_2 and JOY_AXIS_RIGHT_Y to replace JOY_AXIS_3.