Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

顯示導覽

NavigationLayers 是一項可選功能,用於進一步控制在路徑查詢中考慮哪些導覽網格以及可以連接哪些區域。它們的工作方式類似於物理層如何控制碰撞物件之間的碰撞或視覺層如何控制渲染到視窗的內容。

NavigationLayers can be named in the ProjectSettings the same as physics layers or visual layers.

../../_images/navigationlayers_naming.png

If two regions have not a single compatible layer they will not be merged by the NavigationServer. See Connecting navigation meshes for more information on merging navigation meshes.

如果一個區域沒有一個與路徑查詢的「navigation_layers」參數相容的導覽層,則在尋路時將跳過該區域的導覽網格。有關查詢導覽伺服器路徑的更多信息,請參閱 doc_navigation_using_navigationpaths 。

NavigationLayers are a single int value that is used as a bitmask. Many navigation related nodes have set_navigation_layer_value() and get_navigation_layer_value() functions to set and get a layer number directly without the need for more complex bitwise operations.

In scripts the following helper functions can be used to work with the navigation_layers bitmask.

func change_layers():
    var region: NavigationRegion3D = get_node("NavigationRegion3D")
    # enables 4-th layer for this region
    region.navigation_layers = enable_bitmask_inx(region.navigation_layers, 4)
    # disables 1-rst layer for this region
    region.navigation_layers = disable_bitmask_inx(region.navigation_layers, 1)

    var agent: NavigationAgent3D = get_node("NavigationAgent3D")
    # make future path queries of this agent ignore regions with 4-th layer
    agent.navigation_layers = disable_bitmask_inx(agent.navigation_layers, 4)

    var path_query_navigation_layers: int = 0
    path_query_navigation_layers = enable_bitmask_inx(path_query_navigation_layers, 2)
    # get a path that only considers 2-nd layer regions
    var path: PoolVector3Array = NavigationServer3D.map_get_path(
        map,
        start_position,
        target_position,
        true,
        path_query_navigation_layers
        )

static func is_bitmask_inx_enabled(_bitmask: int, _index: int) -> bool:
    return _bitmask & (1 << _index) != 0

static func enable_bitmask_inx(_bitmask: int, _index: int) -> int:
    return _bitmask | (1 << _index)

static func disable_bitmask_inx(_bitmask: int, _index: int) -> int:
    return _bitmask & ~(1 << _index)

更改路徑查詢的導覽層是啟用/停用整個導覽區域的一種效能友善的替代方案。與區域變更相比,具有不同導覽層的導覽路徑查詢不會觸發導覽伺服器上的大規模更新。

Changing the navigation layers of NavigationAgent nodes will have an immediate effect on the next path query. Changing the navigation layers of regions will have an immediate effect on the region but any new region connect or disconnect will only be in effect after the next physics frame.