02.player_input.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. .. _doc_first_3d_game_player_scene_and_input:
  2. Player scene and input actions
  3. ==============================
  4. In the next two lessons, we will design the player scene, register custom input
  5. actions, and code player movement. By the end, you'll have a playable character
  6. that moves in eight directions.
  7. .. TODO: add player animated gif?
  8. .. player_movement.gif
  9. Create a new scene by going to the Scene menu in the top-left and clicking *New
  10. Scene*.
  11. |image0|
  12. Create a :ref:`CharacterBody3D <class_CharacterBody3D>` node as the root
  13. .. image:: img/02.player_input/add_character_body3D.webp
  14. Name the :ref:`CharacterBody3D <class_CharacterBody3D>` to ``Player``.
  15. Character bodies are complementary to the area and rigid bodies used in the 2D
  16. game tutorial. Like rigid bodies, they can move and collide with the
  17. environment, but instead of being controlled by the physics engine, **you** dictate
  18. their movement. You will see how we use the node's unique features when we code
  19. the jump and squash mechanics.
  20. .. seealso::
  21. To learn more about the different physics node types, see the
  22. :ref:`doc_physics_introduction`.
  23. For now, we're going to create a basic rig for our character's 3D model. This
  24. will allow us to rotate the model later via code while it plays an animation.
  25. Add a :ref:`Node3D <class_Node3D>` node as a child of ``Player`` and name it ``Pivot``
  26. .. image:: img/02.player_input/adding_node3D.webp
  27. Then, in the FileSystem dock, expand the ``art/`` folder
  28. by double-clicking it and drag and
  29. drop ``player.glb`` onto ``Pivot``.
  30. |image1|
  31. This should instantiate the model as a child of ``Pivot``.
  32. You can rename it to ``Character``.
  33. |image2|
  34. .. note::
  35. The ``.glb`` files contain 3D scene data based on the open source glTF 2.0
  36. specification. They're a modern and powerful alternative to a proprietary format
  37. like FBX, which Godot also supports. To produce these files, we designed the
  38. model in `Blender 3D <https://www.blender.org/>`__ and exported it to glTF.
  39. As with all kinds of physics nodes, we need a collision shape for our character
  40. to collide with the environment. Select the ``Player`` node again and add a child node
  41. :ref:`CollisionShape3D <class_CollisionShape3D>`. In the *Inspector*, on the *Shape* property, add a new :ref:`SphereShape3D <class_SphereShape3D>`.
  42. .. image:: img/02.player_input/add_capsuleshape3d.webp
  43. The sphere's wireframe appears below the character.
  44. |image3|
  45. It will be the shape the physics engine uses to collide with the environment, so
  46. we want it to better fit the 3D model. Make it a bit larger by dragging the orange
  47. dot in the viewport. My sphere has a radius of about ``0.8`` meters.
  48. Then, move the collision shape up so its bottom roughly aligns with the grid's plane.
  49. |image4|
  50. To make moving the shape easier, you can toggle the model's visibility by clicking
  51. the eye icon next to the ``Character`` or the ``Pivot`` nodes.
  52. |image5|
  53. Save the scene as ``player.tscn``
  54. With the nodes ready, we can almost get coding. But first, we need to define
  55. some input actions.
  56. .. _doc_first_3d_game_input_actions:
  57. Creating input actions
  58. ----------------------
  59. To move the character, we will listen to the player's input, like pressing the
  60. arrow keys. In Godot, while we could write all the key bindings in code, there's
  61. a powerful system that allows you to assign a label to a set of keys and
  62. buttons. This simplifies our scripts and makes them more readable.
  63. This system is the Input Map. To access its editor, head to the *Project* menu
  64. and select *Project Settings*.
  65. |image6|
  66. At the top, there are multiple tabs. Click on *Input Map*. This window allows
  67. you to add new actions at the top; they are your labels. In the bottom part, you
  68. can bind keys to these actions.
  69. |image7|
  70. Godot projects come with some predefined actions designed for user interface
  71. design (see above screenshot). These will become visible if you enable the
  72. *Show Built-in Actions* toggle. We could use these here, but instead we're
  73. defining our own to support gamepads. Leave *Show Built-in Actions* disabled.
  74. We're going to name our actions ``move_left``, ``move_right``, ``move_forward``,
  75. ``move_back``, and ``jump``.
  76. To add an action, write its name in the bar at the top and press Enter.
  77. |image8|
  78. Create the following five actions:
  79. |image9|
  80. To bind a key or button to an action, click the "+" button to its right. Do this
  81. for ``move_left``. Press the left arrow key and click *OK*.
  82. .. image:: img/02.player_input/left_inputmap.webp
  83. Bind also the :kbd:`A` key, onto the action ``move_left``.
  84. |image12|
  85. Let's now add support for a gamepad's left joystick. Click the "+" button again
  86. but this time, select *Manual Selection -> Joypad Axes*.
  87. .. image:: img/02.player_input/joystick_axis_input.webp
  88. Select the negative X axis of the left joystick.
  89. .. image:: img/02.player_input/left_joystick_select.webp
  90. Leave the other values as default and press *OK*
  91. .. note::
  92. If you want controllers to have different input actions, you should use the Devices option in Additional Options. Device 0 corresponds to the first plugged gamepad, Device 1 corresponds to the second plugged gamepad, and so on.
  93. Do the same for the other input actions. For example, bind the right arrow, D,
  94. and the left joystick's positive axis to ``move_right``. After binding all keys,
  95. your interface should look like this.
  96. |image15|
  97. The final action to set up is the ``jump`` action. Bind the Space key and the gamepad's
  98. A button.
  99. |image16|
  100. Your jump input action should look like this.
  101. |image18|
  102. That's all the actions we need for this game. You can use this menu to label any
  103. groups of keys and buttons in your projects.
  104. In the next part, we'll code and test the player's movement.
  105. .. |image0| image:: img/02.player_input/01.new_scene.png
  106. .. |image1| image:: img/02.player_input/02.instantiating_the_model.webp
  107. .. |image2| image:: img/02.player_input/03.scene_structure.png
  108. .. |image3| image:: img/02.player_input/04.sphere_shape.png
  109. .. |image4| image:: img/02.player_input/05.moving_the_sphere_up.png
  110. .. |image5| image:: img/02.player_input/06.toggling_visibility.webp
  111. .. |image6| image:: img/02.player_input/07.project_settings.png
  112. .. |image7| image:: img/02.player_input/07.input_map_tab.png
  113. .. |image8| image:: img/02.player_input/07.adding_action.webp
  114. .. |image9| image:: img/02.player_input/08.actions_list_empty.png
  115. .. |image11| image:: img/02.player_input/09.keyboard_key_popup.png
  116. .. |image12| image:: img/02.player_input/09.keyboard_keys.png
  117. .. |image15| image:: img/02.player_input/12.move_inputs_mapped.webp
  118. .. |image16| image:: img/02.player_input/13.joy_button_option.webp
  119. .. |image17| image:: img/02.player_input/14.add_jump_button.png
  120. .. |image18| image:: img/02.player_input/14.jump_input_action.webp