03.player_movement_code.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. :article_outdated: True
  2. .. _doc_first_3d_game_player_movement:
  3. Moving the player with code
  4. ===========================
  5. It's time to code! We're going to use the input actions we created in the last
  6. part to move the character.
  7. .. note:: For this project, we will be following the Godot naming conventions.
  8. - **GDScript**: Classes (nodes) use PascalCase, variables and
  9. functions use snake_case, and constants use ALL_CAPS (See
  10. :ref:`doc_gdscript_styleguide`).
  11. - **C#**: Classes, export variables and methods use PascalCase,
  12. private fields use _camelCase, local variables and parameters use
  13. camelCase (See :ref:`doc_c_sharp_styleguide`). Be careful to type
  14. the method names precisely when connecting signals.
  15. Right-click the ``Player`` node and select *Attach Script* to add a new script to
  16. it. In the popup, set the *Template* to *Empty* before pressing the *Create*
  17. button.
  18. |image0|
  19. Let's start with the class's properties. We're going to define a movement speed,
  20. a fall acceleration representing gravity, and a velocity we'll use to move the
  21. character.
  22. .. tabs::
  23. .. code-tab:: gdscript GDScript
  24. extends CharacterBody3D
  25. # How fast the player moves in meters per second.
  26. @export var speed = 14
  27. # The downward acceleration when in the air, in meters per second squared.
  28. @export var fall_acceleration = 75
  29. var target_velocity = Vector3.ZERO
  30. .. code-tab:: csharp
  31. using Godot;
  32. public partial class Player : CharacterBody3D
  33. {
  34. // Don't forget to rebuild the project so the editor knows about the new export variable.
  35. // How fast the player moves in meters per second.
  36. [Export]
  37. public int Speed { get; set; } = 14;
  38. // The downward acceleration when in the air, in meters per second squared.
  39. [Export]
  40. public int FallAcceleration { get; set; } = 75;
  41. private Vector3 _targetVelocity = Vector3.Zero;
  42. }
  43. These are common properties for a moving body. The ``target_velocity`` is a :ref:`3D vector <class_Vector3>`
  44. combining a speed with a direction. Here, we define it as a property because
  45. we want to update and reuse its value across frames.
  46. .. note::
  47. The values are quite different from 2D code because distances are in meters.
  48. While in 2D, a thousand units (pixels) may only correspond to half of your
  49. screen's width, in 3D, it's a kilometer.
  50. Let's code the movement. We start by calculating the input direction vector
  51. using the global ``Input`` object, in ``_physics_process()``.
  52. .. tabs::
  53. .. code-tab:: gdscript GDScript
  54. func _physics_process(delta):
  55. # We create a local variable to store the input direction.
  56. var direction = Vector3.ZERO
  57. # We check for each move input and update the direction accordingly.
  58. if Input.is_action_pressed("move_right"):
  59. direction.x += 1
  60. if Input.is_action_pressed("move_left"):
  61. direction.x -= 1
  62. if Input.is_action_pressed("move_back"):
  63. # Notice how we are working with the vector's x and z axes.
  64. # In 3D, the XZ plane is the ground plane.
  65. direction.z += 1
  66. if Input.is_action_pressed("move_forward"):
  67. direction.z -= 1
  68. .. code-tab:: csharp
  69. public override void _PhysicsProcess(double delta)
  70. {
  71. // We create a local variable to store the input direction.
  72. var direction = Vector3.Zero;
  73. // We check for each move input and update the direction accordingly.
  74. if (Input.IsActionPressed("move_right"))
  75. {
  76. direction.X += 1.0f;
  77. }
  78. if (Input.IsActionPressed("move_left"))
  79. {
  80. direction.X -= 1.0f;
  81. }
  82. if (Input.IsActionPressed("move_back"))
  83. {
  84. // Notice how we are working with the vector's X and Z axes.
  85. // In 3D, the XZ plane is the ground plane.
  86. direction.Z += 1.0f;
  87. }
  88. if (Input.IsActionPressed("move_forward"))
  89. {
  90. direction.Z -= 1.0f;
  91. }
  92. }
  93. Here, we're going to make all calculations using the ``_physics_process()``
  94. virtual function. Like ``_process()``, it allows you to update the node every
  95. frame, but it's designed specifically for physics-related code like moving a
  96. kinematic or rigid body.
  97. .. seealso::
  98. To learn more about the difference between ``_process()`` and
  99. ``_physics_process()``, see :ref:`doc_idle_and_physics_processing`.
  100. We start by initializing a ``direction`` variable to ``Vector3.ZERO``. Then, we
  101. check if the player is pressing one or more of the ``move_*`` inputs and update
  102. the vector's ``x`` and ``z`` components accordingly. These correspond to the
  103. ground plane's axes.
  104. These four conditions give us eight possibilities and eight possible directions.
  105. In case the player presses, say, both W and D simultaneously, the vector will
  106. have a length of about ``1.4``. But if they press a single key, it will have a
  107. length of ``1``. We want the vector's length to be consistent, and not move faster diagonally. To do so, we can
  108. call its ``normalized()`` method.
  109. .. tabs::
  110. .. code-tab:: gdscript GDScript
  111. #func _physics_process(delta):
  112. #...
  113. if direction != Vector3.ZERO:
  114. direction = direction.normalized()
  115. # Setting the basis property will affect the rotation of the node.
  116. $Pivot.basis = Basis.looking_at(direction)
  117. .. code-tab:: csharp
  118. public override void _PhysicsProcess(double delta)
  119. {
  120. // ...
  121. if (direction != Vector3.Zero)
  122. {
  123. direction = direction.Normalized();
  124. // Setting the basis property will affect the rotation of the node.
  125. GetNode<Node3D>("Pivot").Basis = Basis.LookingAt(direction);
  126. }
  127. }
  128. Here, we only normalize the vector if the direction has a length greater than
  129. zero, which means the player is pressing a direction key.
  130. We compute the direction the ``$Pivot`` is looking by creating a :ref:`Basis <class_Basis>`
  131. that looks in the ``direction`` direction.
  132. Then, we update the velocity. We have to calculate the ground velocity and the
  133. fall speed separately. Be sure to go back one tab so the lines are inside the
  134. ``_physics_process()`` function but outside the condition we just wrote above.
  135. .. tabs::
  136. .. code-tab:: gdscript GDScript
  137. func _physics_process(delta):
  138. #...
  139. if direction != Vector3.ZERO:
  140. #...
  141. # Ground Velocity
  142. target_velocity.x = direction.x * speed
  143. target_velocity.z = direction.z * speed
  144. # Vertical Velocity
  145. if not is_on_floor(): # If in the air, fall towards the floor. Literally gravity
  146. target_velocity.y = target_velocity.y - (fall_acceleration * delta)
  147. # Moving the Character
  148. velocity = target_velocity
  149. move_and_slide()
  150. .. code-tab:: csharp
  151. public override void _PhysicsProcess(double delta)
  152. {
  153. // ...
  154. if (direction != Vector3.Zero)
  155. {
  156. // ...
  157. }
  158. // Ground velocity
  159. _targetVelocity.X = direction.X * Speed;
  160. _targetVelocity.Z = direction.Z * Speed;
  161. // Vertical velocity
  162. if (!IsOnFloor()) // If in the air, fall towards the floor. Literally gravity
  163. {
  164. _targetVelocity.Y -= FallAcceleration * (float)delta;
  165. }
  166. // Moving the character
  167. Velocity = _targetVelocity;
  168. MoveAndSlide();
  169. }
  170. The ``CharacterBody3D.is_on_floor()`` function returns ``true`` if the body collided with the floor in this frame. That's why
  171. we apply gravity to the ``Player`` only while it is in the air.
  172. For the vertical velocity, we subtract the fall acceleration multiplied by the
  173. delta time every frame.
  174. This line of code will cause our character to fall in every frame, as long as it is not on or colliding with the floor.
  175. The physics engine can only detect interactions with walls, the floor, or other
  176. bodies during a given frame if movement and collisions happen. We will use this
  177. property later to code the jump.
  178. On the last line, we call ``CharacterBody3D.move_and_slide()`` which is a powerful
  179. method of the ``CharacterBody3D`` class that allows you to move a character
  180. smoothly. If it hits a wall midway through a motion, the engine will try to
  181. smooth it out for you. It uses the *velocity* value native to the :ref:`CharacterBody3D <class_CharacterBody3D>`
  182. .. OLD TEXT: The function takes two parameters: our velocity and the up direction. It moves
  183. .. the character and returns a leftover velocity after applying collisions. When
  184. .. hitting the floor or a wall, the function will reduce or reset the speed in that
  185. .. direction from you. In our case, storing the function's returned value prevents
  186. .. the character from accumulating vertical momentum, which could otherwise get so
  187. .. big the character would move through the ground slab after a while.
  188. And that's all the code you need to move the character on the floor.
  189. Here is the complete ``Player.gd`` code for reference.
  190. .. tabs::
  191. .. code-tab:: gdscript GDScript
  192. extends CharacterBody3D
  193. # How fast the player moves in meters per second.
  194. @export var speed = 14
  195. # The downward acceleration when in the air, in meters per second squared.
  196. @export var fall_acceleration = 75
  197. var target_velocity = Vector3.ZERO
  198. func _physics_process(delta):
  199. var direction = Vector3.ZERO
  200. if Input.is_action_pressed("move_right"):
  201. direction.x += 1
  202. if Input.is_action_pressed("move_left"):
  203. direction.x -= 1
  204. if Input.is_action_pressed("move_back"):
  205. direction.z += 1
  206. if Input.is_action_pressed("move_forward"):
  207. direction.z -= 1
  208. if direction != Vector3.ZERO:
  209. direction = direction.normalized()
  210. $Pivot.basis = Basis.looking_at(direction)
  211. # Ground Velocity
  212. target_velocity.x = direction.x * speed
  213. target_velocity.z = direction.z * speed
  214. # Vertical Velocity
  215. if not is_on_floor(): # If in the air, fall towards the floor. Literally gravity
  216. target_velocity.y = target_velocity.y - (fall_acceleration * delta)
  217. # Moving the Character
  218. velocity = target_velocity
  219. move_and_slide()
  220. .. code-tab:: csharp
  221. using Godot;
  222. public partial class Player : CharacterBody3D
  223. {
  224. // How fast the player moves in meters per second.
  225. [Export]
  226. public int Speed { get; set; } = 14;
  227. // The downward acceleration when in the air, in meters per second squared.
  228. [Export]
  229. public int FallAcceleration { get; set; } = 75;
  230. private Vector3 _targetVelocity = Vector3.Zero;
  231. public override void _PhysicsProcess(double delta)
  232. {
  233. var direction = Vector3.Zero;
  234. if (Input.IsActionPressed("move_right"))
  235. {
  236. direction.X += 1.0f;
  237. }
  238. if (Input.IsActionPressed("move_left"))
  239. {
  240. direction.X -= 1.0f;
  241. }
  242. if (Input.IsActionPressed("move_back"))
  243. {
  244. direction.Z += 1.0f;
  245. }
  246. if (Input.IsActionPressed("move_forward"))
  247. {
  248. direction.Z -= 1.0f;
  249. }
  250. if (direction != Vector3.Zero)
  251. {
  252. direction = direction.Normalized();
  253. GetNode<Node3D>("Pivot").Basis = Basis.LookingAt(direction);
  254. }
  255. // Ground velocity
  256. _targetVelocity.X = direction.X * Speed;
  257. _targetVelocity.Z = direction.Z * Speed;
  258. // Vertical velocity
  259. if (!IsOnFloor()) // If in the air, fall towards the floor. Literally gravity
  260. {
  261. _targetVelocity.Y -= FallAcceleration * (float)delta;
  262. }
  263. // Moving the character
  264. Velocity = _targetVelocity;
  265. MoveAndSlide();
  266. }
  267. }
  268. Testing our player's movement
  269. -----------------------------
  270. We're going to put our player in the ``Main`` scene to test it. To do so, we need
  271. to instantiate the player and then add a camera. Unlike in 2D, in 3D, you won't
  272. see anything if your viewport doesn't have a camera pointing at something.
  273. Save your ``Player`` scene and open the ``Main`` scene. You can click on the *Main*
  274. tab at the top of the editor to do so.
  275. |image1|
  276. If you closed the scene before, head to the *FileSystem* dock and double-click
  277. ``main.tscn`` to re-open it.
  278. To instantiate the ``Player``, right-click on the ``Main`` node and select *Instantiate
  279. Child Scene*.
  280. |image2|
  281. In the popup, double-click ``player.tscn``. The character should appear in the
  282. center of the viewport.
  283. Adding a camera
  284. ~~~~~~~~~~~~~~~
  285. Let's add the camera next. Like we did with our *Player*\ 's *Pivot*, we're
  286. going to create a basic rig. Right-click on the ``Main`` node again and select
  287. *Add Child Node*. Create a new :ref:`Marker3D <class_Marker3D>`, and name it ``CameraPivot``. Select ``CameraPivot`` and add a child node :ref:`Camera3D <class_Camera3D>` to it. Your scene tree should look like this.
  288. |image3|
  289. Notice the *Preview* checkbox that appears in the top-left when you have the
  290. *Camera* selected. You can click it to preview the in-game camera projection.
  291. |image4|
  292. We're going to use the *Pivot* to rotate the camera as if it was on a crane.
  293. Let's first split the 3D view to be able to freely navigate the scene and see
  294. what the camera sees.
  295. In the toolbar right above the viewport, click on *View*, then *2 Viewports*.
  296. You can also press :kbd:`Ctrl + 2` (:kbd:`Cmd + 2` on macOS).
  297. |image11|
  298. |image5|
  299. On the bottom view, select your :ref:`Camera3D <class_Camera3D>` and turn on camera Preview by clicking
  300. the checkbox.
  301. |image6|
  302. In the top view, move the camera about ``19`` units on the Z axis (the blue
  303. one).
  304. |image7|
  305. Here's where the magic happens. Select the *CameraPivot* and rotate it ``-45``
  306. degrees around the X axis (using the red circle). You'll see the camera move as
  307. if it was attached to a crane.
  308. |image8|
  309. You can run the scene by pressing :kbd:`F6` and press the arrow keys to move the
  310. character.
  311. |image9|
  312. We can see some empty space around the character due to the perspective
  313. projection. In this game, we're going to use an orthographic projection instead
  314. to better frame the gameplay area and make it easier for the player to read
  315. distances.
  316. Select the *Camera* again and in the *Inspector*, set the *Projection* to
  317. *Orthogonal* and the *Size* to ``19``. The character should now look flatter and
  318. the ground should fill the background.
  319. .. note::
  320. When using an orthogonal camera in Godot 4, directional shadow quality is
  321. dependent on the camera's *Far* value. The higher the *Far* value, the
  322. further away the camera will be able to see. However, higher *Far* values
  323. also decrease shadow quality as the shadow rendering has to cover a greater
  324. distance.
  325. If directional shadows look too blurry after switching to an orthogonal
  326. camera, decrease the camera's *Far* property to a lower value such as
  327. ``100``. Don't decrease this *Far* property too much, or objects in the
  328. distance will start disappearing.
  329. |image10|
  330. Test your scene and you should be able to move in all 8 directions and not glitch through the floor!
  331. Ultimately, we have both player movement and the view in place. Next, we will
  332. work on the monsters.
  333. .. |image0| image:: img/03.player_movement_code/01.attach_script_to_player.webp
  334. .. |image1| image:: img/03.player_movement_code/02.clicking_main_tab.png
  335. .. |image2| image:: img/03.player_movement_code/03.instance_child_scene.webp
  336. .. |image3| image:: img/03.player_movement_code/04.scene_tree_with_camera.webp
  337. .. |image4| image:: img/03.player_movement_code/05.camera_preview_checkbox.png
  338. .. |image5| image:: img/03.player_movement_code/06.two_viewports.png
  339. .. |image6| image:: img/03.player_movement_code/07.camera_preview_checkbox.png
  340. .. |image7| image:: img/03.player_movement_code/08.camera_moved.png
  341. .. |image8| image:: img/03.player_movement_code/09.camera_rotated.png
  342. .. |image9| image:: img/03.player_movement_code/10.camera_perspective.png
  343. .. |image10| image:: img/03.player_movement_code/13.camera3d_values.webp
  344. .. |image11| image:: img/03.player_movement_code/12.viewport_change.webp