bevy_fps_controller icon indicating copy to clipboard operation
bevy_fps_controller copied to clipboard

Add gamepad support

Open Pyroglyph opened this issue 1 year ago • 2 comments

The controller doesn't currently support gamepads. This PR aims to change that!

Currently, keyboard input is broken as the gamepad is overriding the values in FpsControllerInput.
There are two ways of solving this:

  1. Both input methods need to reach consensus. For example, pressing W on your keyboard and moving the left stick to the left will cause the player to move forward and left.
  2. The game should automatically switch between the two input methods (depending on which one received the most recent input)

I'm not sure which solution is best, so any input is welcome! (pun not intended)

Pyroglyph avatar Mar 17 '24 19:03 Pyroglyph

A quick and dirty change to add input consesus is here:

- input.movement = Vec3::new(move_vec.x, vertical_axis, move_vec.y);
+ input.movement += Vec3::new(move_vec.x, vertical_axis, move_vec.y);
                 ^
- input.sprint = button_input.pressed(button(controller.pad_sprint));
+ input.sprint = input.sprint || button_input.pressed(button(controller.pad_sprint));
                 ^-------------^
- input.jump = button_input.pressed(button(controller.pad_jump));
+ input.jump = input.jump || button_input.pressed(button(controller.pad_jump));
               ^-----------^
- input.fly = button_input.just_pressed(button(controller.pad_fly));
+ input.fly = input.fly || button_input.just_pressed(button(controller.pad_fly));
              ^----------^
- input.crouch = button_input.pressed(button(controller.pad_crouch));
+ input.crouch = input.crouch || button_input.pressed(button(controller.pad_crouch));
                 ^-------------^

This naive implementation is kinda brittle as relies on the gamepad input running after the keyboard input, but it does work.

Pyroglyph avatar Mar 17 '24 21:03 Pyroglyph

After asking around online, someone brought up a good point in favour of the input consensus solution.

The Steam Deck and Steam Controller’s touchpads can result in both mouse and joystick input being used simultaneously. Locking down which input is used prevents this from working as intended.

As such, I think the consensus solution would be the way to go.

Here's a link to the thread if anyone's interested.

Pyroglyph avatar Mar 18 '24 14:03 Pyroglyph