02.player_input.rst 6.4 KB

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