2D Joystick Player Rotation

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

I have been making a 2D top-down shooting game. I have made it so my Xbox controller can move my character (left right up down etc.) and I was wondering how I could make the other joystick rotate my character to look in the direction my joystick is going? I am using GDScript and I am a beginner so if possible, even a simplified only semi working version would make me happy for now… Thanks :slight_smile:

:bust_in_silhouette: Reply From: Pixelope

Just this morning I’ve been trying to figure this out, so far I have the following:

var rs_look = Vector2()

func _physics_process(_delta):
	rslook()

func rslook():
	rs_look.y = Input.get_joy_axis(0, JOY_AXIS_3)
	rs_look.x = Input.get_joy_axis(0, JOY_AXIS_2)
	rotation = rs_look.angle()

I have a variable for the right stick set to Vector 2. I then created a new function called rslook() which i call in the physics process. In the rslook() function I then add the y and x axis input (deviceID, AXIS) and then set rotation.

Note: The Udemy course I’m doing tells us to create new functions and then reference it in the physics process, I guess to keep the code organised (I’m not sure), however you can ignore the rslook() function altogether and put just the code in the physics_process like this and it works still:

var rs_look = Vector2()

func _physics_process(_delta):
   rs_look.y = Input.get_joy_axis(0, JOY_AXIS_3)
   rs_look.x = Input.get_joy_axis(0, JOY_AXIS_2)
   rotation = rs_look.angle()

I just need to set up deadzones properly because without pressing the right stick it moves slightly, so having deadzones properly set up should resolve this…but I haven’t got that far yet. I hope this all makes sense, I too am very new to godot/gdscript.

– EDIT –
Here is the updated code with deadzone added, play around with the deadzone value, don’t forget to take into consideration ‘stick flick’, if your deadzone is too low, when you release the stick it might flick back and turn the player by accident:

var rs_look = Vector2(0,0)
var deadzone = 0.3

func _physics_process(_delta):
	rslook()

func rslook():
	rs_look.y = Input.get_joy_axis(0, JOY_AXIS_3)
	rs_look.x = Input.get_joy_axis(0, JOY_AXIS_2)
	if rs_look.length() >= deadzone:
		rotation = rs_look.angle()

My player doesn’t rotate by the own axis.
How can I fix this?

Joshfah | 2021-03-09 03:23

:bust_in_silhouette: Reply From: njamster

See this answer. If you have any concrete questions regarding that, feel free to ask them here in the comments and I’ll gladly help you out.