NavMeshClamp errors on graph change

Hi!

I’m trying to use NavMeshClamp script to clamp my character to the Navmesh when using keyboard controls. It works quite well most of the time, but every once in a while it breaks down completely and prints an error message into the console on every frame.

The first case where i noticed this happens is when I open some door. This is likely because there is a NavMeshCut component on them and it modifies the graph. This error floods the console:

NullReferenceException: Object reference not set to an instance of an object
Pathfinding.NavmeshBase.Linecast (Pathfinding.NavmeshBase graph, UnityEngine.Vector3 origin, UnityEngine.Vector3 end, Pathfinding.GraphNode hint, Pathfinding.GraphHitInfo& hit, System.Collections.Generic.List`1[T] trace) (at Assets/AstarPathfindingProject/Generators/NavmeshBase.cs:1242)
Pathfinding.NavmeshBase.Linecast (UnityEngine.Vector3 origin, UnityEngine.Vector3 end, Pathfinding.GraphNode hint, Pathfinding.GraphHitInfo& hit) (at Assets/AstarPathfindingProject/Generators/NavmeshBase.cs:1057)
Pathfinding.NavmeshClamp.LateUpdate () (at Assets/AstarPathfindingProject/ExampleScenes/ExampleScripts/NavmeshClamp.cs:35)

Another case is when I teleport the character to a different graph:

IndexOutOfRangeException: Index was outside the bounds of the array.
Pathfinding.NavmeshTile.GetVertexInGraphSpace (System.Int32 index) (at Assets/AstarPathfindingProject/Generators/Utilities/NavmeshTile.cs:63)
Pathfinding.NavmeshBase.GetVertexInGraphSpace (System.Int32 index) (at Assets/AstarPathfindingProject/Generators/NavmeshBase.cs:176)
Pathfinding.TriangleMeshNode.GetVerticesInGraphSpace (Pathfinding.Int3& v0, Pathfinding.Int3& v1, Pathfinding.Int3& v2) (at Assets/AstarPathfindingProject/Generators/NodeClasses/TriangleMeshNode.cs:105)
Pathfinding.TriangleMeshNode.ClosestPointOnNodeXZInGraphSpace (UnityEngine.Vector3 p) (at Assets/AstarPathfindingProject/Generators/NodeClasses/TriangleMeshNode.cs:146)
Pathfinding.NavmeshBase.Linecast (Pathfinding.NavmeshBase graph, UnityEngine.Vector3 origin, UnityEngine.Vector3 end, Pathfinding.GraphNode hint, Pathfinding.GraphHitInfo& hit, System.Collections.Generic.List`1[T] trace) (at Assets/AstarPathfindingProject/Generators/NavmeshBase.cs:1183)
Pathfinding.NavmeshBase.Linecast (UnityEngine.Vector3 origin, UnityEngine.Vector3 end, Pathfinding.GraphNode hint, Pathfinding.GraphHitInfo& hit) (at Assets/AstarPathfindingProject/Generators/NavmeshBase.cs:1057)
Pathfinding.NavmeshClamp.LateUpdate () (at Assets/AstarPathfindingProject/ExampleScenes/ExampleScripts/NavmeshClamp.cs:35)

Both these errors go away when I change the line 31 in NavmeshClamp script:
var graph = AstarData.GetGraph(prevNode) as IRaycastableGraph;
to
var graph = AstarData.active as IRaycastableGraph;
but it also stops clamping the character correctly to the Navmesh so its not really a fix :smile:

I have also tried looking in the forums and found a few suggestions how to do the navmesh clamping, for example:
transform.position = AstarPath.active.GetNearest(transform.position, NNConstraint.Default).position

This works well on a perfectly flat terrain, but any slopes make the character wiggle vertically a bit. I assume this is because of the difference of height (Y coords) between the navmesh mesh and the actual ground.

Is there some other way (or a fix I can apply to the NavMeshClamp script) I can make navmesh clamping work correctly even with navmesh cuts and teleporting between graphs?

Thanks,
Peter

Hi

Which version of the package are you using?

I’m using version “4.2.15” of A* (Pro package). Unity is on version 2019.4.16f .

Ah, I think it will not work well if you change to a different graph.

To fix it, I think you can change this line:

if (graph.Linecast(prevPos, transform.position, prevNode, out hit)) {

to

if (graph.Linecast(prevPos, transform.position, null, out hit)) {

Yeah seems that helped, thanks a lot!

1 Like

Great. The fix will also be included in the next beta version.