navigation_introduction_3d.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. .. _doc_navigation_overview_3d:
  2. Introduction
  3. ===================
  4. Godot provides multiple objects, classes and servers to facilitate grid-based or mesh-based navigation
  5. and pathfinding for 2D and 3D games. The following section provides a quick overview over all available
  6. navigation related objects in Godot for 3D scenes and their primary use.
  7. 3D Navigation Overview
  8. ----------------------
  9. Godot provides the following objects and classes for 3D navigation:
  10. - :ref:`Astar3D<class_Astar3D>`
  11. ``Astar3D`` objects provide an option to find the shortest path in a graph of weighted **points**.
  12. The AStar3D class is best suited for cellbased 3D gameplay that does not require actors to reach any
  13. possible position within an area but only predefined, distinct positions.
  14. - :ref:`NavigationServer3D<class_NavigationServer3D>`
  15. ``NavigationServer3D`` provides a powerful server API to find the shortest path between two positions
  16. on a area defined by a navigation mesh.
  17. The NavigationServer is best suited for 3D realtime gameplay that does require actors to reach any
  18. possible position within an navmesh defined area. Meshbased navigation scales well with large gameworlds
  19. as a large area can often be defined with a single polygon when it would require many, many grid cells.
  20. The NavigationServer holds different navigation maps that each consist of regions that hold navigation mesh
  21. data. Agents can be placed on a map for avoidance calculation. RIDs are used to reference the internal maps,
  22. regions and agents when communicating with the server.
  23. The following NavigationServer RID types are available.
  24. - NavMap RID
  25. Reference to a specific navigation map that holds regions and agents.
  26. The map will attempt to join changed navigation meshes of regions by proximity.
  27. The map will synchronise regions and agents each physics frame.
  28. - NavRegion RID
  29. Reference to a specific navigation region that can hold navigation mesh data.
  30. The region can be enabled / disabled or the use restricted with a navigationlayer bitmask.
  31. - NavAgent RID
  32. Reference to a specific avoidance agent with a radius value use solely in avoidance.
  33. The following SceneTree Nodes are available as helpers to work with the NavigationServer3D API.
  34. - :ref:`NavigationRegion3D<class_NavigationRegion3D>` Node
  35. A Node that holds a Navigation Mesh resource that defines a navigation mesh for the NavigationServer3D.
  36. The region can be enabled / disabled.
  37. The use in pathfinding can be further restricted through the navigationlayers bitmask.
  38. Regions can join their navigation meshes by proximity for a combined navigation mesh.
  39. - :ref:`NavigationAgent3D<class_NavigationAgent3D>` Node
  40. An optional helper Node to facilitate common NavigationServer3D API calls for pathfinding and avoidance for
  41. a Node3D inheriting parent Node.
  42. - :ref:`NavigationObstacle3D<class_NavigationObstacle3D>` Node
  43. A Node that acts as an agent with avoidance radius, to work it needs to be added under a Node3D
  44. inheriting parent Node. Obstacles are intended as a last resort option for constantly moving objects
  45. that cannot be re(baked) to a navigation mesh efficiently. This node also only works if RVO processing
  46. is being used.
  47. The 3D navigation meshes are defined with the following resources:
  48. - :ref:`NavigationMesh<class_NavigationMesh>` Resource
  49. A resource that holds 3D navigation mesh data and provides 3D geometry baking options to define navigation
  50. areas inside the Editor as well as at runtime.
  51. - The NavigationRegion3D Node uses this resource to define its navigation area.
  52. - The NavigationServer3D uses this resource to update navmesh of individual regions.
  53. - The GridMap Editor uses this resource when specific navigation meshes are defined for each gridcell.
  54. Setup for 3D scene
  55. ------------------
  56. The following steps show how to setup a minimum viable navigation in 3D that uses the NavigationServer3D and
  57. a NavigationAgent3D for path movement.
  58. 1.) Add a NavigationRegion3D Node to the scene.
  59. 2.) Click on the region node and add a new :ref:`NavigationMesh<class_NavigationMesh>` Resource to the region node
  60. .. image:: img/nav_3d_min_setup_step1.png
  61. 3.) Add a new MeshInstance node as a child of the region node
  62. 4.) Select the meshinstance node and add a new PlaneMesh and increase the xy size to 10.
  63. 5.) Select the region node again and press the "Bake Navmesh" button on the top bar
  64. .. image:: img/nav_3d_min_setup_step2.png
  65. 7.) Now a transparent navigation mesh appeared that hovers some distance on top the planemesh.
  66. .. image:: img/nav_3d_min_setup_step3.png
  67. 8.) Add a CharacterBody3D below the region node with a basic collision shape and some mesh for visuals.
  68. 9.) Add a NavigationAgent3D node below the character node
  69. .. image:: img/nav_3d_min_setup_step4.png
  70. 10.) Add a script to the CharacterBody3D node with the following content.
  71. .. tabs::
  72. .. code-tab:: gdscript GDScript
  73. extends CharacterBody3D
  74. var movement_speed : float = 4.0
  75. @onready var navigation_agent = $NavigationAgent3D
  76. func set_movement_target(movement_target : Vector3):
  77. navigation_agent.set_target_location(movement_target)
  78. func _physics_process(delta):
  79. var current_agent_position : Vector3 = global_transform.origin
  80. var next_path_position : Vector3 = navigation_agent.get_next_location()
  81. var new_velocity : Vector3 = next_path_position - current_agent_position
  82. new_velocity = new_velocity.normalized()
  83. new_velocity = new_velocity * movement_speed
  84. set_velocity(new_velocity)
  85. move_and_slide()
  86. Set a movement target with the set_movement_target() function after the scene has fully loaded.
  87. Also add a Camera3D and some light and environment to see something.
  88. .. warning::
  89. On the first frame the NavigationServer map has not synchronised region data and any path query will return empty.
  90. Use ``await get_tree().physics_frame`` to pause scripts until the NavigationServer had time to sync.