使用 NavigationLink
NavigationLink 即导航链接,可以将相隔任意距离的导航网格多边形连接起来参与寻路,多边形来自 NavigationRegion2D 和 NavigationRegion3D。
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`以了解有关如何连接导航网格的更多信息。
NavigationLinks与NavigationRegions共享许多属性,例如 navigation_layers
。与使用导览网格资源新增更多本地可走访区域的NavigationRegions相比,NavigationLink在任意距离的两个位置之间新增单一联机。
NavigationLinks 有一个 start_position
和 end_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” 在编辑器中都有调试标记。位置的可见半径显示多边形搜索半径。将比较内部的所有导航网格多边形,并为边连接拾取最近的多边形。如果在搜索半径内未找到有效的多边形,则导航链接将被禁用。
可以在 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
extends Node2D
var link_rid: RID
var link_start_position: Vector2
var link_end_position: Vector2
func _ready() -> void:
link_rid = NavigationServer2D.link_create()
var link_owner_id: int = get_instance_id()
var link_enter_cost: float = 1.0
var link_travel_cost: float = 1.0
var link_navigation_layers: int = 1
var link_bidirectional: bool = true
NavigationServer2D.link_set_owner_id(link_rid, link_owner_id)
NavigationServer2D.link_set_enter_cost(link_rid, link_enter_cost)
NavigationServer2D.link_set_travel_cost(link_rid, link_travel_cost)
NavigationServer2D.link_set_navigation_layers(link_rid, link_navigation_layers)
NavigationServer2D.link_set_bidirectional(link_rid, link_bidirectional)
# Enable the link and set it to the default navigation map.
NavigationServer2D.link_set_enabled(link_rid, true)
NavigationServer2D.link_set_map(link_rid, get_world_2d().get_navigation_map())
# Move the 2 link positions to their intended global positions.
NavigationServer2D.link_set_start_position(link_rid, link_start_position)
NavigationServer2D.link_set_end_position(link_rid, link_end_position)
extends Node3D
var link_rid: RID
var link_start_position: Vector3
var link_end_position: Vector3
func _ready() -> void:
link_rid = NavigationServer3D.link_create()
var link_owner_id: int = get_instance_id()
var link_enter_cost: float = 1.0
var link_travel_cost: float = 1.0
var link_navigation_layers: int = 1
var link_bidirectional: bool = true
NavigationServer3D.link_set_owner_id(link_rid, link_owner_id)
NavigationServer3D.link_set_enter_cost(link_rid, link_enter_cost)
NavigationServer3D.link_set_travel_cost(link_rid, link_travel_cost)
NavigationServer3D.link_set_navigation_layers(link_rid, link_navigation_layers)
NavigationServer3D.link_set_bidirectional(link_rid, link_bidirectional)
# Enable the link and set it to the default navigation map.
NavigationServer3D.link_set_enabled(link_rid, true)
NavigationServer3D.link_set_map(link_rid, get_world_3d().get_navigation_map())
# Move the 2 link positions to their intended global positions.
NavigationServer3D.link_set_start_position(link_rid, link_start_position)
NavigationServer3D.link_set_end_position(link_rid, link_end_position)