Topic was automatically imported from the old Question2Answer platform.
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.
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).
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.