1

我在godot中有一个tilemap,但是所有的tile都是障碍物并提供碰撞。只有没有任何瓷砖的单元格可以步行。我现在正在尝试通过 Navigation2D 节点添加导航。据我所知,没有办法告诉它“一切都可以步行,但不是这些瓷砖在哪里”(只能说“瓷砖的这一部分是可步行的”,但在我目前的设置中没有瓷砖步行空间)。

作为一种解决方法,我尝试将没有瓦片的每个单元格设置为“虚拟瓦片”,该“虚拟瓦片”可以使用以下代码完全步行:

func _ready():
    for x in size.x:
        for y in size.y:
            var cell = get_cell(x, y)
            if cell == -1:
                set_cell(x, y, WALKABLE)

但 Navigation2D 节点无法识别这些图块。如果我手动放置WALKABLE瓷砖,一切都会按预期工作。

我想我可能遇到了这个问题,需要打电话update_dirty_quadrants(),但这并不能解决问题。

我用 3.0.6stable、3.1Alpha2 和最近的提交 9a8569d(由 godotwild 提供)尝试了这个,结果总是一样的。

无论如何都可以使用 tilemaps 进行导航,而无需事先手动放置每个图块?

4

2 回答 2

2

对于将来遇到此问题的任何人,“虚拟导航图块”解决方案现在都可以使用(使用 Godot 3.2.3)。将此脚本放在有问题的瓷砖地图上:

extends TileMap

# Empty/invisible tile marked as completely walkable. The ID of the tile should correspond
# to the order in which it was created in the tileset editor.
export(int) var _nav_tile_id := 0

func _ready() -> void:
    # Find the bounds of the tilemap (there is no 'size' property available)
    var bounds_min := Vector2.ZERO
    var bounds_max := Vector2.ZERO
    for pos in get_used_cells():
        if pos.x < bounds_min.x:
            bounds_min.x = int(pos.x)
        elif pos.x > bounds_max.x:
            bounds_max.x = int(pos.x)
        if pos.y < bounds_min.y:
            bounds_min.y = int(pos.y)
        elif pos.y > bounds_max.y:
            bounds_max.y = int(pos.y)
    
    # Replace all empty tiles with the provided navigation tile
    for x in range(bounds_min.x, bounds_max.x):
        for y in range(bounds_min.y, bounds_max.y):
            if get_cell(x, y) == -1:
                set_cell(x, y, _nav_tile_id)

    # Force the navigation mesh to update immediately
    update_dirty_quadrants()
于 2020-09-20T15:00:39.467 回答
1

我在 autotiler 中找不到我的 tile 的 ID(在编辑器中检查时,选择了 tilemap,这些都显示相同的 imagename.png 0)。因此,我没有使用 ID,而是使用 Tileset 中可行走的空白瓷砖的坐标。代码与 LukeZaz' 相同,但将 set_cell 替换为:

set_cell(x, y, 0, false, false, false, Vector2(7,4)); 其中 Vector2(7,4) 是我的瓦片集中具有导航多边形的空白瓦片的坐标。(记住坐标从 0 开始)

(Godot 3.2.3.stable)

于 2021-02-18T11:42:21.297 回答