Finding path to object reachable. Issues with GetNearest

I’m having a problem with start path which I use right now to figure out if an entity can reach a target or not. Everything works well except in cases where it has to path find to a entity on an unwalkable node which throws an error “Path Error The node closest to the end point is not walkable”. To get around that I use a ray trace get the collider hit point and then use GetNearest with the default NNConstraint on it to get a ‘walkable’ end point. However when I feed the position into the StartPath it still throws the same error.

Here is the code for reference:

if (targetVehicleController.stationary)
{
    Debug.Log("Found stationary target");
    LayerMask layerMask = 1 << target.layer;
    // Getall colliders
    RaycastHit [] rayResults = Physics.RaycastAll(vehicleController.transform.position,(target.transform.position - vehicleController.transform.position),layerMask);
    Collider targetCollider = target.GetComponent<Collider>();
#if WAR_DEBUG
    if (targetCollider == null)
    {
        Debug.LogWarning("Target Collider is null");
    }
#endif
    if (rayResults != null)
    {
        // Find colliders, check against worldcollider and get collison point
        foreach (RaycastHit raycastHit in rayResults)
        {
            if (raycastHit.collider == targetCollider)
            {
                Debug.Log("Got collider, getting nearest walkable");
                // Get Neartest walkable node position as targetPosition
                NNInfo nninfo = AstarPath.active.GetNearest(raycastHit.point,NNConstraint.Default);
                targetPosition = nninfo.clampedPosition;
                break;
            }
        }
    }
}
Path p = ABPath.Construct(vehicleController.transform.position, targetPosition,OnPathFound);
p.nnConstraint = NNConstraint.None;
_targetIndex[p] = target;
AstarPath.StartPath(p);

Hi

You might not want to use the clamped position since that will be the closest point on the node and thus it will likely be placed right at the edge between two nodes. Use

(Vector3)nninfo.node.Position

instead.

Another approach could be to search for a path and then when you have calculated it, check if the end point is within some distance threshold to the target.