gamepad_control.gd 538 B

12345678910111213141516171819202122
  1. class_name GamepadControl
  2. extends Node
  3. # Command from keyboard input
  4. signal dir_changed(dir: Vector2)
  5. var _last_dir = Vector2.ZERO
  6. func _unhandled_input(event: InputEvent) -> void:
  7. if event is InputEventJoypadMotion:
  8. var joyEvent := event as InputEventJoypadMotion
  9. if joyEvent.get_axis() == JOY_AXIS_LEFT_X:
  10. _last_dir.x = joyEvent.get_axis_value()
  11. dir_changed.emit(_last_dir)
  12. elif joyEvent.get_axis() == JOY_AXIS_TRIGGER_RIGHT:
  13. _last_dir.y = joyEvent.get_axis_value()
  14. else:
  15. return
  16. dir_changed.emit(_last_dir)