navigation_introduction_3d.rst 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. .. _doc_navigation_overview_3d:
  2. 3D navigation overview
  3. ======================
  4. Godot provides multiple objects, classes and servers to facilitate grid-based or mesh-based navigation and pathfinding for 2D and 3D games.
  5. The following section provides a quick overview over all available navigation related objects in Godot for 3D scenes and their primary use.
  6. Godot provides the following objects and classes for 3D navigation:
  7. - :ref:`Astar3D<class_Astar3D>`
  8. ``Astar3D`` objects provide an option to find the shortest path in a graph of weighted **points**.
  9. The AStar3D class is best suited for cell-based 3D gameplay that does not require actors to reach any possible position within an area but only predefined, distinct positions.
  10. - :ref:`NavigationServer3D<class_NavigationServer3D>`
  11. ``NavigationServer3D`` provides a powerful server API to find the shortest path between two positions on an area defined by a navigation mesh.
  12. The NavigationServer is best suited for 3D realtime gameplay that does require actors to reach any possible position within a navigation mesh defined area.
  13. Mesh-based navigation scales well with large game worlds as a large area can often be defined with a single polygon when it would require many, many grid cells.
  14. The NavigationServer holds different navigation maps that each consist of regions that hold navigation mesh data.
  15. Agents can be placed on a map for avoidance calculation.
  16. RIDs are used to reference internal maps, regions, and agents when communicating with the server.
  17. The following NavigationServer RID types are available.
  18. - NavMap RID
  19. Reference to a specific navigation map that holds regions and agents.
  20. The map will attempt to join the navigation meshes of the regions by proximity.
  21. The map will synchronize regions and agents each physics frame.
  22. - NavRegion RID
  23. Reference to a specific navigation region that can hold navigation mesh data.
  24. The region can be enabled / disabled or the use restricted with a navigation layer bitmask.
  25. - NavLink RID
  26. Reference to a specific navigation link that connects two navigation mesh positions over arbitrary distances.
  27. - NavAgent RID
  28. Reference to a specific avoidance agent.
  29. The avoidance is defined by a radius value.
  30. - NavObstacle RID
  31. Reference to a specific avoidance obstacle used to affect and constrain the avoidance velocity of agents.
  32. The following scene tree nodes are available as helpers to work with the NavigationServer3D API.
  33. - :ref:`NavigationRegion3D<class_NavigationRegion3D>` Node
  34. A Node that holds a Navigation Mesh resource that defines a navigation mesh for the NavigationServer3D.
  35. - The region can be enabled / disabled.
  36. - The use in pathfinding can be further restricted through the ``navigation_layers`` bitmask.
  37. - The NavigationServer3D will join the navigation meshes of regions by proximity for a combined navigation mesh.
  38. - :ref:`NavigationLink3D<class_NavigationLink3D>` Node
  39. A Node that connects two positions on navigation meshes over arbitrary distances for pathfinding.
  40. - The link can be enabled / disabled.
  41. - The link can be made one-way or bidirectional.
  42. - The use in pathfinding can be further restricted through the ``navigation_layers`` bitmask.
  43. Links tell the pathfinding that a connection exists and at what cost. The actual agent handling and movement needs to happen in custom scripts.
  44. - :ref:`NavigationAgent3D<class_NavigationAgent3D>` Node
  45. A helper Node used to facilitate common NavigationServer3D API calls for pathfinding and avoidance.
  46. Use this Node with a Node3D inheriting parent Node.
  47. - :ref:`NavigationObstacle3D<class_NavigationObstacle3D>` Node
  48. A Node that can be used to affect and constrain the avoidance velocity of avoidance enabled agents.
  49. This Node does NOT affect the pathfinding of agents. You need to change the navigation meshes for that instead.
  50. The 3D navigation meshes are defined with the following resources:
  51. - :ref:`NavigationMesh<class_NavigationMesh>` Resource
  52. A resource that holds 3D navigation mesh data.
  53. It provides 3D geometry baking options to define navigation areas inside the Editor as well as at runtime.
  54. - The NavigationRegion3D Node uses this resource to define its navigation area.
  55. - The NavigationServer3D uses this resource to update the navigation mesh of individual regions.
  56. - The GridMap Editor uses this resource when specific navigation meshes are defined for each grid cell.
  57. .. seealso::
  58. You can see how 3D navigation works in action using the
  59. `3D Navigation demo project <https://github.com/godotengine/godot-demo-projects/tree/master/3d/navigation>`__.
  60. Setup for 3D scene
  61. ------------------
  62. The following steps show a basic setup for minimal viable navigation in 3D.
  63. It uses the NavigationServer3D and a NavigationAgent3D for path movement.
  64. #. Add a NavigationRegion3D Node to the scene.
  65. #. Click on the region node and add a new :ref:`NavigationMesh<class_NavigationMesh>` Resource to
  66. the region node.
  67. .. image:: img/nav_3d_min_setup_step1.png
  68. #. Add a new MeshInstance3D node as a child of the region node.
  69. #. Select the MeshInstance3D node and add a new PlaneMesh and increase the xy size to 10.
  70. #. Select the region node again and press the "Bake Navmesh" button on the top bar.
  71. .. image:: img/nav_3d_min_setup_step2.png
  72. #. Now a transparent navigation mesh appears that hovers some distance on top of the PlaneMesh.
  73. .. image:: img/nav_3d_min_setup_step3.png
  74. #. Add a CharacterBody3D node in the scene with a basic collision shape and some mesh for visuals.
  75. #. Add a NavigationAgent3D node below the character node.
  76. .. image:: img/nav_3d_min_setup_step4.webp
  77. #. Add a script to the CharacterBody3D node with the following content. We make sure to set a
  78. movement target after the scene has fully loaded and the NavigationServer had time to sync.
  79. Also, add a Camera3D and some light and environment to see something.
  80. .. tabs::
  81. .. code-tab:: gdscript GDScript
  82. extends CharacterBody3D
  83. var movement_speed: float = 2.0
  84. var movement_target_position: Vector3 = Vector3(-3.0,0.0,2.0)
  85. @onready var navigation_agent: NavigationAgent3D = $NavigationAgent3D
  86. func _ready():
  87. # These values need to be adjusted for the actor's speed
  88. # and the navigation layout.
  89. navigation_agent.path_desired_distance = 0.5
  90. navigation_agent.target_desired_distance = 0.5
  91. # Make sure to not await during _ready.
  92. actor_setup.call_deferred()
  93. func actor_setup():
  94. # Wait for the first physics frame so the NavigationServer can sync.
  95. await get_tree().physics_frame
  96. # Now that the navigation map is no longer empty, set the movement target.
  97. set_movement_target(movement_target_position)
  98. func set_movement_target(movement_target: Vector3):
  99. navigation_agent.set_target_position(movement_target)
  100. func _physics_process(delta):
  101. if navigation_agent.is_navigation_finished():
  102. return
  103. var current_agent_position: Vector3 = global_position
  104. var next_path_position: Vector3 = navigation_agent.get_next_path_position()
  105. velocity = current_agent_position.direction_to(next_path_position) * movement_speed
  106. move_and_slide()
  107. .. code-tab:: csharp C#
  108. using Godot;
  109. public partial class MyCharacterBody3D : CharacterBody3D
  110. {
  111. private NavigationAgent3D _navigationAgent;
  112. private float _movementSpeed = 2.0f;
  113. private Vector3 _movementTargetPosition = new Vector3(-3.0f, 0.0f, 2.0f);
  114. public Vector3 MovementTarget
  115. {
  116. get { return _navigationAgent.TargetPosition; }
  117. set { _navigationAgent.TargetPosition = value; }
  118. }
  119. public override void _Ready()
  120. {
  121. base._Ready();
  122. _navigationAgent = GetNode<NavigationAgent3D>("NavigationAgent3D");
  123. // These values need to be adjusted for the actor's speed
  124. // and the navigation layout.
  125. _navigationAgent.PathDesiredDistance = 0.5f;
  126. _navigationAgent.TargetDesiredDistance = 0.5f;
  127. // Make sure to not await during _Ready.
  128. Callable.From(ActorSetup).CallDeferred();
  129. }
  130. public override void _PhysicsProcess(double delta)
  131. {
  132. base._PhysicsProcess(delta);
  133. if (_navigationAgent.IsNavigationFinished())
  134. {
  135. return;
  136. }
  137. Vector3 currentAgentPosition = GlobalTransform.Origin;
  138. Vector3 nextPathPosition = _navigationAgent.GetNextPathPosition();
  139. Velocity = currentAgentPosition.DirectionTo(nextPathPosition) * _movementSpeed;
  140. MoveAndSlide();
  141. }
  142. private async void ActorSetup()
  143. {
  144. // Wait for the first physics frame so the NavigationServer can sync.
  145. await ToSignal(GetTree(), SceneTree.SignalName.PhysicsFrame);
  146. // Now that the navigation map is no longer empty, set the movement target.
  147. MovementTarget = _movementTargetPosition;
  148. }
  149. }
  150. .. note::
  151. On the first frame the NavigationServer map has not synchronized region data and any path query will return empty. Wait for the NavigationServer synchronization by awaiting one frame in the script.