How do I use the right stick on a gamepad/controller to move the camera around in an FPS?

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

I am very new to Godot, I just started yesterday as a matter of fact after coming from using Unity for a couple of years now. I am really loving this engine, however I have hit a strong wall where I am trying to make an FPS with controller support and my issue is that I can’t figure out how to move the camera with the right stick.

In these lines below, “head” is the parent to my camera:

if event is InputEventMouseMotion:
	head.rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))

So now, how do I do that but with a gamepad and using the right analog stick? From my research, it seems like this is a very common issue that no one seems to be able to figure out. Even the available documentation doesn’t help me.

If someone has experience with this and has learned how to properly use the right stick in an FPS, I would greatly appreciate any help.

:bust_in_silhouette: Reply From: archeron

I’m using something like the following to turn my camera in a Third-Person view (the script the following code is in is attached to a CameraGimbal node, which has an “InnerGimbal” child node, which then contains the camera. The script will orbit the camera around the player (which you will obviously have to fix in a first person view), but it demonstrates how you can access the gamepad’s different analog axes.

func _ready():
	camera = get_node("InnerGimbal/Camera")

func _process(delta):
	
	if Input.get_connected_joypads().size() == 0:
		return		
		
	var xAxis = Input.get_joy_axis(0, JOY_AXIS_2)
	if abs(xAxis) > JOY_DEADZONE:
		if xAxis >0:
			xAxis = (xAxis-JOY_DEADZONE) * JOY_AXIS_RESCALE
		else:
			xAxis = (xAxis+JOY_DEADZONE) * JOY_AXIS_RESCALE
		rotate_object_local(Vector3.UP, -xAxis * delta * JOY_ROTATION_MULTIPLIER)
		
	var yAxis = Input.get_joy_axis(0, JOY_AXIS_3)
	if abs(yAxis) > JOY_DEADZONE:
		if yAxis >0:
			yAxis = (yAxis-JOY_DEADZONE) * JOY_AXIS_RESCALE
		else:
			yAxis = (yAxis+JOY_DEADZONE) * JOY_AXIS_RESCALE
		$InnerGimbal.rotate_object_local(Vector3.RIGHT, -yAxis * delta * JOY_ROTATION_MULTIPLIER/2)
		$InnerGimbal.rotation.x = clamp($InnerGimbal.rotation.x, -1.0, 1.0)
		emit_signal("camera_moved", camera)

Note the constants in caps - I have these defined like so, but in a real game the user would need to be able to configure these manually (also, a real game should give the player the opportunity to redefine the axis - I just use a hard-coded JOY_AXIS_2 because I’m still in dev mode).

const JOY_DEADZONE = 0.2
const JOY_AXIS_RESCALE = 1.0/(1.0-JOY_DEADZONE)
const JOY_ROTATION_MULTIPLIER = 200.0 * PI / 180.0

Thanks for the help I was looking for a way to do this then found this question and your answer. Worked flawlessly aside from me typing in the wrong JOY_AXIS on the second bit there.

jGames20 | 2021-05-10 23:14

:bust_in_silhouette: Reply From: AveragePeter

I took a weird approach to the controller, I’m doing a Third person controller though I think this might give you an idea.

func _init():
    has_controller = Input.get_connected_joypads().size() > 0
func _input():
    if event is InputEventJoypadMotion and has_controller:
    	if event.get_axis() == JOY_AXIS_2:
    		if abs(event.get_axis_value()) > JOY_DEADZONE:
    			camera_velocity.y = (event.get_axis_value() * JOY_H_SENS)
    		else:
    			camera_velocity.y = 0
    	elif event.get_axis() == JOY_AXIS_3:
    		if abs(event.get_axis_value()) > JOY_DEADZONE:
    			camera_velocity.x = (event.get_axis_value() * JOY_V_SENS)
    		else:
    			camera_velocity.x = 0
func _process(delta):
   cameraAnchor.rotate_y(deg2rad(camera_velocity.y))
   cameraAnchor.rotation.x += deg2rad(camera_velocity.x)

   cameraAnchor.rotation.x =clamp(cameraAnchor.rotation.x,deg2rad(-80),deg2rad(80))

I used some constants to prevent the Input event to record really small values.

const JOY_DEADZONE : float = 0.25
const JOY_V_SENS : int = 3
const JOY_H_SENS : int = 2