0 votes

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.

Godot version 3.2
in Engine by (12 points)

1 Answer

+1 vote

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 JOYAXIS2 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
by (291 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.