docs/bevy/examples/input/gamepad_input.rs 962 B · 30 lines · raw · history

1 //! Shows handling of gamepad input, connections, and disconnections.
2
3 use bevy::prelude::*;
4
5 fn main() {
6 App::new()
7 .add_plugins(DefaultPlugins)
8 .add_systems(Update, gamepad_system)
9 .run();
10 }
11
12 fn gamepad_system(gamepads: Query<(Entity, &Gamepad)>) {
13 for (entity, gamepad) in &gamepads {
14 if gamepad.just_pressed(GamepadButton::South) {
15 info!("{} just pressed South", entity);
16 } else if gamepad.just_released(GamepadButton::South) {
17 info!("{} just released South", entity);
18 }
19
20 let right_trigger = gamepad.get(GamepadButton::RightTrigger2).unwrap();
21 if right_trigger.abs() > 0.01 {
22 info!("{} RightTrigger2 value is {}", entity, right_trigger);
23 }
24
25 let left_stick_x = gamepad.get(GamepadAxis::LeftStickX).unwrap();
26 if left_stick_x.abs() > 0.01 {
27 info!("{} LeftStickX value is {}", entity, left_stick_x);
28 }
29 }
30 }