0 votes

Hi,
I want to get the values from the two analog sticks of a Xbox gamepad.
In the Godot Input Map I can't find them (it shows me 15 options, but none of these seem to correspond to the analog sticks).

How can I use the sticks?

Godot version 3.2.3
in Engine by (48 points)

2 Answers

+1 vote

Add this to a Node's script:

func _input(event):
    if event is InputEventJoypadMotion:
        print(
                "Device: %s. Joypad Axis Index: %s. Strength: %s."
                % [event.device, event.axis, event.axis_value]
        )

It should print the device number and the axis index every time you move an analog stick. If it doesn't print anything, then Godot's not detecting your controller.

Once you know the device number and the axis index, you can make that axis trigger an action:

  1. Open the Input Map.
  2. Find or create an action that will be triggered by the analog stick. As an example, I will use the action named ui_left.
  3. To the right of that action, press the plus button.
  4. Select "Joy Axis".
  5. Set the "Device" to the one with the number you determined before.
  6. Set the "Joypad Axis Index" to the one with the number you determined before.
  7. Click "Add".

Now that the action is triggered by the analog stick, you can make the player respond to that action:

func _physics_process(delta):
    if Input.is_action_pressed("ui_left"):
        # Strength will be a number between 0 and 1. 0 means that
        # the axis isn't being pushed at all. 1 means that the
        # axis is being pushed all the way. 0.5 means that the
        # axis is being pushed halfway.
        var strength = Input.get_action_strength("ui_left")
        position.x -= 250 * strength * delta
by (355 points)
edited by

Thanks for the hint. I'll try that :-)

But do the analog sticks really need to be handled via events? During the game they will ALWAYS be moving around, so I would rather just poll the current position in my physics loop. Events seem to be more suited for sporadic events like button presses.
Or am I understanding this the wrong way?

No, analog sticks don't need to be handled using events. You can make a Joystick axis trigger an action. The code I provided is just to help you figure out which device and which axis you're using. Once you know the device number and the axis number, you can use the Input Map to make the axis trigger an action. I'll update my answer with information about analog actions.

0 votes

Input Map joystick axis not button !

func _process(delta: float) -> void:
    Input.get_joy_axis(device ,axis)
by (280 points)
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.