navigation_using_navigationlinks.rst 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 have overlapping edges or edges that are within navigation map ``edge_connection_margin``.
  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. A navigation link does not provide any specialized movement through the link. Instead, when
  33. an agent reaches the position of a link, game code needs to react (e.g. through area triggers)
  34. and provide means for the agent to move through the link to end up at the links other position
  35. (e.g. through teleport or animation). Without that an agent will attempt to move itself along
  36. the path of the link. You could end up with an agent walking over a bottomless pit instead of
  37. waiting for a moving platform, or walking through a teleporter and proceeding through a wall.
  38. Navigation link script templates
  39. --------------------------------
  40. The following script uses the NavigationServer to create a new navigation link.
  41. .. tabs::
  42. .. code-tab:: gdscript 2D GDScript
  43. extends Node2D
  44. var link_rid: RID
  45. var link_start_position: Vector2
  46. var link_end_position: Vector2
  47. func _ready() -> void:
  48. link_rid = NavigationServer2D.link_create()
  49. var link_owner_id: int = get_instance_id()
  50. var link_enter_cost: float = 1.0
  51. var link_travel_cost: float = 1.0
  52. var link_navigation_layers: int = 1
  53. var link_bidirectional: bool = true
  54. NavigationServer2D.link_set_owner_id(link_rid, link_owner_id)
  55. NavigationServer2D.link_set_enter_cost(link_rid, link_enter_cost)
  56. NavigationServer2D.link_set_travel_cost(link_rid, link_travel_cost)
  57. NavigationServer2D.link_set_navigation_layers(link_rid, link_navigation_layers)
  58. NavigationServer2D.link_set_bidirectional(link_rid, link_bidirectional)
  59. # Enable the link and set it to the default navigation map.
  60. NavigationServer2D.link_set_enabled(link_rid, true)
  61. NavigationServer2D.link_set_map(link_rid, get_world_2d().get_navigation_map())
  62. # Move the 2 link positions to their intended global positions.
  63. NavigationServer2D.link_set_start_position(link_rid, link_start_position)
  64. NavigationServer2D.link_set_end_position(link_rid, link_end_position)
  65. .. code-tab:: csharp 2D C#
  66. using Godot;
  67. public partial class MyNode2D : Node2D
  68. {
  69. private Rid _linkRid;
  70. private Vector2 _linkStartPosition;
  71. private Vector2 _linkEndPosition;
  72. public override void _Ready()
  73. {
  74. _linkRid = NavigationServer2D.LinkCreate();
  75. ulong linkOwnerId = GetInstanceId();
  76. float linkEnterCost = 1.0f;
  77. float linkTravelCost = 1.0f;
  78. uint linkNavigationLayers = 1;
  79. bool linkBidirectional = true;
  80. NavigationServer2D.LinkSetOwnerId(_linkRid, linkOwnerId);
  81. NavigationServer2D.LinkSetEnterCost(_linkRid, linkEnterCost);
  82. NavigationServer2D.LinkSetTravelCost(_linkRid, linkTravelCost);
  83. NavigationServer2D.LinkSetNavigationLayers(_linkRid, linkNavigationLayers);
  84. NavigationServer2D.LinkSetBidirectional(_linkRid, linkBidirectional);
  85. // Enable the link and set it to the default navigation map.
  86. NavigationServer2D.LinkSetEnabled(_linkRid, true);
  87. NavigationServer2D.LinkSetMap(_linkRid, GetWorld2D().NavigationMap);
  88. // Move the 2 link positions to their intended global positions.
  89. NavigationServer2D.LinkSetStartPosition(_linkRid, _linkStartPosition);
  90. NavigationServer2D.LinkSetEndPosition(_linkRid, _linkEndPosition);
  91. }
  92. }
  93. .. code-tab:: gdscript 3D GDScript
  94. extends Node3D
  95. var link_rid: RID
  96. var link_start_position: Vector3
  97. var link_end_position: Vector3
  98. func _ready() -> void:
  99. link_rid = NavigationServer3D.link_create()
  100. var link_owner_id: int = get_instance_id()
  101. var link_enter_cost: float = 1.0
  102. var link_travel_cost: float = 1.0
  103. var link_navigation_layers: int = 1
  104. var link_bidirectional: bool = true
  105. NavigationServer3D.link_set_owner_id(link_rid, link_owner_id)
  106. NavigationServer3D.link_set_enter_cost(link_rid, link_enter_cost)
  107. NavigationServer3D.link_set_travel_cost(link_rid, link_travel_cost)
  108. NavigationServer3D.link_set_navigation_layers(link_rid, link_navigation_layers)
  109. NavigationServer3D.link_set_bidirectional(link_rid, link_bidirectional)
  110. # Enable the link and set it to the default navigation map.
  111. NavigationServer3D.link_set_enabled(link_rid, true)
  112. NavigationServer3D.link_set_map(link_rid, get_world_3d().get_navigation_map())
  113. # Move the 2 link positions to their intended global positions.
  114. NavigationServer3D.link_set_start_position(link_rid, link_start_position)
  115. NavigationServer3D.link_set_end_position(link_rid, link_end_position)
  116. .. code-tab:: csharp 3D C#
  117. using Godot;
  118. public partial class MyNode3D : Node3D
  119. {
  120. private Rid _linkRid;
  121. private Vector3 _linkStartPosition;
  122. private Vector3 _linkEndPosition;
  123. public override void _Ready()
  124. {
  125. _linkRid = NavigationServer3D.LinkCreate();
  126. ulong linkOwnerId = GetInstanceId();
  127. float linkEnterCost = 1.0f;
  128. float linkTravelCost = 1.0f;
  129. uint linkNavigationLayers = 1;
  130. bool linkBidirectional = true;
  131. NavigationServer3D.LinkSetOwnerId(_linkRid, linkOwnerId);
  132. NavigationServer3D.LinkSetEnterCost(_linkRid, linkEnterCost);
  133. NavigationServer3D.LinkSetTravelCost(_linkRid, linkTravelCost);
  134. NavigationServer3D.LinkSetNavigationLayers(_linkRid, linkNavigationLayers);
  135. NavigationServer3D.LinkSetBidirectional(_linkRid, linkBidirectional);
  136. // Enable the link and set it to the default navigation map.
  137. NavigationServer3D.LinkSetEnabled(_linkRid, true);
  138. NavigationServer3D.LinkSetMap(_linkRid, GetWorld3D().NavigationMap);
  139. // Move the 2 link positions to their intended global positions.
  140. NavigationServer3D.LinkSetStartPosition(_linkRid, _linkStartPosition);
  141. NavigationServer3D.LinkSetEndPosition(_linkRid, _linkEndPosition);
  142. }
  143. }