How can I generate a working NavigationMesh from code?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Reisflocke

Hello there!

I’m currently trying to implement path finding for my AI in my 3D Topw Down Game.
I created a custom level editor and now I wanne auto generate a working NavigationMesh.

My currnet approach (this code runs in the Editor via tool Script):

#SETUP (done in other parts of the script)
var floor_positions = [Vector2(1,3), Vector2(0, 4)] # and so on ...
var nav_mesh: ArrayMesh = load("something/something/nav_mesh.obj")

func create_node(node_new: Node, parent: Node=self) -> Node:
	parent.add_child(node_new)
	node_new.set_owner(get_tree().edited_scene_root)
	return node_new

#**GENERATION (the nav mesh generation part)**
		#generate nav mesh
	var st := SurfaceTool.new()
	for p in floor_positions:
			var mesh_transform: Transform = Transform(Basis(), Vector3(p.x, 0.0, p.y))
			st.append_from(nav_mesh, 0, mesh_transform)
	var navigation: Navigation = create_node(Navigation.new())
	navigation.hide()
	var navigation_mesh_inst: NavigationMeshInstance = create_node(NavigationMeshInstance.new(), navigation)
	navigation_mesh_inst.navmesh = NavigationMesh.new()
	navigation_mesh_inst.navmesh.create_from_mesh(st.commit())

Godot 3.2.2, the generated NavigationMesh + Scene

This code DOES generate a NavigationMesh, but once I try to test it with:
get_simple_path()
it returns:
What is the reason for this?

PS: if you need any more details on this complex question I’m happy to provide them :wink:

:bust_in_silhouette: Reply From: doggod
var targetPos = target.global_transform.origin
begin = nav.get_closest_point(self.get_translation())
end = nav.get_closest_point(target.get_translation())
if begin == end or begin.distance_to(end) < 1.5 or end == PreviousDest:
	target = targets[rand_range(0, targets.size() -1)]
	begin = nav.get_closest_point(self.get_translation())
	end = nav.get_closest_point(target.get_translation())
var p = nav.get_simple_path(begin, end)
path = Array(p)
path.invert()

ps:get_simple_path are local coordinates, and the point must be on the navigation grid