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.