XBox Gamepad: how to use analog sticks?

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

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?

:bust_in_silhouette: Reply From: Jayman2000

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

Thanks for the hint. I’ll try that :slight_smile:

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?

VanKurt | 2021-03-04 20:00

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.

Jayman2000 | 2021-03-04 21:54

:bust_in_silhouette: Reply From: brainbug

Input Map joystick axis not button !

func _process(delta: float) -> void:
	Input.get_joy_axis(device ,axis)