01.game_setup.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/GDQuest/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. |image1|
  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. |image2|
  18. Click *Import & Edit* to open the project in the editor.
  19. |image3|
  20. The start project contains an icon and two folders: ``art/`` and ``fonts/``.
  21. There, you will find the art assets and music we'll use in the game.
  22. |image4|
  23. There are two 3D models, ``player.glb`` and ``mob.glb``, some materials that
  24. belong to these models, and a music track.
  25. Setting up the playable area
  26. ----------------------------
  27. We're going to create our main scene with a plain *Node* as its root. In the
  28. *Scene* dock, click the *Add Node* button represented by a "+" icon in the
  29. top-left and double-click on *Node*. Name the node "Main". Alternatively, to add
  30. a node to the scene, you can press :kbd:`Ctrl + a` (or :kbd:`Cmd + a` on macOS).
  31. |image5|
  32. Save the scene as ``Main.tscn`` by pressing :kbd:`Ctrl + s` (:kbd:`Cmd + s` on macOS).
  33. We'll start by adding a floor that'll prevent the characters from falling. To
  34. create static colliders like the floor, walls, or ceilings, you can use
  35. *StaticBody* nodes. They require *CollisionShape* child nodes to
  36. define the collision area. With the *Main* node selected, add a *StaticBody*
  37. node, then a *CollisionShape*. Rename the *StaticBody* as *Ground*.
  38. |image6|
  39. A warning sign next to the *CollisionShape* appears because we haven't defined
  40. its shape. If you click the icon, a popup appears to give you more information.
  41. |image7|
  42. To create a shape, with the *CollisionShape* selected, head to the *Inspector*
  43. and click the *[empty]* field next to the *Shape* property. Create a new *Box
  44. Shape*.
  45. |image8|
  46. The box shape is perfect for flat ground and walls. Its thickness makes it
  47. reliable to block even fast-moving objects.
  48. A box's wireframe appears in the viewport with three orange dots. You can click
  49. and drag these to edit the shape's extents interactively. We can also precisely
  50. set the size in the inspector. Click on the *BoxShape* to expand the resource.
  51. Set its *Extents* to ``30`` on the X axis, ``1`` for the Y axis, and ``30`` for
  52. the Z axis.
  53. |image9|
  54. .. note::
  55. In 3D, translation and size units are in meters. The box's total size is
  56. twice its extents: ``60`` by ``60`` meters on the ground plane and ``2``
  57. units tall. The ground plane is defined by the X and Z axes, while the Y
  58. axis represents the height.
  59. Collision shapes are invisible. We need to add a visual floor that goes along
  60. with it. Select the *Ground* node and add a *MeshInstance* as its child.
  61. |image10|
  62. In the *Inspector*, click on the field next to *Mesh* and create a *CubeMesh*
  63. resource to create a visible cube.
  64. |image11|
  65. Once again, it's too small by default. Click the cube icon to expand the
  66. resource and set its *Size* to ``60``, ``2``, and ``60``. As the cube
  67. resource works with a size rather than extents, we need to use these values so
  68. it matches our collision shape.
  69. |image12|
  70. You should see a wide grey slab that covers the grid and blue and red axes in
  71. the viewport.
  72. We're going to move the ground down so we can see the floor grid. Select the
  73. *Ground* node, hold the :kbd:`Ctrl` key down to turn on grid snapping (:kbd:`Cmd` on macOS),
  74. and click and drag down on the Y axis. It's the green arrow in the move gizmo.
  75. |image13|
  76. .. note::
  77. If you can't see the 3D object manipulator like on the image above, ensure
  78. the *Select Mode* is active in the toolbar above the view.
  79. |image14|
  80. Move the ground down ``1`` meter. A label in the bottom-left corner of the
  81. viewport tells you how much you're translating the node.
  82. |image15|
  83. .. note::
  84. Moving the *Ground* node down moves both children along with it.
  85. Ensure you move the *Ground* node, **not** the *MeshInstance* or the
  86. *CollisionShape*.
  87. Let's add a directional light so our scene isn't all grey. Select the *Main*
  88. node and add a *DirectionalLight* as a child of it. We need to move it and
  89. rotate it. Move it up by clicking and dragging on the manipulator's green arrow
  90. and click and drag on the red arc to rotate it around the X axis, until the
  91. ground is lit.
  92. In the *Inspector*, turn on *Shadow -> Enabled* by clicking the checkbox.
  93. |image16|
  94. At this point, your project should look like this.
  95. |image17|
  96. That's our starting point. In the next part, we will work on the player scene
  97. and base movement.
  98. .. |image1| image:: img/01.game_setup/01.import_button.png
  99. .. |image2| image:: img/01.game_setup/02.browse_to_project_folder.png
  100. .. |image3| image:: img/01.game_setup/03.import_and_edit.png
  101. .. |image4| image:: img/01.game_setup/04.start_assets.png
  102. .. |image5| image:: img/01.game_setup/05.main_node.png
  103. .. |image6| image:: img/01.game_setup/06.staticbody_node.png
  104. .. |image7| image:: img/01.game_setup/07.collision_shape_warning.png
  105. .. |image8| image:: img/01.game_setup/08.create_box_shape.png
  106. .. |image9| image:: img/01.game_setup/09.box_extents.png
  107. .. |image10| image:: img/01.game_setup/10.mesh_instance.png
  108. .. |image11| image:: img/01.game_setup/11.cube_mesh.png
  109. .. |image12| image:: img/01.game_setup/12.cube_resized.png
  110. .. |image13| image:: img/01.game_setup/13.move_gizmo_y_axis.png
  111. .. |image14| image:: img/01.game_setup/14.select_mode_icon.png
  112. .. |image15| image:: img/01.game_setup/15.translation_amount.png
  113. .. |image16| image:: img/01.game_setup/16.turn_on_shadows.png
  114. .. |image17| image:: img/01.game_setup/17.project_with_light.png