使用 NavigationLink

../../_images/nav_navmesh_links.png

NavigationLink 即导航链接,可以将相隔任意距离的导航网格多边形连接起来参与寻路,多边形来自 NavigationRegion2DNavigationRegion3D

NavigationLink 也可以用来实现寻路过程中需要与游戏中的对象交互才能使用的捷径,例如梯子、跳板、传送门等。

2D和 3D版本的 NavigationJumplinks 节点分別可用作 NavigationLink2D。

Different NavigationRegions can connect their navigation meshes without the need for a NavigationLink as long as they have overlapping edges or edges that are within navigation map edge_connection_margin. As soon as the distance becomes too large, building valid connections becomes a problem - a problem that NavigationLinks can solve.

请参阅 :ref:`doc_navigation_using_navigationregions`以了解有关导航区域使用的更多信息。请参阅 :ref:`doc_navigation_connecting_navmesh`以了解有关如何连接导航网格的更多信息。

../../_images/nav_link_properties.png

NavigationLinks与NavigationRegions共享许多属性,例如 navigation_layers 。与使用导览网格资源新增更多本地可走访区域的NavigationRegions相比,NavigationLink在任意距离的两个位置之间新增单一联机。

NavigationLinks 有一个 start_positionend_position ,当启用 bidirectional 时,可以双向导航。当放置导航链接时,它会连接搜索半径内最靠近其 start_position (起始位置)和 end_position结束位置 )的导航网格多边形,以进行寻路。

多边形搜索半径可以在ProjectSettings的 navigation/2d_or_3d/default_link_connection_radius 下全局配置,也可以使用 NavigationServer.map_set_link_connection_radius() 函数为每个导航 map 单独设置。

“start_position” 和 “end_position” 在编辑器中都有调试标记。位置的可见半径显示多边形搜索半径。将比较内部的所有导航网格多边形,并为边连接拾取最近的多边形。如果在搜索半径内未找到有效的多边形,则导航链接将被禁用。

../../_images/nav_link_debug_visuals.png

可以在 debug/shapes/navigation 下的编辑器中更改链接调试视觉效果 ProjectSettings 。调试的可见性也可以在编辑器3D视口小工具菜单中进行控制。

A navigation link does not provide any specialized movement through the link. Instead, when an agent reaches the position of a link, game code needs to react (e.g. through area triggers) and provide means for the agent to move through the link to end up at the links other position (e.g. through teleport or animation). Without that an agent will attempt to move itself along the path of the link. You could end up with an agent walking over a bottomless pit instead of waiting for a moving platform, or walking through a teleporter and proceeding through a wall.

导航链接脚本模板

以下代码使用 NavigationServer 新建导航链接。

2D GDScript3D GDScript

  1. extends Node2D
  2. var link_rid: RID
  3. var link_start_position: Vector2
  4. var link_end_position: Vector2
  5. func _ready() -> void:
  6. link_rid = NavigationServer2D.link_create()
  7. var link_owner_id: int = get_instance_id()
  8. var link_enter_cost: float = 1.0
  9. var link_travel_cost: float = 1.0
  10. var link_navigation_layers: int = 1
  11. var link_bidirectional: bool = true
  12. NavigationServer2D.link_set_owner_id(link_rid, link_owner_id)
  13. NavigationServer2D.link_set_enter_cost(link_rid, link_enter_cost)
  14. NavigationServer2D.link_set_travel_cost(link_rid, link_travel_cost)
  15. NavigationServer2D.link_set_navigation_layers(link_rid, link_navigation_layers)
  16. NavigationServer2D.link_set_bidirectional(link_rid, link_bidirectional)
  17. # Enable the link and set it to the default navigation map.
  18. NavigationServer2D.link_set_enabled(link_rid, true)
  19. NavigationServer2D.link_set_map(link_rid, get_world_2d().get_navigation_map())
  20. # Move the 2 link positions to their intended global positions.
  21. NavigationServer2D.link_set_start_position(link_rid, link_start_position)
  22. NavigationServer2D.link_set_end_position(link_rid, link_end_position)
  1. extends Node3D
  2. var link_rid: RID
  3. var link_start_position: Vector3
  4. var link_end_position: Vector3
  5. func _ready() -> void:
  6. link_rid = NavigationServer3D.link_create()
  7. var link_owner_id: int = get_instance_id()
  8. var link_enter_cost: float = 1.0
  9. var link_travel_cost: float = 1.0
  10. var link_navigation_layers: int = 1
  11. var link_bidirectional: bool = true
  12. NavigationServer3D.link_set_owner_id(link_rid, link_owner_id)
  13. NavigationServer3D.link_set_enter_cost(link_rid, link_enter_cost)
  14. NavigationServer3D.link_set_travel_cost(link_rid, link_travel_cost)
  15. NavigationServer3D.link_set_navigation_layers(link_rid, link_navigation_layers)
  16. NavigationServer3D.link_set_bidirectional(link_rid, link_bidirectional)
  17. # Enable the link and set it to the default navigation map.
  18. NavigationServer3D.link_set_enabled(link_rid, true)
  19. NavigationServer3D.link_set_map(link_rid, get_world_3d().get_navigation_map())
  20. # Move the 2 link positions to their intended global positions.
  21. NavigationServer3D.link_set_start_position(link_rid, link_start_position)
  22. NavigationServer3D.link_set_end_position(link_rid, link_end_position)