02.player_input.rst 6.4 KB

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