NavmeshClamp moves character

I have a problem with NavmeshClamp which I hope someone can help me with. I’m trying to keep my characters constrained to a navmesh so I can avoid creating hundreds of collision objects around my level. They are using a navmesh to chase my player in a diablo style RPG game for IOS. Because this is mobile I’m not using the built in obstacle avoidance and instead wrote my own very simple avoidance using Physics.OverlapSphere instead of raycasts for better performance. However, this means my characters can run through walls when trying to avoid another character. Adding the NavmeshClamp script stops them leaving the navmesh, but they pop to other places on the edge of the Navmesh and can teleport around a bit. I guess this happens when they move slightly off the navmesh but end up closer to another edge of the mesh and get moved over to it.
Any thoughts on how to fix this?
Is there a simple way to just test if they are on the navmesh or not?


I haven’t used the NavmeshClamp script so much lately, it looks like it is more unstable than before… might be because of some linecast logic change I have made.

Anyway. I made a tiny change to the script to avoid floating point errors which are likely the major cause of instability right now.

It is a lot more stable as I test it now, only in some corners it oscillates a bit.
If it starts to teleport, you can try increasing the constant 0.008f I have in the code.

To test if an object is on the navmesh, you would do something like this:
NNInfo nn = (transform.position); Vector3 closest = nn.constClampedPosition; //Check distance, ignoring distance on the Y axis if (new Vector2(closest.x-transform.position.x,closest.z-transform.position.z).sqrMagnitude < 0.01f*0.01f) { // Or some other small margin // On the graph }

Thanks for the reply Aron, that seems to work much better now.
Saying that though, I found your small code example also worked in my case, so I’m now using,

`	public void LateUpdate()
		nninfo = (MyTransform.position);
		MyTransform.position = nninfo.clampedPosition;

I assume that is a little quicker and seems to keep them locked to the navmesh.


Yes that will also keep them locked on the navmesh. However it will fail if the character moves too much (i.e through a wall) and a node which should not be picked becomes the closest one. But keeping that in mind, that will work just fine.

Hm, maybe I should add an alternative mode to the NavmeshClamp script…