05.spawning_mobs.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. .. _doc_first_3d_game_spawning_monsters:
  2. Spawning monsters
  3. =================
  4. In this part, we're going to spawn monsters along a path randomly. By the end,
  5. you will have monsters roaming the game board.
  6. |image0|
  7. Double-click on ``main.tscn`` in the *FileSystem* dock to open the ``Main`` scene.
  8. Before drawing the path, we're going to change the game resolution. Our game has
  9. a default window size of ``1152x648``. We're going to set it to ``720x540``, a
  10. nice little box.
  11. Go to *Project -> Project Settings*.
  12. |image1|
  13. If you still have *Input Map* open, switch to the *General* tab.
  14. In the left menu, navigate down to *Display -> Window*. On the right, set the
  15. *Width* to ``720`` and the *Height* to ``540``.
  16. |image2|
  17. Creating the spawn path
  18. -----------------------
  19. Like you did in the 2D game tutorial, you're going to design a path and use a
  20. :ref:`PathFollow3D <class_PathFollow3D>` node to sample random locations on it.
  21. In 3D though, it's a bit more complicated to draw the path. We want it to be
  22. around the game view so monsters appear right outside the screen. But if we draw
  23. a path, we won't see it from the camera preview.
  24. To find the view's limits, we can use some placeholder meshes. Your viewport
  25. should still be split into two parts, with the camera preview at the bottom. If
  26. that isn't the case, press :kbd:`Ctrl + 2` (:kbd:`Cmd + 2` on macOS) to split the view into two.
  27. Select the :ref:`Camera3D <class_Camera3D>` node and click the *Preview* checkbox in the bottom
  28. viewport.
  29. |image3|
  30. Adding placeholder cylinders
  31. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  32. Let's add the placeholder meshes. Add a new :ref:`Node3D <class_Node3D>` as a child of the
  33. ``Main`` node and name it ``Cylinders``. We'll use it to group the cylinders. Select ``Cylinders`` and add a child node :ref:`MeshInstance3D <class_MeshInstance3D>`
  34. |image4|
  35. In the *Inspector*, assign a *CylinderMesh* to the *Mesh* property.
  36. |image5|
  37. Set the top viewport to the top orthogonal view using the menu in the viewport's
  38. top-left corner. Alternatively, you can press the keypad's 7 key.
  39. |image6|
  40. The grid may be distracting. You can toggle it by going to the *View*
  41. menu in the toolbar and clicking *View Grid*.
  42. |image7|
  43. You now want to move the cylinder along the ground plane, looking at the camera
  44. preview in the bottom viewport. I recommend using grid snap to do so. You can
  45. toggle it by clicking the magnet icon in the toolbar or pressing Y.
  46. |image8|
  47. Move the cylinder so it's right outside the camera's view in the top-left
  48. corner.
  49. |image9|
  50. We're going to create copies of the mesh and place them around the game area.
  51. Press :kbd:`Ctrl + D` (:kbd:`Cmd + D` on macOS) to duplicate the node. You can also right-click
  52. the node in the *Scene* dock and select *Duplicate*. Move the copy down along
  53. the blue Z axis until it's right outside the camera's preview.
  54. Select both cylinders by pressing the :kbd:`Shift` key and clicking on the unselected
  55. one and duplicate them.
  56. |image10|
  57. Move them to the right by dragging the red X axis.
  58. |image11|
  59. They're a bit hard to see in white, aren't they? Let's make them stand out by
  60. giving them a new material.
  61. In 3D, materials define a surface's visual properties like its color, how it
  62. reflects light, and more. We can use them to change the color of a mesh.
  63. We can update all four cylinders at once. Select all the mesh instances in the
  64. *Scene* dock. To do so, you can click on the first one and Shift click on the
  65. last one.
  66. |image12|
  67. In the *Inspector*, expand the *Material* section and assign a :ref:`StandardMaterial3D <class_StandardMaterial3D>` to slot *0*.
  68. |image13|
  69. .. image:: img/05.spawning_mobs/standard_material.webp
  70. Click the sphere icon to open the material resource. You get a preview of the
  71. material and a long list of sections filled with properties. You can use these
  72. to create all sorts of surfaces, from metal to rock or water.
  73. Expand the *Albedo* section.
  74. .. image:: img/05.spawning_mobs/albedo_section.webp
  75. Set the color to something that contrasts with
  76. the background, like a bright orange.
  77. |image14|
  78. We can now use the cylinders as guides. Fold them in the *Scene* dock by
  79. clicking the grey arrow next to them. Moving forward, you can also toggle their
  80. visibility by clicking the eye icon next to *Cylinders*.
  81. |image15|
  82. Add a child node :ref:`Path3D <class_Path3D>` to ``Main`` node. In the toolbar, four icons appear. Click
  83. the *Add Point* tool, the icon with the green "+" sign.
  84. |image16|
  85. .. note:: You can hover any icon to see a tooltip describing the tool.
  86. Click in the center of each cylinder to create a point. Then, click the *Close
  87. Curve* icon in the toolbar to close the path. If any point is a bit off, you can
  88. click and drag on it to reposition it.
  89. |image17|
  90. Your path should look like this.
  91. |image18|
  92. To sample random positions on it, we need a :ref:`PathFollow3D <class_PathFollow3D>` node. Add a
  93. :ref:`PathFollow3D <class_PathFollow3D>` as a child of the ``Path3D``. Rename the two nodes to ``SpawnLocation`` and
  94. ``SpawnPath``, respectively. It's more descriptive of what we'll use them for.
  95. |image19|
  96. With that, we're ready to code the spawn mechanism.
  97. Spawning monsters randomly
  98. --------------------------
  99. Right-click on the ``Main`` node and attach a new script to it.
  100. We first export a variable to the *Inspector* so that we can assign ``mob.tscn``
  101. or any other monster to it.
  102. .. tabs::
  103. .. code-tab:: gdscript GDScript
  104. extends Node
  105. @export var mob_scene: PackedScene
  106. .. code-tab:: csharp
  107. using Godot;
  108. public partial class Main : Node
  109. {
  110. // Don't forget to rebuild the project so the editor knows about the new export variable.
  111. [Export]
  112. public PackedScene MobScene { get; set; }
  113. }
  114. We want to spawn mobs at regular time intervals. To do this, we need to go back
  115. to the scene and add a timer. Before that, though, we need to assign the
  116. ``mob.tscn`` file to the ``mob_scene`` property above (otherwise it's null!)
  117. Head back to the 3D screen and select the ``Main`` node. Drag ``mob.tscn`` from
  118. the *FileSystem* dock to the *Mob Scene* slot in the *Inspector*.
  119. |image20|
  120. Add a new :ref:`Timer <class_Timer>` node as a child of ``Main``. Name it ``MobTimer``.
  121. |image21|
  122. In the *Inspector*, set its *Wait Time* to ``0.5`` seconds and turn on
  123. *Autostart* so it automatically starts when we run the game.
  124. |image22|
  125. Timers emit a ``timeout`` signal every time they reach the end of their *Wait
  126. Time*. By default, they restart automatically, emitting the signal in a cycle.
  127. We can connect to this signal from the *Main* node to spawn monsters every
  128. ``0.5`` seconds.
  129. With the *MobTimer* still selected, head to the *Node* dock on the right, and
  130. double-click the ``timeout`` signal.
  131. |image23|
  132. Connect it to the *Main* node.
  133. |image24|
  134. This will take you back to the script, with a new empty
  135. ``_on_mob_timer_timeout()`` function.
  136. Let's code the mob spawning logic. We're going to:
  137. 1. Instantiate the mob scene.
  138. 2. Sample a random position on the spawn path.
  139. 3. Get the player's position.
  140. 4. Call the mob's ``initialize()`` method, passing it the random position and
  141. the player's position.
  142. 5. Add the mob as a child of the *Main* node.
  143. .. tabs::
  144. .. code-tab:: gdscript GDScript
  145. func _on_mob_timer_timeout():
  146. # Create a new instance of the Mob scene.
  147. var mob = mob_scene.instantiate()
  148. # Choose a random location on the SpawnPath.
  149. # We store the reference to the SpawnLocation node.
  150. var mob_spawn_location = get_node("SpawnPath/SpawnLocation")
  151. # And give it a random offset.
  152. mob_spawn_location.progress_ratio = randf()
  153. var player_position = $Player.position
  154. mob.initialize(mob_spawn_location.position, player_position)
  155. # Spawn the mob by adding it to the Main scene.
  156. add_child(mob)
  157. .. code-tab:: csharp
  158. // We also specified this function name in PascalCase in the editor's connection window.
  159. private void OnMobTimerTimeout()
  160. {
  161. // Create a new instance of the Mob scene.
  162. Mob mob = MobScene.Instantiate<Mob>();
  163. // Choose a random location on the SpawnPath.
  164. // We store the reference to the SpawnLocation node.
  165. var mobSpawnLocation = GetNode<PathFollow3D>("SpawnPath/SpawnLocation");
  166. // And give it a random offset.
  167. mobSpawnLocation.ProgressRatio = GD.Randf();
  168. Vector3 playerPosition = GetNode<Player>("Player").Position;
  169. mob.Initialize(mobSpawnLocation.Position, playerPosition);
  170. // Spawn the mob by adding it to the Main scene.
  171. AddChild(mob);
  172. }
  173. Above, ``randf()`` produces a random value between ``0`` and ``1``, which is
  174. what the *PathFollow* node's ``progress_ratio`` expects:
  175. 0 is the start of the path, 1 is the end of the path.
  176. The path we have set is around the camera's viewport, so any random value between 0 and 1
  177. is a random position alongside the edges of the viewport!
  178. Note that if you remove the ``Player`` from the main scene, the following line
  179. .. tabs::
  180. .. code-tab:: gdscript GDScript
  181. var player_position = $Player.position
  182. .. code-tab:: csharp
  183. Vector3 playerPosition = GetNode<Player>("Player").Position;
  184. gives an error because there is no $Player!
  185. Here is the complete ``main.gd`` script so far, for reference.
  186. .. tabs::
  187. .. code-tab:: gdscript GDScript
  188. extends Node
  189. @export var mob_scene: PackedScene
  190. func _on_mob_timer_timeout():
  191. # Create a new instance of the Mob scene.
  192. var mob = mob_scene.instantiate()
  193. # Choose a random location on the SpawnPath.
  194. # We store the reference to the SpawnLocation node.
  195. var mob_spawn_location = get_node("SpawnPath/SpawnLocation")
  196. # And give it a random offset.
  197. mob_spawn_location.progress_ratio = randf()
  198. var player_position = $Player.position
  199. mob.initialize(mob_spawn_location.position, player_position)
  200. # Spawn the mob by adding it to the Main scene.
  201. add_child(mob)
  202. .. code-tab:: csharp
  203. using Godot;
  204. public partial class Main : Node
  205. {
  206. [Export]
  207. public PackedScene MobScene { get; set; }
  208. private void OnMobTimerTimeout()
  209. {
  210. // Create a new instance of the Mob scene.
  211. Mob mob = MobScene.Instantiate<Mob>();
  212. // Choose a random location on the SpawnPath.
  213. // We store the reference to the SpawnLocation node.
  214. var mobSpawnLocation = GetNode<PathFollow3D>("SpawnPath/SpawnLocation");
  215. // And give it a random offset.
  216. mobSpawnLocation.ProgressRatio = GD.Randf();
  217. Vector3 playerPosition = GetNode<Player>("Player").Position;
  218. mob.Initialize(mobSpawnLocation.Position, playerPosition);
  219. // Spawn the mob by adding it to the Main scene.
  220. AddChild(mob);
  221. }
  222. }
  223. You can test the scene by pressing :kbd:`F6`. You should see the monsters spawn and
  224. move in a straight line.
  225. |image25|
  226. For now, they bump and slide against one another when their paths cross. We'll
  227. address this in the next part.
  228. .. |image0| image:: img/05.spawning_mobs/01.monsters_path_preview.png
  229. .. |image1| image:: img/05.spawning_mobs/02.project_settings.png
  230. .. |image2| image:: img/05.spawning_mobs/03.window_settings.webp
  231. .. |image3| image:: img/05.spawning_mobs/04.camera_preview.png
  232. .. |image4| image:: img/05.spawning_mobs/05.cylinders_node.png
  233. .. |image5| image:: img/05.spawning_mobs/06.cylinder_mesh.png
  234. .. |image6| image:: img/05.spawning_mobs/07.top_view.png
  235. .. |image7| image:: img/05.spawning_mobs/08.toggle_view_grid.png
  236. .. |image8| image:: img/05.spawning_mobs/09.toggle_grid_snap.png
  237. .. |image9| image:: img/05.spawning_mobs/10.place_first_cylinder.png
  238. .. |image10| image:: img/05.spawning_mobs/11.both_cylinders_selected.png
  239. .. |image11| image:: img/05.spawning_mobs/12.four_cylinders.png
  240. .. |image12| image:: img/05.spawning_mobs/13.selecting_all_cylinders.png
  241. .. |image13| image:: img/05.spawning_mobs/14.multi_material_selection.webp
  242. .. |image14| image:: img/05.spawning_mobs/15.bright-cylinders.png
  243. .. |image15| image:: img/05.spawning_mobs/16.cylinders_fold.png
  244. .. |image16| image:: img/05.spawning_mobs/17.points_options.png
  245. .. |image17| image:: img/05.spawning_mobs/18.close_path.png
  246. .. |image18| image:: img/05.spawning_mobs/19.path_result.png
  247. .. |image19| image:: img/05.spawning_mobs/20.spawn_nodes.png
  248. .. |image20| image:: img/05.spawning_mobs/20.mob_scene_property.png
  249. .. |image21| image:: img/05.spawning_mobs/21.mob_timer.png
  250. .. |image22| image:: img/05.spawning_mobs/22.mob_timer_properties.png
  251. .. |image23| image:: img/05.spawning_mobs/23.timeout_signal.png
  252. .. |image24| image:: img/05.spawning_mobs/24.connect_timer_to_main.webp
  253. .. |image25| image:: img/05.spawning_mobs/25.spawn_result.png