Godot Version
4.2.2
Question
I am moving a CharacterBody3D
with a NavigationAgend3D
. If i block off the path completely (i.e. with a big enough StaticBody3D
) my CharacterBody3D
walks up to that obstacle and stops.
Now i want the
StaticBody3D
to resume it’s way to the target location once that obstacle has been removed. However, if i remove the StaticBody3D
and its parent (MeshInstance3D
) and then call bake_navigation_mesh
the NavigationMesh
does not seem to be updated.
I checked and the nodes get deleted from the SceneTree. If I do this manually in the Editor the NavigationMesh
gets updated correctly.
This is my scene:
And this is the script
extends CharacterBody3D
var movement_speed: float = 2.0
var movement_target_position: Vector3 = Vector3(-10.0,0.0,10.0)
@onready var navigation_agent: NavigationAgent3D = $NavigationAgent3D
@onready var navigation_region_3d = $"../NavigationRegion3D"
func _ready():
# These values need to be adjusted for the actor's speed
# and the navigation layout.
navigation_agent.path_desired_distance = 0.5
navigation_agent.target_desired_distance = 0.5
# Make sure to not await during _ready.
call_deferred("actor_setup")
func actor_setup():
# Wait for the first physics frame so the NavigationServer can sync.
await get_tree().physics_frame
# Now that the navigation map is no longer empty, set the movement target.
set_movement_target(movement_target_position)
func set_movement_target(movement_target: Vector3):
navigation_agent.set_target_position(movement_target)
func _physics_process(delta):
if navigation_agent.is_navigation_finished():
return
var current_agent_position: Vector3 = global_position
var next_path_position: Vector3 = navigation_agent.get_next_path_position()
velocity = current_agent_position.direction_to(next_path_position) * movement_speed
move_and_slide()
for i in get_slide_collision_count():
var collision = get_slide_collision(i)
print("I collided with ", collision.get_collider().name)
var obstacle = collision.get_collider()
print(obstacle)
var mesh = obstacle.get_parent()
for n in mesh.get_children():
n.remove_child(n)
n.queue_free()
mesh.queue_free()
navigation_region_3d.bake_navigation_mesh(false)
print("Baked navigation mesh")
set_movement_target(movement_target_position)
And this is the scene without obstacle (removed and baked again in the editor):
What am i doing wrong? Or is there a better way to do this?