controllers_gamepads_joysticks.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. .. _doc_controllers_gamepads_joysticks:
  2. Controllers, gamepads, and joysticks
  3. ====================================
  4. Godot supports hundreds of controller models out of the box.
  5. Controllers are supported on Windows, macOS, Linux, Android, iOS, and Web.
  6. .. note::
  7. Since Godot 4.5, the engine relies on `SDL 3 <https://www.libsdl.org/index.php>`__
  8. for controller support on Windows, macOS, and Linux. This means the list of
  9. supported controllers and their behavior should closely match what is available
  10. in other games and engines using SDL 3. Note that SDL is only used for input,
  11. not for windowing or sound.
  12. Prior to Godot 4.5, the engine used its own controller support code.
  13. This can cause certain controllers to behave incorrectly.
  14. This custom code is still used to support controllers on Android, iOS,
  15. and Web, so it may result in issues appearing only on those platforms.
  16. Note that more specialized devices such as steering wheels, rudder pedals and
  17. `HOTAS <https://en.wikipedia.org/wiki/HOTAS>`__ are less tested and may not
  18. always work as expected. Overriding force feedback for those devices is also not
  19. implemented yet. If you have access to one of those devices, don't hesitate to
  20. `report bugs on GitHub
  21. <https://github.com/godotengine/godot/blob/master/CONTRIBUTING.md#reporting-bugs>`__.
  22. In this guide, you will learn:
  23. - **How to write your input logic to support both keyboard and controller inputs.**
  24. - **How controllers can behave differently from keyboard/mouse input.**
  25. - **Troubleshooting issues with controllers in Godot.**
  26. Supporting universal input
  27. --------------------------
  28. Thanks to Godot's input action system, Godot makes it possible to support both
  29. keyboard and controller input without having to write separate code paths.
  30. Instead of hardcoding keys or controller buttons in your scripts, you should
  31. create *input actions* in the Project Settings which will then refer to
  32. specified key and controller inputs.
  33. Input actions are explained in detail on the :ref:`doc_inputevent` page.
  34. .. note::
  35. Unlike keyboard input, supporting both mouse and controller input for an
  36. action (such as looking around in a first-person game) will require
  37. different code paths since these have to be handled separately.
  38. Which Input singleton method should I use?
  39. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  40. There are 3 ways to get input in an analog-aware way:
  41. - When you have two axes (such as joystick or WASD movement) and want both
  42. axes to behave as a single input, use ``Input.get_vector()``:
  43. .. tabs::
  44. .. code-tab:: gdscript GDScript
  45. # `velocity` will be a Vector2 between `Vector2(-1.0, -1.0)` and `Vector2(1.0, 1.0)`.
  46. # This handles deadzone in a correct way for most use cases.
  47. # The resulting deadzone will have a circular shape as it generally should.
  48. var velocity = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
  49. # The line below is similar to `get_vector()`, except that it handles
  50. # the deadzone in a less optimal way. The resulting deadzone will have
  51. # a square-ish shape when it should ideally have a circular shape.
  52. var velocity = Vector2(
  53. Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
  54. Input.get_action_strength("move_back") - Input.get_action_strength("move_forward")
  55. ).limit_length(1.0)
  56. .. code-tab:: csharp
  57. // `velocity` will be a Vector2 between `Vector2(-1.0, -1.0)` and `Vector2(1.0, 1.0)`.
  58. // This handles deadzone in a correct way for most use cases.
  59. // The resulting deadzone will have a circular shape as it generally should.
  60. Vector2 velocity = Input.GetVector("move_left", "move_right", "move_forward", "move_back");
  61. // The line below is similar to `get_vector()`, except that it handles
  62. // the deadzone in a less optimal way. The resulting deadzone will have
  63. // a square-ish shape when it should ideally have a circular shape.
  64. Vector2 velocity = new Vector2(
  65. Input.GetActionStrength("move_right") - Input.GetActionStrength("move_left"),
  66. Input.GetActionStrength("move_back") - Input.GetActionStrength("move_forward")
  67. ).LimitLength(1.0);
  68. - When you have one axis that can go both ways (such as a throttle on a
  69. flight stick), or when you want to handle separate axes individually,
  70. use ``Input.get_axis()``:
  71. .. tabs::
  72. .. code-tab:: gdscript GDScript
  73. # `walk` will be a floating-point number between `-1.0` and `1.0`.
  74. var walk = Input.get_axis("move_left", "move_right")
  75. # The line above is a shorter form of:
  76. var walk = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
  77. .. code-tab:: csharp
  78. // `walk` will be a floating-point number between `-1.0` and `1.0`.
  79. float walk = Input.GetAxis("move_left", "move_right");
  80. // The line above is a shorter form of:
  81. float walk = Input.GetActionStrength("move_right") - Input.GetActionStrength("move_left");
  82. - For other types of analog input, such as handling a trigger or handling
  83. one direction at a time, use ``Input.get_action_strength()``:
  84. .. tabs::
  85. .. code-tab:: gdscript GDScript
  86. # `strength` will be a floating-point number between `0.0` and `1.0`.
  87. var strength = Input.get_action_strength("accelerate")
  88. .. code-tab:: csharp
  89. // `strength` will be a floating-point number between `0.0` and `1.0`.
  90. float strength = Input.GetActionStrength("accelerate");
  91. For non-analog digital/boolean input (only "pressed" or "not pressed" values),
  92. such as controller buttons, mouse buttons or keyboard keys,
  93. use ``Input.is_action_pressed()``:
  94. .. tabs::
  95. .. code-tab:: gdscript GDScript
  96. # `jumping` will be a boolean with a value of `true` or `false`.
  97. var jumping = Input.is_action_pressed("jump")
  98. .. code-tab:: csharp
  99. // `jumping` will be a boolean with a value of `true` or `false`.
  100. bool jumping = Input.IsActionPressed("jump");
  101. .. note::
  102. If you need to know whether an input was *just* pressed in the previous
  103. frame, use ``Input.is_action_just_pressed()`` instead of
  104. ``Input.is_action_pressed()``. Unlike ``Input.is_action_pressed()`` which
  105. returns ``true`` as long as the input is
  106. held, ``Input.is_action_just_pressed()`` will only return ``true`` for one
  107. frame after the button has been pressed.
  108. Vibration
  109. ---------
  110. Vibration (also called *haptic feedback*) can be used to enhance the feel of a
  111. game. For instance, in a racing game, you can convey the surface the car is
  112. currently driving on through vibration, or create a sudden vibration on a crash.
  113. Use the Input singleton's
  114. :ref:`start_joy_vibration<class_Input_method_start_joy_vibration>` method to
  115. start vibrating a gamepad. Use
  116. :ref:`stop_joy_vibration<class_Input_method_stop_joy_vibration>` to stop
  117. vibration early (useful if no duration was specified when starting).
  118. On mobile devices, you can also use
  119. :ref:`vibrate_handheld<class_Input_method_vibrate_handheld>` to vibrate the
  120. device itself (independently from the gamepad). On Android, this requires the
  121. ``VIBRATE`` permission to be enabled in the Android export preset before
  122. exporting the project.
  123. .. note::
  124. Vibration can be uncomfortable for certain players. Make sure to provide an
  125. in-game slider to disable vibration or reduce its intensity.
  126. Differences between keyboard/mouse and controller input
  127. -------------------------------------------------------
  128. If you're used to handling keyboard and mouse input, you may be surprised by how
  129. controllers handle specific situations.
  130. Dead zone
  131. ~~~~~~~~~
  132. Unlike keyboards and mice, controllers offer axes with *analog* inputs. The
  133. upside of analog inputs is that they offer additional flexibility for actions.
  134. Unlike digital inputs which can only provide strengths of ``0.0`` and ``1.0``,
  135. an analog input can provide *any* strength between ``0.0`` and ``1.0``. The
  136. downside is that without a deadzone system, an analog axis' strength will never
  137. be equal to ``0.0`` due to how the controller is physically built. Instead, it
  138. will linger at a low value such as ``0.062``. This phenomenon is known as
  139. *drifting* and can be more noticeable on old or faulty controllers.
  140. Let's take a racing game as a real-world example. Thanks to analog inputs, we
  141. can steer the car slowly in one direction or another. However, without a
  142. deadzone system, the car would slowly steer by itself even if the player isn't
  143. touching the joystick. This is because the directional axis strength won't be
  144. equal to ``0.0`` when we expect it to. Since we don't want our car to steer by
  145. itself in this case, we define a "dead zone" value of ``0.2`` which will ignore
  146. all input whose strength is lower than ``0.2``. An ideal dead zone value is high
  147. enough to ignore the input caused by joystick drifting, but is low enough to not
  148. ignore actual input from the player.
  149. Godot features a built-in deadzone system to tackle this problem. The default
  150. value is ``0.5``, but you can adjust it on a per-action basis in the Project
  151. Settings' Input Map tab. For ``Input.get_vector()``, the deadzone can be
  152. specified as an optional 5th parameter. If not specified, it will calculate the
  153. average deadzone value from all of the actions in the vector.
  154. "Echo" events
  155. ~~~~~~~~~~~~~
  156. Unlike keyboard input, holding down a controller button such as a D-pad
  157. direction will **not** generate repeated input events at fixed intervals (also
  158. known as "echo" events). This is because the operating system never sends "echo"
  159. events for controller input in the first place.
  160. If you want controller buttons to send echo events, you will have to generate
  161. :ref:`class_InputEvent` objects by code and parse them using
  162. :ref:`Input.parse_input_event() <class_Input_method_parse_input_event>`
  163. at regular intervals. This can be accomplished
  164. with the help of a :ref:`class_Timer` node.
  165. Window focus
  166. ~~~~~~~~~~~~
  167. Unlike keyboard input, controller inputs can be seen by **all** windows on the
  168. operating system, including unfocused windows.
  169. While this is useful for
  170. `third-party split screen functionality <https://nucleus-coop.github.io/>`__,
  171. it can also have adverse effects. Players may accidentally send controller inputs
  172. to the running project while interacting with another window.
  173. If you wish to ignore events when the project window isn't focused, you will
  174. need to create an :ref:`autoload <doc_singletons_autoload>` called ``Focus``
  175. with the following script and use it to check all your inputs:
  176. ::
  177. # Focus.gd
  178. extends Node
  179. var focused := true
  180. func _notification(what: int) -> void:
  181. match what:
  182. NOTIFICATION_APPLICATION_FOCUS_OUT:
  183. focused = false
  184. NOTIFICATION_APPLICATION_FOCUS_IN:
  185. focused = true
  186. func input_is_action_pressed(action: StringName) -> bool:
  187. if focused:
  188. return Input.is_action_pressed(action)
  189. return false
  190. func event_is_action_pressed(event: InputEvent, action: StringName) -> bool:
  191. if focused:
  192. return event.is_action_pressed(action)
  193. return false
  194. Then, instead of using ``Input.is_action_pressed(action)``, use
  195. ``Focus.input_is_action_pressed(action)`` where ``action`` is the name of
  196. the input action. Also, instead of using ``event.is_action_pressed(action)``,
  197. use ``Focus.event_is_action_pressed(event, action)`` where ``event`` is an
  198. InputEvent reference and ``action`` is the name of the input action.
  199. Power saving prevention
  200. ~~~~~~~~~~~~~~~~~~~~~~~
  201. Unlike keyboard and mouse input, controller inputs do **not** inhibit sleep and
  202. power saving measures (such as turning off the screen after a certain amount of
  203. time has passed).
  204. To combat this, Godot enables power saving prevention by default when a project
  205. is running. If you notice the system is turning off its display when playing
  206. with a gamepad, check the value of **Display > Window > Energy Saving > Keep Screen On**
  207. in the Project Settings.
  208. On Linux, power saving prevention requires the engine to be able to use D-Bus.
  209. Check whether D-Bus is installed and reachable if running the project within a
  210. Flatpak, as sandboxing restrictions may make this impossible by default.
  211. Troubleshooting
  212. ---------------
  213. .. seealso::
  214. You can view a list of
  215. `known issues with controller support <https://github.com/godotengine/godot/issues?q=is%3Aopen+is%3Aissue+label%3Atopic%3Ainput+gamepad>`__
  216. on GitHub.
  217. My controller isn't recognized by Godot.
  218. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  219. First, check that your controller is recognized by other applications. You can
  220. use the `Gamepad Tester <https://hardwaretester.com/gamepad>`__ website to confirm
  221. that your controller is recognized.
  222. On Windows Godot only supports up to 4 controllers at a time. This is
  223. because Godot uses the XInput API, which is limited to supporting 4 controllers
  224. at once. Additional controllers above this limit are ignored by Godot.
  225. My controller has incorrectly mapped buttons or axes.
  226. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  227. First, if your controller provides some kind of firmware update utility,
  228. make sure to run it to get the latest fixes from the manufacturer. For instance,
  229. Xbox One and Xbox Series controllers can have their firmware updated using the
  230. `Xbox Accessories app <https://www.microsoft.com/en-us/p/xbox-accessories/9nblggh30xj3>`__.
  231. (This application only runs on Windows, so you have to use a Windows machine
  232. or a Windows virtual machine with USB support to update the controller's firmware.)
  233. After updating the controller's firmware, unpair the controller and pair it again
  234. with your PC if you are using the controller in wireless mode.
  235. If buttons are incorrectly mapped, this may be due to an erroneous mapping from
  236. the SDL game controller database used by Godot or the
  237. `Godot game controller database <https://github.com/godotengine/godot/blob/master/core/input/godotcontrollerdb.txt>`__.
  238. In this case, you will need to create a custom mapping for your controller.
  239. There are many ways to create mappings. One option is to use the mapping wizard
  240. in the `official Joypads demo <https://godotengine.org/asset-library/asset/2785>`__.
  241. Once you have a working mapping for your controller, you can test it by defining
  242. the ``SDL_GAMECONTROLLERCONFIG`` environment variable before running Godot:
  243. .. tabs::
  244. .. code-tab:: bash Linux/macOS
  245. export SDL_GAMECONTROLLERCONFIG="your:mapping:here"
  246. ./path/to/godot.x86_64
  247. .. code-tab:: bat Windows (cmd)
  248. set SDL_GAMECONTROLLERCONFIG=your:mapping:here
  249. path\to\godot.exe
  250. .. code-tab:: powershell Windows (PowerShell)
  251. $env:SDL_GAMECONTROLLERCONFIG="your:mapping:here"
  252. path\to\godot.exe
  253. To test mappings on non-desktop platforms or to distribute your project with
  254. additional controller mappings, you can add them by calling
  255. :ref:`Input.add_joy_mapping() <class_Input_method_add_joy_mapping>`
  256. as early as possible in a script's ``_ready()`` function.
  257. Once you are satisfied with the custom mapping, you can contribute it for
  258. the next Godot version by opening a pull request on the
  259. `Godot game controller database <https://github.com/godotengine/godot/blob/master/core/input/godotcontrollerdb.txt>`__.
  260. My controller works on a given platform, but not on another platform.
  261. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  262. Linux
  263. ^^^^^
  264. If you're using a self-compiled engine binary, make sure it was compiled with
  265. udev support. This is enabled by default, but it is possible to disable udev
  266. support by specifying ``udev=no`` on the SCons command line. If you're using an
  267. engine binary supplied by a Linux distribution, double-check whether it was
  268. compiled with udev support.
  269. Controllers can still work without udev support, but it is less reliable as
  270. regular polling must be used to check for controllers being connected or
  271. disconnected during gameplay (hotplugging).
  272. Android/iOS
  273. ^^^^^^^^^^^
  274. As described at the top of the page, controller support on mobile platforms relies
  275. on a custom implementation instead of using SDL for input. This means controller
  276. support may be less reliable than on desktop platforms.
  277. Support for SDL-based controller input on mobile platforms is
  278. `planned <https://github.com/godotengine/godot/pull/109645>`__
  279. in a future release.
  280. Web
  281. ^^^
  282. Web controller support is often less reliable compared to "native" platforms.
  283. The quality of controller support tends to vary wildly across browsers. As a
  284. result, you may have to instruct your players to use a different browser if they
  285. can't get their controller to work.
  286. Like for mobile platforms, support for SDL-based controller input on the web platform
  287. is `planned <https://github.com/godotengine/godot/pull/109645>`__ in a future release.