• Godot Help3D
  • Investigating the floating NavMesh agent behaviour and fixing it for good

I keep running into this problem constantly and it's highly irritating and I never really did get properly to the bottom of what eactly causes this issue when I experimented. It seems no matter what setup I try at times, the NavmeshAgent stubbornly likes to just float of into space to one degree or another. It's almost like it's calculated it's path on an entirely wrong axis but the agent never stops going up or floating off into space.

It would be great if people with more knowledge of the backend could comment on this, I followed a tutorial here in the hopes of grabbing some more simplified code compared to what I was dealing with pre-3.5. Pseudo code posted below as the other stuff I have on there is irrelevant to the navigation setup. One thing I haven't done is experimented with flat navmeshes as a comparison which might be an idea and simplified geometry but I think even with the terrain I've imported it shouldn't be that much of an issue unless I've set something up badly wrong.

onready var maleAdultNavigationTarget = get_parent().transform.origin

func _ready():
	maleAdultNavigationAgent.set_target_location(maleAdultNavigationTarget)
	
func _physics_process(delta):
	
	var next = maleAdultNavigationAgent.get_next_location()
	var velocity = (next - transform.origin).normalized() * speed * delta
	maleAdultNavigationAgent.set_velocity(velocity)

func _on_MaleAdultNavigationAgent_velocity_computed(safe_velocity):
	move_and_collide(safe_velocity)
  • Okay! I went away and did some more thinking and I have now come up with a successful work around to this incredibly annoying problem and the issue with the baking really does seem to be scale. In this instance it's how steep of a slope you make with the sculpting tool within blender and I think I've just about found the maximum height. The problem I had was with the type of game I was aiming for I wanted to have some big sweeping landscapes for you to fly through with the camera to really make you feel like a god looking down on your population and village so naturally I thought I could get away with scaling up the land and just leave everything else at the default scale.

    Wrong, the correct answer seems to be to make the island lower in scale and do some sneaky dev optical illusion trickery. Namely, making all the other in game assets smaller so that the navmesh baking all works correctly while still keeping that sense of scale. This might seem ridiculous but this looks like it's the only way to get the navmesh to bake correctly over the whole land and without breaking the accuracy

    With some minor adjustments to the position of the collider and mesh, it's now completely working and my villager can make my way up the hill fairly accurately. I'll show you the comparison between the hill and the island just for reference so that people don't struggle if they're trying to replicate all of this. There is some minor clipping happening with the slope when the villager walks up the hill but I believe that's just more of a typical problem with how meshes handle slopes in general rather than anything to do with the navmesh behaviour itself.

    I won't lie, I'm quite proud of myself for solving all of this lol but I do think that a better solution would be to remove the max slope limit so you don't have to do all this ridiculous tweaking in the first place.

    I hope this helps anyone who has potentially been massively struggling with the new navmesh setup like I have.

    TLDR Summary of this total nonsense:

    . If your navmesh isn't baking your landscape properly, lower your slopes and decrease the scale of your assets to maintain the sense of scale you're going for

    . Move navmesh agent collider above your navmesh and tweak the navmesh agent height offset to move both the mesh and the collider closer to the ground so that the navmesh agent can still reach it's destination without getting stuck in the baked navmesh if you offset the agent too far down it will get stuck on the slopes and be unable to reach the destination

    Edit: Extra note about the scale, thankfully it seems the scale doesn't actually effect the way the navmesh bakes like I thought it might, I'm doing proper level design now, but that eliminates a potential variable to deal with. It seems to be purely how much your mesh is at an angle whether or not the navmesh bakes properly.

Bumping, would genuinely like to see if we can find a proper answer to this problem, it should help others who keep running into this issue too.

LOL I just rage solved it, if the agent is going up infinitely it seems to be purely to do with the position of the collider. You have to adjust it downwards to account for the strange offset behaviour going on, it would be nice though to know exactly why this is.

Holy hell the collider position of a navmesh agent causes a lot of issues, the height values interfere with the pathfinding as well, I've at least gotten it to set a path to the correct position now but as you can see adjustments are still needed.

I'm wondering if now I need to potentially redesign my island with less steep slopes in order to help the navmesh instance fit better. I feel like I shouldn't have to do that though, surely the settings can be tweaked? This is why I've made a fresh thread on the topic, I'm wondering if the main culprit for my problems is the max slope only being 90 degrees, it won't let me go any higher than that.

Okay, I've done some more investigating and I think it is indeed the max slope setting only being 90 degrees maximum that's the issue. I put the cell size to 0.01 for maximum accuracy on the mesh and the height for 0.01 so there's barely any height offset to worry about.

I think the slopes are too steep for the engine, is it possible to change the maximum slope height to a higher number to test this? It explains why my shooter project is dealing with the navmesh baking fine and it's because the ramps aren't at a very high angle.

Dropping some extra notes here so I don't forget how I solved all of this because the info I looked up is very scattered, I have come up with what I view are some quite hacky work arounds to get the AI pathfinding working on steep slopes among other things and how to deal with not only the height issues but also path target distances are a problem as well.

. If you are having issues with slopes, increase the cell height and re-bake to get the navmesh over the mesh you are wanting to bake to

. Adjust the agent height offset accordingly to get the agent to the ground level

. If you run into jittering issues and the navmesh agent is unable to get to the navmesh target, increase the Target Desired Distance, apparently from what I've gleened off various tutorial and forum posts this is a fairly common issue where for whatever reason the agent can't reach the destination just because the distance is too low which causes it to jitter about and never finish the path however in typical fashion I've managed to break it so it somehow still jitters even when the target is reached I have yet to figure out why this is happening

. While the agent seems to be somewhat running fine on certain angled surfaces I think yet again the max slope variable is potentially causing issues I tried remedying the problem of going down and up really steep slopes by directly changing the transform.origin.y of the agent with a raycast and offsetting it but I think the max slope value is just generally interfering with the movement for that to work and I don't know if when going up it would necessarily work right either

Would like to talk to some of the backend devs about this potentially and see if they have any ideas.

Booleans now updating correctly on path finished, but still need to do tweaking on the agent behaviour, would very much like to investigate the max slope value.

Solved the jittering, hadn't made sure that the velocity of the agent was stopped at the end of the path, that's nice, means the code is largely complete now for all the basic movement, also added Look_at() to deal with the direction. I used the navigation finished signal and a custom boolean to handle this.

	if isNavMeshPathFinished == false:
		var next = maleAdultNavigationAgent.get_next_location()
		var velocity = (next - global_transform.origin).normalized() * speed * delta
		maleAdultNavigationAgent.set_velocity(velocity)
		look_at(Vector3(maleAdultNavigationTarget.x, maleAdultNavigationTarget.y, 0), Vector3.UP)
	elif isNavMeshPathFinished == true:
		maleAdultNavigationAgent.set_velocity(Vector3(0, 0, 0))

Well what do you know this code seems to work fine with absolute minimum path distancing as well how weird.

8 days later

I've done more digging into this issue again and it looks like I'm more reliably able to work around some issues. I hadn't realised that with the baking if the navmesh itself through the cell size isn't set low enough or to the absolute minimum you will end up with big gaps in the navmesh that are what prevent the agent from going up and down slopes and it's simply that the navmesh hasn't baked correctly.

Also, if the cell height isn't high enough over the mesh itself and if the mesh is particularly tall like mine is you won't get the results that you want in terms of baking accuracy. This is why I believe the collision shape itself need to be adjusted properly in order for the navmesh agent to even be able to move however there isn't much in the way of visual indication on if the agent is set at the correct height or not. I 'think' you're adjusting the collision position to roughly match the height of the cells which explains why I've had so many problems.

With this I've been able to get much more accurate results all of a sudden using the agent normally. However the mesh position is still an issue, I could live with the way the navmesh is as long as I can find a way to have the mesh go to the ground, will need to look at more tweaking again.

Note the third image and the difference between the profile of the navmesh and the mesh it's baked to. I had to move the collision shape above the villager a fair bit to make the agent work and I believe this is because of this offset I've had to do in order to bake the navmesh properly without any weird gaps showing due to slopes.

This is why I believe a fairly quick and straight forward fix would be to remove the limits of the max slope in order to make it so that the mesh can deal with slopes that steep potentially unless I'm badly missing something. This is why I would really like some input from people on my testing.

Edit: As an extra test to just confirm what I've suspected, I moved my villager extra high along with the collision shape in it's original place way above the navmesh, as expected, the villager now matches the cell height properly and moves along without any problems. That includes when dealing with slopes so I think I've done about all I can as a front end dev to see what's going on.

As another quick test, I had the agent walk up a steep slope this time just to double check I wasn't prematurely celebrating, the positioning all works correctly and the agent is finishing it's path. As explained previously, it seems to be how the navmesh bakes slopes that is giving me the most headaches.

I think I've finally cracked the height issue for good! However it does look like it will need some backend dev intervention in order to have some proper fixes to deal with this kind of problem unless somebody knows something that I don't.

If the cell height is too low, the calculations that the baking does will leave massive gaps in the mesh you've provided if the slopes are too steep on the mesh being baked to. However if you bake within the provided parameters that will give you the most accurate results and give you a proper navmesh your agent can walk on without too much tweaking.

I've managed to think of workarounds but they're a bit clunky and I think that if the devs removed some of the limits they have on the baking these workaround wouldn't be needed which is why I'm trying to kind of chase people up on this issue. You can increase the cell height to make the navmesh bake 'correctly' however this creates and extremely frustrating height offset which breaks the agent's movement if you place it on the ground as you'd expect because the collider is technically underneath the navmesh so no wonder it's not moving but at least I can explain that in detail now.

In order to fix this, you can move the collider up and it seems as long as it's above the navmesh the pathfinding will calculate correctly and is actually quite accurate. It's just that because of the previously mentioned height offset with the navmesh baking above the ground that ruins the height accuracy that you would otherwise get with normal settings. I didn't know this was what was happening, so I was getting extremely frustrated as none of the tweaks I was previously trying was working because I was incorrectly expecting the navmesh to just bake itself to the ground fairly accurately.

The next logical thing I was thinking of would be to simply have the villager mesh itself be moved further down either through a raycast or by moving the mesh down to account for the height offset but leaving the collision shape in the same place. This did actually work, to a certain extend and simply dragging the villager mesh further down it did go to the ground, however because of the height offset and the way the collision was following navmesh above the ground it resulted in the mesh frequently clipping through the ground and losing it's height accuracy.

In terms of keeping everything accurate, the best thing I can think of is maybe re-modelling the mesh entirely so it's not at such an angle that the baking works fine. However I feel like that takes away from the sort of aesthetic I'm looking for with regards to how I've designed my island and I would like to see if the back end devs could have a look at this in detail. I know they're pushing ahead with various features for 4.0 which I am looking forward to but it would be nice because especially for noobs who want to try what I'm doing it would prevent a lot of headaches in the future.

By the way, moving both the navmesh and collision shape way above the baked navmesh has the same result so again, the height of the baked navmesh cells seem to have been causing the majority of my problems.

I'm just posting again to explain in proper detail now that I understand what it is that I've been tweaking so much to get the agents to work correctly. I may try redesigning my island since that's a solution I can do myself and seeing how much of a difference that makes and if it does then that will confirm what I've been testing.

Okay! I went away and did some more thinking and I have now come up with a successful work around to this incredibly annoying problem and the issue with the baking really does seem to be scale. In this instance it's how steep of a slope you make with the sculpting tool within blender and I think I've just about found the maximum height. The problem I had was with the type of game I was aiming for I wanted to have some big sweeping landscapes for you to fly through with the camera to really make you feel like a god looking down on your population and village so naturally I thought I could get away with scaling up the land and just leave everything else at the default scale.

Wrong, the correct answer seems to be to make the island lower in scale and do some sneaky dev optical illusion trickery. Namely, making all the other in game assets smaller so that the navmesh baking all works correctly while still keeping that sense of scale. This might seem ridiculous but this looks like it's the only way to get the navmesh to bake correctly over the whole land and without breaking the accuracy

With some minor adjustments to the position of the collider and mesh, it's now completely working and my villager can make my way up the hill fairly accurately. I'll show you the comparison between the hill and the island just for reference so that people don't struggle if they're trying to replicate all of this. There is some minor clipping happening with the slope when the villager walks up the hill but I believe that's just more of a typical problem with how meshes handle slopes in general rather than anything to do with the navmesh behaviour itself.

I won't lie, I'm quite proud of myself for solving all of this lol but I do think that a better solution would be to remove the max slope limit so you don't have to do all this ridiculous tweaking in the first place.

I hope this helps anyone who has potentially been massively struggling with the new navmesh setup like I have.

TLDR Summary of this total nonsense:

. If your navmesh isn't baking your landscape properly, lower your slopes and decrease the scale of your assets to maintain the sense of scale you're going for

. Move navmesh agent collider above your navmesh and tweak the navmesh agent height offset to move both the mesh and the collider closer to the ground so that the navmesh agent can still reach it's destination without getting stuck in the baked navmesh if you offset the agent too far down it will get stuck on the slopes and be unable to reach the destination

Edit: Extra note about the scale, thankfully it seems the scale doesn't actually effect the way the navmesh bakes like I thought it might, I'm doing proper level design now, but that eliminates a potential variable to deal with. It seems to be purely how much your mesh is at an angle whether or not the navmesh bakes properly.