Cyrille
Cyrille

Reputation: 14543

In Godot 4.3 (beta3), avoid obstacle in new TileMapLayer node

I don't understand how to handle obstacles (areas with physics layers) from TileMapsLayers with NavigationAgent2D path.

This is what I have so far.

I have a simple scene world with only 2 TileMapLayers.

enter image description here

enter image description here

enter image description here

In this example (it's supposed to be a tree in 2 tiles), only the bottom half of the sprite is covered. The upper half is not covered.

And I added a NavigationAgent2D for ennemis.

enter image description here

If I run the game, ennemies are stuck as soon as they hit the tree.

So, I tried to add a script in Island layer :

extends TileMapLayer

@onready var obstacles: TileMapLayer = $"../Obstacles"

func _use_tile_data_runtime_update(coords: Vector2i) -> bool:
    return coords in obstacles.get_used_cells_by_id(0)
    
func _tile_data_runtime_update(coords: Vector2i, tile_data: TileData) -> void:
    tile_data.set_navigation_polygon(0, null)

But with this script, all tiles containing the tiles from obstacle layer are fully removed.

I need to remove only the part with the physics polygon from the obstacle layer. In my tree example, I don't want to remove the upper half from navigation (ennemies should walk behind the upper part of tree). And sometime, I even want to remove a smaller part only of the tile from navigation.

OR, maybe, I need to tell my NavigationAgent2D to avoid all areas covered by a physics layer from this TileSet ?

What can I do now ? I'm new to Godot, so maybe I'm missing something obvious. If so, feel free to tell me.

Upvotes: 0

Views: 335

Answers (2)

Diederik Kroondijk
Diederik Kroondijk

Reputation: 11

I think you have an error in your update function

func _tile_data_runtime_update(coords: Vector2i, tile_data: TileData) -> void:
    tile_data.set_navigation_polygon(0, null)

This sets all of your navigation_polygons to null. You want to use the function you defined above to check if the current tile overlaps with an obstacle and if it does set the navigation polygon to null.

func _tile_data_runtime_update(coords: Vector2i, tile_data: TileData) -> void:
    if _use_tile_data_runtime_update(coords): 
        tile_data.set_navigation_polygon(0, null)

Upvotes: 0

Bryan
Bryan

Reputation: 3

try

func _use_tile_data_runtime_update(coords: Vector2i) -> bool:
    return coords in obstacles.get_used_cells()

that worked for me

Upvotes: 0

Related Questions