I have been making a 2D Top down shooter. I have made so Controller (Xbox Controller) can move my player. Now I wan't to rotate my player by the own axis, if I rotate the right joystick. I don't know how I can make this can anyone help me with the script and so.

PS: Sorry for my bad english, i'm german.

This discussion was caught in the moderation queue since you have not confirmed your account yet.
Upon creating your account you should have received an account verification email. The confirmation email may have been incorrectly flagged as spam, so please also check your spam filter. Without confirming your account, future posts may also be caught in the moderation queue. You can resend a confirmation email when you log into your account if you cannot find the first verification email.
If you need any help, please let us know! You can find ways to contact forum staff on the Contact page. Thanks! :smile:

Welcome to the forum @Joshfah!

If you have a Vector2 with the joystick value, then you can convert it to a rotation using atan2:

rotation = atan2(joystick_vector.y, joystick_vector.x)

@TwistedTwigleg said: Welcome to the forum @Joshfah!

If you have a Vector2 with the joystick value, then you can convert it to a rotation using atan2:

rotation = atan2(joystick_vector.y, joystick_vector.x)

I don't know how to get the joystick value. Sry im new at godot.

@Joshfah said:

@TwistedTwigleg said: Welcome to the forum @Joshfah!

If you have a Vector2 with the joystick value, then you can convert it to a rotation using atan2:

rotation = atan2(joystick_vector.y, joystick_vector.x)

I don't know how to get the joystick value. Sry im new at godot.

No worries! There are probably some tutorials on how to get joystick input, but here's a quick code snippet and the documentation for the function:

# 0 = first device and the second joystick axis is the horizontal movement
# of the right joystick
var joystick_horizontal = Input.get_joy_axis(0, JOY_AXIS_2)
# Same thing, but using the third joystick axis, which is the vertical
# movement of the right joystick
var joystick_vertical = Input.get_joy_axis(0, JOY_AXIS_3)

# Then we can use atan2!
# (No Vector2 needed if we have both the horizontal and vertical values)
rotation = atan2(joystick_vertical, joystick_horizontal)

@TwistedTwigleg said:

@Joshfah said:

@TwistedTwigleg said: Welcome to the forum @Joshfah!

If you have a Vector2 with the joystick value, then you can convert it to a rotation using atan2:

rotation = atan2(joystick_vector.y, joystick_vector.x)

I don't know how to get the joystick value. Sry im new at godot.

No worries! There are probably some tutorials on how to get joystick input, but here's a quick code snippet and the documentation for the function:

# 0 = first device and the second joystick axis is the horizontal movement
# of the right joystick
var joystick_horizontal = Input.get_joy_axis(0, JOY_AXIS_2)
# Same thing, but using the third joystick axis, which is the vertical
# movement of the right joystick
var joystick_vertical = Input.get_joy_axis(0, JOY_AXIS_3)

# Then we can use atan2!
# (No Vector2 needed if we have both the horizontal and vertical values)
rotation = atan2(joystick_vertical, joystick_horizontal)

I don't know why but, when I type this in the script it doesn't work. I didn't use a Kinematicbody2D for the player because I need the position of the Node. I used a Node2D

@Joshfah said:

@TwistedTwigleg said:

@Joshfah said:

@TwistedTwigleg said: Welcome to the forum @Joshfah!

If you have a Vector2 with the joystick value, then you can convert it to a rotation using atan2:

rotation = atan2(joystick_vector.y, joystick_vector.x)

I don't know how to get the joystick value. Sry im new at godot.

No worries! There are probably some tutorials on how to get joystick input, but here's a quick code snippet and the documentation for the function:

# 0 = first device and the second joystick axis is the horizontal movement
# of the right joystick
var joystick_horizontal = Input.get_joy_axis(0, JOY_AXIS_2)
# Same thing, but using the third joystick axis, which is the vertical
# movement of the right joystick
var joystick_vertical = Input.get_joy_axis(0, JOY_AXIS_3)

# Then we can use atan2!
# (No Vector2 needed if we have both the horizontal and vertical values)
rotation = atan2(joystick_vertical, joystick_horizontal)

I don't know why but, when I type this in the script it doesn't work. I didn't use a Kinematicbody2D for the player because I need the position of the Node. I used a Node2D

Well, the code above is just an example. Did you put the code in the _process function or _physics_process function? If you want, if you share the code you are using I can try to take a look and see if I can figure out what is wrong.

Also, no worries on the node choice. Any Node2D-based node should have the rotation property, so it should work just fine with a Node2D node.

It's in the _physics_process function. Here is my code:

extends Node2D

var bullet = preload("res://Szenen/Censored.tscn")

onready var ship = $ship
onready var cursor = $Arrow
onready var camera = $ship/Camera2D

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
	

func _physics_process(delta):
	var move = Vector2()
	if Input.is_action_pressed("up"):
		move.y -= 1
	if Input.is_action_pressed("down"):
		move.y += 1
	if Input.is_action_pressed("left"):
		move.x -= 1
	if Input.is_action_pressed("right"):
		move.x += 1
	if Input.is_action_just_pressed("shoot"):
		shoot()
	
	ship.position += move * 600 * delta
	
	var joystick_horizontal = Input.get_joy_axis(0, JOY_AXIS_2)
	var joystick_vertical = Input.get_joy_axis(0, JOY_AXIS_3)
	
	var mouseposition = get_global_mouse_position()
	var dif = mouseposition - ship.global_position
	
	ship.rotation = atan2(dif.y, dif.x)
	
	cursor.position = mouseposition
	cursor.rotation = ship.rotation
	
	camera.offset_h = dif.x * 0.003
	camera.offset_v = dif.y * 0.004

func shoot():
	var p: Node2D = bullet.instance()
	p.rotation = ship.rotation
	p.position = ship.position + Vector2(130,0).rotated(ship.rotation)
	add_child(p)

Looking at the code, I don't see where the joystick horizontal and vertical values are being used to affect the rotation, but the mouse seems to be affecting rotation currently. To get it working, I think you just need to add the following under line 29:

ship.rotation = atan2(joystick_vertical, joystick_horizontal)

And then the joystick should be able to rotate the ship. However, it may seem that nothing is working because layer, in line 34, you are setting the rotation based on the mouse position. Are you trying to have both the mouse and joystick affect rotation? If so, you will almost certainly need some way to tell the code which to use, as the rotation will be overridden by one or the other. For example, if the joystick is moving and the code for rotating to look at the mouse is left as-is, then only one of the two input methods (mouse or joystick) will work and the other will not.

2 years later