TErrain and navigation

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

Is the terrain plugin compatible with navigation mesh? Yesterday I tried to use it and it seems to be ignored when baking the NAvigationMeshInstance.

:bust_in_silhouette: Reply From: Michael Antkiewicz

I’ve had a lot of trouble with 3d navigation on the terrain. My agents won’t go up slopes, they take a bad route, and the navmesh slows everything down and causes stuttering. As soon as you bake the navmesh it stops working for me. If you still want to go down that route, generate the mesh from the terrain, then the navmesh from that. Make sure they are both under the navigation node. Follow this tutorial:https://www.youtube.com/watch?v=_urHlep2P84. Don’t bake it. It works for me if I do that.

If that method isn’t working for you, I’ve come up with a kind of half baked solution using astar and gridmap. Basically what I do is loop over the terrain mesh and generate a gridmap from the faces. Then disable terrain collision on your bots. Then use astar pathfinding over the gridmap you generated. The results aren’t great, but good enough for prototyping imo.

export var terrain_path := NodePath()
var terrain

export var grid_path := NodePath()
var grid

export var bounds := Vector3()

# stops the grid from stacking on top of each other
export var allow_overlap = false

var astar

func _enter_tree():
	#connect("pressed", self, "clicked")

func generate():
	terrain = get_node(terrain_path).mesh
	grid = get_node(grid_path)
	var faces = terrain.get_faces()
	for face in faces:
		if face.x < bounds.x and face.y < bounds.y and face.z < bounds.z:
			var cell_pos = grid.world_to_map(face)
			if !allow_overlap:
				var set = false
				if grid.get_cell_item(cell_pos.x, cell_pos.y+grid.cell_size.y, cell_pos.z) == grid.INVALID_CELL_ITEM:
					grid.set_cell_item(cell_pos.x, cell_pos.y, cell_pos.z, 0)
				grid.set_cell_item(cell_pos.x, cell_pos.y, cell_pos.z, 0)

It’s best you run this in the editor, as you can then edit the gridmap and erase where you don’t want them to go e.g. obstacles. I’m sure this isn’t the best way to do things, but it seems to be faster than using the navmesh, though I haven’t done any real tests.