controllers_gamepads_joysticks.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. .. _doc_controllers_gamepads_joysticks:
  2. Controllers, gamepads, and joysticks
  3. ====================================
  4. Godot supports hundreds of controller models thanks to the community-sourced
  5. `SDL game controller database <https://github.com/gabomdq/SDL_GameControllerDB>`__.
  6. Controllers are supported on Windows, macOS, Linux, Android, iOS, and HTML5.
  7. Note that more specialized devices such as steering wheels, rudder pedals and
  8. `HOTAS <https://en.wikipedia.org/wiki/HOTAS>`__ are less tested and may not
  9. always work as expected. Overriding force feedback for those devices is also not
  10. implemented yet. If you have access to one of those devices, don't hesitate to
  11. `report bugs on GitHub
  12. <https://github.com/godotengine/godot/blob/master/CONTRIBUTING.md#reporting-bugs>`__.
  13. In this guide, you will learn:
  14. - **How to write your input logic to support both keyboard and controller inputs.**
  15. - **How controllers can behave differently from keyboard/mouse input.**
  16. - **Troubleshooting issues with controllers in Godot.**
  17. Supporting universal input
  18. --------------------------
  19. Thanks to Godot's input action system, Godot makes it possible to support both
  20. keyboard and controller input without having to write separate code paths.
  21. Instead of hardcoding keys or controller buttons in your scripts, you should
  22. create *input actions* in the Project Settings which will then refer to
  23. specified key and controller inputs.
  24. Input actions are explained in detail on the :ref:`doc_inputevent` page.
  25. .. note::
  26. Unlike keyboard input, supporting both mouse and controller input for an
  27. action (such as looking around in a first-person game) will require
  28. different code paths since these have to be handled separately.
  29. Which Input singleton method should I use?
  30. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  31. There are 3 ways to get input in an analog-aware way:
  32. - When you have two axes (such as joystick or WASD movement) and want both
  33. axes to behave as a single input, use ``Input.get_vector()``:
  34. .. tabs::
  35. .. code-tab:: gdscript GDScript
  36. # `velocity` will be a Vector2 between `Vector2(-1.0, -1.0)` and `Vector2(1.0, 1.0)`.
  37. # This handles deadzone in a correct way for most use cases.
  38. # The resulting deadzone will have a circular shape as it generally should.
  39. var velocity = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
  40. # The line below is similar to `get_vector()`, except that it handles
  41. # the deadzone in a less optimal way. The resulting deadzone will have
  42. # a square-ish shape when it should ideally have a circular shape.
  43. var velocity = Vector2(Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
  44. Input.get_action_strength("move_back") - Input.get_action_strength("move_forward")).clamped(1)
  45. .. code-tab:: csharp
  46. // `velocity` will be a Vector2 between `Vector2(-1.0, -1.0)` and `Vector2(1.0, 1.0)`.
  47. // This handles deadzone in a correct way for most use cases.
  48. // The resulting deadzone will have a circular shape as it generally should.
  49. Vector2 velocity = Input.GetVector("move_left", "move_right", "move_forward", "move_back");
  50. // The line below is similar to `get_vector()`, except that it handles
  51. // the deadzone in a less optimal way. The resulting deadzone will have
  52. // a square-ish shape when it should ideally have a circular shape.
  53. Vector2 velocity = new Vector2(Input.GetActionStrength("move_right") - Input.GetActionStrength("move_left"),
  54. Input.GetActionStrength("move_back") - Input.GetActionStrength("move_forward")).Clamped(1);
  55. - When you have one axis that can go both ways (such as a throttle on a
  56. flight stick), or when you want to handle separate axes individually,
  57. use ``Input.get_axis()``:
  58. .. tabs::
  59. .. code-tab:: gdscript GDScript
  60. # `walk` will be a floating-point number between `-1.0` and `1.0`.
  61. var walk = Input.get_axis("move_left", "move_right")
  62. # The line above is a shorter form of:
  63. var walk = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
  64. .. code-tab:: csharp
  65. // `walk` will be a floating-point number between `-1.0` and `1.0`.
  66. float walk = Input.GetAxis("move_left", "move_right");
  67. // The line above is a shorter form of:
  68. float walk = Input.GetActionStrength("move_right") - Input.GetActionStrength("move_left");
  69. - For other types of analog input, such as handling a trigger or handling
  70. one direction at a time, use ``Input.get_action_strength()``:
  71. .. tabs::
  72. .. code-tab:: gdscript GDScript
  73. # `strength` will be a floating-point number between `0.0` and `1.0`.
  74. var strength = Input.get_action_strength("accelerate")
  75. .. code-tab:: csharp
  76. // `strength` will be a floating-point number between `0.0` and `1.0`.
  77. float strength = Input.GetActionStrength("accelerate");
  78. For non-analog digital/boolean input (only "pressed" or "not pressed" values),
  79. such as controller buttons, mouse buttons or keyboard keys,
  80. use ``Input.is_action_pressed()``:
  81. .. tabs::
  82. .. code-tab:: gdscript GDScript
  83. # `jumping` will be a boolean with a value of `true` or `false`.
  84. var jumping = Input.is_action_pressed("jump")
  85. .. tabs::
  86. .. code-tab:: csharp
  87. // `jumping` will be a boolean with a value of `true` or `false`.
  88. bool jumping = Input.IsActionPressed("jump");
  89. In Godot versions before 3.4, such as 3.3, ``Input.get_vector()`` and
  90. ``Input.get_axis()`` aren't available. Only ``Input.get_action_strength()``
  91. and ``Input.is_action_pressed()`` are available in Godot 3.3.
  92. Differences between keyboard/mouse and controller input
  93. -------------------------------------------------------
  94. If you're used to handling keyboard and mouse input, you may be surprised by how
  95. controllers handle specific situations.
  96. Dead zone
  97. ^^^^^^^^^
  98. Unlike keyboards and mice, controllers offer axes with *analog* inputs. The
  99. upside of analog inputs is that they offer additional flexibility for actions.
  100. Unlike digital inputs which can only provide strengths of ``0.0`` and ``1.0``,
  101. an analog input can provide *any* strength between ``0.0`` and ``1.0``. The
  102. downside is that without a deadzone system, an analog axis' strength will never
  103. be equal to ``0.0`` due to how the controller is physically built. Instead, it
  104. will linger at a low value such as ``0.062``. This phenomenon is known as
  105. *drifting* and can be more noticeable on old or faulty controllers.
  106. Let's take a racing game as a real-world example. Thanks to analog inputs, we
  107. can steer the car slowly in one direction or another. However, without a
  108. deadzone system, the car would slowly steer by itself even if the player isn't
  109. touching the joystick. This is because the directional axis strength won't be
  110. equal to ``0.0`` when we expect it to. Since we don't want our car to steer by
  111. itself in this case, we define a "dead zone" value of ``0.2`` which will ignore
  112. all input whose strength is lower than ``0.2``. An ideal dead zone value is high
  113. enough to ignore the input caused by joystick drifting, but is low enough to not
  114. ignore actual input from the player.
  115. Godot features a built-in dead zone system to tackle this problem. The default
  116. value is ``0.2``, but you can increase it or decrease it on a per-action basis
  117. in the Project Settings' Input Map tab.
  118. For ``Input.get_vector()``, the deadzone can be specified, or otherwise it
  119. will calculate the average deadzone value from all of the actions in the vector.
  120. "Echo" events
  121. ^^^^^^^^^^^^^
  122. Unlike keyboard input, holding down a controller button such as a D-pad
  123. direction will **not** generate repeated input events at fixed intervals (also
  124. known as "echo" events). This is because the operating system never sends "echo"
  125. events for controller input in the first place.
  126. If you want controller buttons to send echo events, you will have to generate
  127. :ref:`class_InputEvent` objects by code and parse them using
  128. :ref:`Input.parse_input_event() <class_Input_method_parse_input_event>`
  129. at regular intervals. This can be accomplished
  130. with the help of a :ref:`class_Timer` node.
  131. Troubleshooting
  132. ---------------
  133. .. seealso::
  134. You can view a list of
  135. `known issues with controller support <https://github.com/godotengine/godot/issues?q=is%3Aopen+is%3Aissue+label%3Atopic%3Ainput+gamepad>`__
  136. on GitHub.
  137. My controller isn't recognized by Godot.
  138. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  139. First, check that your controller is recognized by other applications. You can
  140. use the `Gamepad Tester <https://gamepad-tester.com/>`__ website to confirm that
  141. your controller is recognized.
  142. My controller has incorrectly mapped buttons or axes.
  143. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  144. If buttons are incorrectly mapped, this may be due to an erroneous mapping from
  145. the `SDL game controller database <https://github.com/gabomdq/SDL_GameControllerDB>`__.
  146. You can contribute an updated mapping to be included in the next Godot version
  147. by opening a pull request on the linked repository.
  148. There are many ways to create mappings. One option is to use the mapping wizard
  149. in the `official Joypads demo <https://godotengine.org/asset-library/asset/140>`__.
  150. Once you have a working mapping for your controller, you can test it by defining
  151. the ``SDL_GAMECONTROLLERCONFIG`` environment variable before running Godot:
  152. .. tabs::
  153. .. code-tab:: bash Linux/macOS
  154. export SDL_GAMECONTROLLERCONFIG="your:mapping:here"
  155. ./path/to/godot.x86_64
  156. .. code-tab:: bat Windows (cmd)
  157. set SDL_GAMECONTROLLERCONFIG=your:mapping:here
  158. path\to\godot.exe
  159. .. code-tab:: powershell Windows (powershell)
  160. $env:SDL_GAMECONTROLLERCONFIG="your:mapping:here"
  161. path\to\godot.exe
  162. To test mappings on non-desktop platforms or to distribute your project with
  163. additional controller mappings, you can add them by calling
  164. :ref:`Input.add_joy_mapping() <class_Input_method_add_joy_mapping>`
  165. as early as possible in a script's ``_ready()`` function.
  166. My controller works on a given platform, but not on another platform.
  167. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  168. Linux
  169. ~~~~~
  170. Prior to Godot 3.3, official Godot binaries were compiled with udev support
  171. but self-compiled binaries were compiled *without* udev support unless
  172. ``udev=yes`` was passed on the SCons command line. This made controller
  173. hotplugging support unavailable in self-compiled binaries.
  174. HTML5
  175. ~~~~~
  176. HTML5 controller support is often less reliable compared to "native" platforms.
  177. The quality of controller support tends to vary wildly across browsers. As a
  178. result, you may have to instruct your players to use a different browser if they
  179. can't get their controller to work.
  180. Also, note that
  181. `controller support was significantly improved <https://github.com/godotengine/godot/pull/45078>`__
  182. in Godot 3.3 and later.