I just want to define obstacles and then get the desired path, but my simple code does not seem to work. It prints an empty path []
every frame.
extends Node2D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
call_deferred("test_path")
func test_path():
# create a new "obstacle" and place it on the default navigation map.
var new_obstacle_rid: RID = NavigationServer2D.obstacle_create()
var default_map_rid: RID = get_world_2d().get_navigation_map()
NavigationServer2D.obstacle_set_map(new_obstacle_rid, default_map_rid)
NavigationServer2D.obstacle_set_position(new_obstacle_rid, Vector2(0,0))
# Use obstacle static by adding a square that pushes agents out.
var outline = PackedVector2Array([Vector2(150, 150), Vector2(250, 150), Vector2(250, 250), Vector2(150,250)])
NavigationServer2D.obstacle_set_vertices(new_obstacle_rid, outline)
# Enable the obstacle.
NavigationServer2D.obstacle_set_avoidance_enabled(new_obstacle_rid, true)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func _physics_process(delta: float) -> void:
var default_map_rid: RID = get_world_2d().get_navigation_map()
print(NavigationServer2D.map_get_path(default_map_rid, Vector2(50, 50), Vector2(400, 400), false))