navigation_using_navigationlinks.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. .. _doc_navigation_using_navigationlinks:
  2. Using NavigationLinks
  3. =====================
  4. .. image:: img/nav_navmesh_links.png
  5. NavigationLinks are used to connect navigation mesh polygons from :ref:`NavigationRegion2D<class_NavigationRegion2D>`
  6. and :ref:`NavigationRegion3D<class_NavigationRegion3D>` over arbitrary distances for pathfinding.
  7. NavigationLinks are also used to consider movement shortcuts in pathfinding available through
  8. interacting with gameplay objects e.g. ladders, jump pads or teleports.
  9. 2D and 3D versions of NavigationJumplinks nodes are available as
  10. :ref:`NavigationLink2D<class_NavigationLink2D>` and
  11. :ref:`NavigationLink3D<class_NavigationLink3D>` respectively.
  12. Different NavigationRegions can connect their navigation meshes without the need for a NavigationLink
  13. as long as they are within navigation map ``edge_connection_margin`` and have compatible ``navigation_layers``.
  14. As soon as the distance becomes too large, building valid connections becomes a problem - a problem that NavigationLinks can solve.
  15. See :ref:`doc_navigation_using_navigationregions` to learn more about the use of navigation regions.
  16. See :ref:`doc_navigation_connecting_navmesh` to learn more about how to connect navigation meshes.
  17. .. image:: img/nav_link_properties.png
  18. NavigationLinks share many properties with NavigationRegions like ``navigation_layers``.
  19. NavigationLinks add a single connection between two positions over an arbitrary distance
  20. compared to NavigationRegions that add a more local traversable area with a navigation mesh resource.
  21. NavigationLinks have a ``start_position`` and ``end_position`` and can go in both directions when ``bidirectional`` is enabled.
  22. When placed a navigationlink connects the navigation mesh polygons closest to its ``start_position`` and ``end_position`` within search radius for pathfinding.
  23. The polygon search radius can be configured globally in the ProjectSettings under ``navigation/2d_or_3d/default_link_connection_radius``
  24. or set for each navigation **map** individually using the ``NavigationServer.map_set_link_connection_radius()`` function.
  25. Both ``start_position`` and ``end_position`` have debug markers in the Editor.
  26. The visible radius of a position shows the polygon search radius.
  27. All navigation mesh polygons inside are compared and the closest is picked for the edge connection.
  28. If no valid polygon is found within the search radius the navigation link gets disabled.
  29. .. image:: img/nav_link_debug_visuals.png
  30. The link debug visuals can be changed in the Editor :ref:`ProjectSettings<class_ProjectSettings>` under ``debug/shapes/navigation``.
  31. The visibility of the debug can also be controlled in the Editor 3D Viewport gizmo menu.
  32. .. note::
  33. NavigationLinks do not move agents between the two link positions by themselves.
  34. A navigation link does not provide any automated movement through the link. Instead, when
  35. an agent reaches the position of a link, game code needs to react (e.g. through area triggers) and provide means for the agent
  36. to move through the link to end up at the links other position (e.g. through teleport or animation) to continue along the path.
  37. Navigation link script templates
  38. --------------------------------
  39. The following script uses the NavigationServer to create a new navigation link.
  40. .. tabs::
  41. .. code-tab:: gdscript 2D GDScript
  42. extends Node2D
  43. var link_rid: RID
  44. var link_start_position: Vector2
  45. var link_end_position: Vector2
  46. func _ready() -> void:
  47. link_rid = NavigationServer2D.link_create()
  48. var link_owner_id: int = get_instance_id()
  49. var link_enter_cost: float = 1.0
  50. var link_travel_cost: float = 1.0
  51. var link_navigation_layers: int = 1
  52. var link_bidirectional: bool = true
  53. NavigationServer2D.link_set_owner_id(link_rid, link_owner_id)
  54. NavigationServer2D.link_set_enter_cost(link_rid, link_enter_cost)
  55. NavigationServer2D.link_set_travel_cost(link_rid, link_travel_cost)
  56. NavigationServer2D.link_set_navigation_layers(link_rid, link_navigation_layers)
  57. NavigationServer2D.link_set_bidirectional(link_rid, link_bidirectional)
  58. # Enable the link and set it to the default navigation map.
  59. NavigationServer2D.link_set_enabled(link_rid, true)
  60. NavigationServer2D.link_set_map(link_rid, get_world_2d().get_navigation_map())
  61. # Move the 2 link positions to their intended global positions.
  62. NavigationServer2D.link_set_start_position(link_rid, link_start_position)
  63. NavigationServer2D.link_set_end_position(link_rid, link_end_position)
  64. .. code-tab:: gdscript 3D GDScript
  65. extends Node3D
  66. var link_rid: RID
  67. var link_start_position: Vector3
  68. var link_end_position: Vector3
  69. func _ready() -> void:
  70. link_rid = NavigationServer3D.link_create()
  71. var link_owner_id: int = get_instance_id()
  72. var link_enter_cost: float = 1.0
  73. var link_travel_cost: float = 1.0
  74. var link_navigation_layers: int = 1
  75. var link_bidirectional: bool = true
  76. NavigationServer3D.link_set_owner_id(link_rid, link_owner_id)
  77. NavigationServer3D.link_set_enter_cost(link_rid, link_enter_cost)
  78. NavigationServer3D.link_set_travel_cost(link_rid, link_travel_cost)
  79. NavigationServer3D.link_set_navigation_layers(link_rid, link_navigation_layers)
  80. NavigationServer3D.link_set_bidirectional(link_rid, link_bidirectional)
  81. # Enable the link and set it to the default navigation map.
  82. NavigationServer3D.link_set_enabled(link_rid, true)
  83. NavigationServer3D.link_set_map(link_rid, get_world_3d().get_navigation_map())
  84. # Move the 2 link positions to their intended global positions.
  85. NavigationServer3D.link_set_start_position(link_rid, link_start_position)
  86. NavigationServer3D.link_set_end_position(link_rid, link_end_position)