01.game_setup.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. .. _doc_first_3d_game_game_area:
  2. Setting up the game area
  3. ========================
  4. In this first part, we're going to set up the game area. Let's get started by
  5. importing the start assets and setting up the game scene.
  6. We've prepared a Godot project with the 3D models and sounds we'll use for this
  7. tutorial, linked in the index page. If you haven't done so yet, you can download
  8. the archive here: `Squash the Creeps assets
  9. <https://github.com/godotengine/godot-3d-dodge-the-creeps/releases/tag/1.1.0>`__.
  10. Once you downloaded it, extract the .zip archive on your computer. Open the
  11. Godot Project Manager and click the *Import* button.
  12. .. image:: img/01.game_setup/01.import_button.webp
  13. In the import popup, enter the full path to the freshly created directory
  14. ``squash_the_creeps_start/``. You can click the *Browse* button on the right to
  15. open a file browser and navigate to the ``project.godot`` file the folder
  16. contains.
  17. .. image:: img/01.game_setup/02.browse_to_project_folder.webp
  18. Click *Import & Edit* to open the project in the editor.
  19. .. image:: img/01.game_setup/03.import_and_edit.webp
  20. A window notifying you that the project was generated by an older Godot version may appear.
  21. Click *Convert Full Project* to convert the project to your current Godot version.
  22. .. image:: img/01.game_setup/import_project_to_4.x_prompt.webp
  23. The start project contains an icon and two folders: ``art/`` and ``fonts/``.
  24. There, you will find the art assets and music we'll use in the game.
  25. .. image:: img/01.game_setup/04.start_assets.png
  26. There are two 3D models, ``player.glb`` and ``mob.glb``, some materials that
  27. belong to these models, and a music track.
  28. Setting up the playable area
  29. ----------------------------
  30. We're going to create our main scene with a plain :ref:`Node <class_Node>` as its root. In the
  31. *Scene* dock, click the *Add Child Node* button represented by a "+" icon in the
  32. top-left and double-click on *Node*. Name the node ``Main``. An alternate method to rename the node is to right-click on *Node* and choose *Rename* (or :kbd:`F2`). Alternatively, to add
  33. a node to the scene, you can press :kbd:`Ctrl + A` (:kbd:`Cmd + A` on macOS).
  34. .. image:: img/01.game_setup/05.main_node.png
  35. Save the scene as ``main.tscn`` by pressing :kbd:`Ctrl + S` (:kbd:`Cmd + S` on macOS).
  36. We'll start by adding a floor that'll prevent the characters from falling. To
  37. create static colliders like the floor, walls, or ceilings, you can use :ref:`StaticBody3D <class_StaticBody3D>` nodes. They require :ref:`CollisionShape3D <class_CollisionShape3D>` child nodes to
  38. define the collision area. With the ``Main`` node selected, add a :ref:`StaticBody3D <class_StaticBody3D>`
  39. node, then a :ref:`CollisionShape3D <class_CollisionShape3D>`. Rename the :ref:`StaticBody3D <class_StaticBody3D>` to ``Ground``.
  40. .. image:: img/01.game_setup/adding_static_body3D.webp
  41. Your scene tree should look like this
  42. .. image:: img/01.game_setup/06.staticbody_node.png
  43. A warning sign next to the :ref:`CollisionShape3D <class_CollisionShape3D>` appears because we haven't defined
  44. its shape. If you click the icon, a popup appears to give you more information.
  45. .. image:: img/01.game_setup/07.collision_shape_warning.png
  46. To create a shape, select the :ref:`CollisionShape3D <class_CollisionShape3D>` node, head to the *Inspector*
  47. and click the *<empty>* field next to the *Shape* property. Create a new *BoxShape3D*.
  48. .. image:: img/01.game_setup/08.create_box_shape3D.jpg
  49. The box shape is perfect for flat ground and walls. Its thickness makes it
  50. reliable to block even fast-moving objects.
  51. A box's wireframe appears in the viewport with three orange dots. You can click
  52. and drag these to edit the shape's extents interactively. We can also precisely
  53. set the size in the inspector. Click on the :ref:`BoxShape3D <class_BoxShape3D>` to expand the resource.
  54. Set its *Size* to ``60`` on the X-axis, ``2`` for the Y-axis, and ``60`` for
  55. the Z-axis.
  56. .. image:: img/01.game_setup/09.box_size.webp
  57. Collision shapes are invisible. We need to add a visual floor that goes along
  58. with it. Select the ``Ground`` node and add a :ref:`MeshInstance3D <class_MeshInstance3D>` as its child.
  59. .. image:: img/01.game_setup/10.mesh_instance3d.png
  60. In the *Inspector*, click on the field next to *Mesh* and create a :ref:`BoxMesh <class_BoxMesh>`
  61. resource to create a visible box.
  62. .. image:: img/01.game_setup/11.box_mesh.webp
  63. Once again, it's too small by default. Click the box icon to expand the
  64. resource and set its *Size* to ``60``, ``2``, and ``60``.
  65. .. image:: img/01.game_setup/12.cube_resized.png
  66. You should see a wide grey slab that covers the grid and blue and red axes in
  67. the viewport.
  68. We're going to move the ground down so we can see the floor grid. To do this, the grid snapping feature can be used.
  69. Grid snapping can be activated 2 ways in the 3D editor.
  70. The first is by pressing the *Use Snap* button (or pressing the :kbd:`Y` key).
  71. The second is by selecting a node, dragging a handle on the gizmo **then** holding :kbd:`Ctrl` while still clicking to enable snapping as long as :kbd:`Ctrl` is held.
  72. .. image:: img/01.game_setup/use_snap.webp
  73. Start by setting snapping with your preferred method. Then move the ``Ground`` node using the Y-axis (the green arrow on the gizmo).
  74. .. image:: img/01.game_setup/move_gizmo_y_axis.webp
  75. .. note::
  76. If you can't see the 3D object manipulator like on the image above, ensure
  77. the *Select Mode* is active in the toolbar above the view.
  78. .. image:: img/01.game_setup/14.select_mode_icon.png
  79. Move the ground down ``1`` meter, in order to have a visible editor grid. A label in the bottom-left corner of the
  80. viewport tells you how much you're translating the node.
  81. .. image:: img/01.game_setup/15.translation_amount.png
  82. .. note::
  83. Moving the *Ground* node down moves both children along with it.
  84. Ensure you move the *Ground* node, **not** the *MeshInstance3D* or the
  85. *CollisionShape3D*.
  86. Ultimately, ``Ground``'s transform.position.y should be -1
  87. .. image:: img/01.game_setup/ground_down1meter.webp
  88. Let's add a directional light so our scene isn't all grey. Select the ``Main``
  89. node and add a child node :ref:`DirectionalLight3D <class_DirectionalLight3D>`.
  90. .. image:: img/01.game_setup/create_directional_light3d.webp
  91. We need to move and rotate the :ref:`DirectionalLight3D <class_DirectionalLight3D>` node.
  92. Move it up by clicking and dragging on the manipulator's green arrow
  93. and click and drag on the red arc to rotate it around the X-axis, until the
  94. ground is lit.
  95. In the *Inspector*, turn on *Shadow -> Enabled* by clicking the checkbox.
  96. .. image:: img/01.game_setup/16.turn_on_shadows.webp
  97. At this point, your project should look like this.
  98. .. image:: img/01.game_setup/17.project_with_light.webp
  99. That's our starting point. In the next part, we will work on the player scene
  100. and base movement.