首页 > 解决方案 > 导航瓷砖地图,无需手动放置可步行瓷砖

问题描述

我在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 进行导航,而无需事先手动放置每个图块?

标签: godot

解决方案


对于将来遇到此问题的任何人,“虚拟导航图块”解决方案现在都可以使用(使用 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()

推荐阅读