Setting Path Destination Off Grid Graph

I’m using A* to generate a path from the player position to a point clicked on the ground. Then the player follows that path to the end point (where the click occurred).

This works except when I click in an area not on the graph – in that case the path still gets generated, and the player ends up going to a spot not on the graph (which is not desired).

Ideally the path returned would only contain points on the grid graph (and it would try to get as close to the desired point as possible). In fact, this is what appears to happen if you use graph update to mark a region as inaccessible. Doing that for every obstacle is not practical for me though, so I was wondering if I’m missing some obvious thing here.

Note: I am using the AIPath module for this, which asks the Seeker for a path given an arbitrary start and end.

I figured this out – posting in case anyone else finds useful.

When determining the point to pathfind to, I pass the raycast hit to AstartPath.active.GetNearest(). If the constraint is set up to be walkable, then I’ll get the closest walkable node (or point?). I guess that is a question I still have, will GetNearest() return the closest node position, or point?

code:

		NNConstraint constraint = NNConstraint.Default;
		constraint.constrainWalkability = true;
		constraint.walkable = true;
		NNInfo info = AstarPath.active.GetNearest(hit.point, constraint);						
		m_MovementMarker.transform.position = info.clampedPosition;

GetNearest will return the closest node. However some graph generators (namely navmesh based ones, I will add support for grid graphs later) will fill in the NNInfo.clampedPosition to the closest point on the node (since navmesh graphs are built up from polygons, it would be filled in with the closest point on the closest polygon). Otherwise the clampedPosition will simply be the node’s centre (node.position).

PS: I think a better solution to your problem is to edit the setting for the StartEndModifier on the Seeker. More specifically, set End Point to ClosestOnNode.