Tell seeker to wait until a possible path is calculated to start the path

Basically as the title says, I want the seeker to ignore paths that aren’t possible and ONLY choose one that it’s able to reach. Here’s my failed attempt:

using UnityEngine;
using System.Collections;
using Pathfinding;

public class WanderRandom : MonoBehaviour
{
    public float radius = 20;
    IAstarAI ai;

    void Start()
    {
        ai = GetComponent<IAstarAI>();
    }

    void PickRandomPoint(out Vector3 closestPointOnGraph, out bool possible, out Pathfinding.ABPath path)
    {
        var point = Random.insideUnitSphere * radius;
        var pointOnGraph = AstarPath.active.GetNearest(point, NNConstraint.Default);
        var node1 = AstarPath.active.GetNearest(transform.position).node;
        var node2 = AstarPath.active.GetNearest(point).node;
        possible = PathUtilities.IsPathPossible(node1, node2);
        var closestPoint = pointOnGraph.position;
        closestPoint.y = 0;
        closestPoint += ai.position;
        closestPointOnGraph = closestPoint;
        path = ABPath.Construct(transform.position, point);

    }


    void Update()
    {
        Vector3 i;
        bool a;
        Pathfinding.ABPath path;
        PickRandomPoint(out i, out a, out path);

        print(a);

        // Update the destination of the AI if
        // the AI is not already calculating a path and
        // the ai has reached the end of the path or it has no path at all
        if (!ai.pathPending && (ai.reachedEndOfPath || !ai.hasPath))
        {
            AstarPath.StartPath(path);
            path.BlockUntilCalculated();
            if(a){
                ai.destination = i;
                ai.SearchPath();
            }
          
        }
  //      var closestPointOnGraph = PickRandomPoint();
 //       var path = ABPath.Construct(transform.position,closestPointOnGraph);
 //       var pathLength = path.GetTotalLength();
  //      Debug.Log(pathLength);
        Debug.DrawLine(transform.position, ai.destination, Color.magenta);
    }
}

As it is, I’m able to see in the console that the seeker is pursuing paths that are impossible, and so he gets stuck while trying to pass through a wall. I should mention that this script draws from the WanderingAI tutorial and I’m using a grid graph.

I’m new to all of this so any help with this issue/guidance on improving my script would be incredible, thanks!

Hi

One approach could be to only calculate points that the agent is guaranteed to be able to reach. Take a look at the tutorial again, but avoid Method 1 as that one explicitly does not take reachability into account: https://arongranberg.com/astar/docs/wander.html

You do seem to be doing something strange in your approach though.
In particular these lines:

var closestPoint = pointOnGraph.position;
closestPoint.y = 0;
closestPoint += ai.position;
closestPointOnGraph = closestPoint;

That will add the x and z coordinates of the AI’s position and the position of the closest point on the graph which is probably not what you want. That point will end up at some large coordinate, probably in many cases even outside the map.