Trying to Make a wanderAI using AIpath

The problem is The agent always gets stuck at the edge of a none-walkable. Its is just inside the walkable area but not enough to register path complete. Cant seam to figure out the fix. Here is the code:

public void Wander(float range)
{
animator.SetBool(“Walking”, true);
if (!ai.pathPending && (!ai.hasPath))
{

        ai.destination = PickRandomPointV3(transform, range);
        if(ai.hasPath)
            animator.SetBool("Walking", true);
        
       
    }
   
    if (ai.reachedEndOfPath)
    {
       

        ai.destination = PickRandomPointV3(transform, range);

        if (ai.hasPath)
            animator.SetBool("Walking", true);
        
    }
}

public Vector3 PickRandomPointV3(Transform trans, float radius)
{
    GraphNode nodeCurr = AstarPath.active.GetNearest(trans.position).node;
    Vector3 point;
    GraphNode nodeDest;
    float randomRadius;
    float rotationZ;
    Quaternion direction;
    
    do
    {

        randomRadius = Random.Range(radius / 2, radius);
        rotationZ = Random.Range(0, 360);
        direction = Quaternion.Euler(0.0f, 0.0f, rotationZ);
        point = trans.position + ((direction * trans.up) * randomRadius);

        nodeDest = AstarPath.active.GetNearest(point).node;
    } while (!nodeDest.Walkable && !PathUtilities.IsPathPossible(nodeCurr, nodeDest));


        return (Vector3)nodeDest.position;

}

Here is the agent stuck at the edge of the walkable area:

OK so I found the agent is has picked a node that is the other side of that wall. So this means either PathUtilities.IsPathPossible is not working or I am missunderstanding how it works.

If you check the implementation of IsPathPossible it actually only does two simple checks for ‘potential’ reachability. If your target is not reachable based on tags, weights or other similar means that hinder movement in between, only a real path calculation can determine if it is actually reachable.

But the red dots in the picture above are non-walkable nodes. It isnt possible for the agent to get to the other side of them yet it is still trying to. This mean IsPathPossible isnt working right?

When picking the location point to wander I am trying this code:
var nodeCurr = AstarPath.active.GetNearest(trans.position).node;
List reachableNodes = PathUtilities.GetReachableNodes(nodeCurr);
return (Vector3)reachableNodes[UnityEngine.Random.Range(0, reachableNodes.Count)].position;

but this causes Unity to eventually crash after 4 or 5 agents. Each agent causes huge lag. Also it returns some error in the console saying the index variable cant be negative. I think this is a variable in the parol script of A* pathfinding.

Can anyone help me with this?

Hi

Assuming you are not using tags then IsPathPossible should work well.

Make sure the destination actually lies on the navmesh, not outside it. Otherwise the character will never be able to reach it.

Im using grid Graph. Is Navmesh better? Also I am getting this error:

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <437ba245d8404784b9fbab9b439ac908>:0)

on this code:

public override Vector3 PickRandomPointV3(Transform trans, float radius)
{
GraphNode nodeCurr;
if (inNest)
{
nodeCurr = AstarPath.active.GetNearest(trans.position).node;
List reachableNodes = PathUtilities.GetReachableNodes(nodeCurr);
return (Vector3)reachableNodes[UnityEngine.Random.Range(0, reachableNodes.Count)].position;
}

    //return base.PickRandomPointV3(trans, radius);
    
    nodeCurr = AstarPath.active.GetNearest(trans.position).node;
    Vector3 point;
    GraphNode nodeDest;
    float randomRadius;
    float rotationZ;
    Quaternion direction;

    do
    {

        randomRadius = Random.Range(radius / 2, radius);
        rotationZ = Random.Range(0, 360);
        direction = Quaternion.Euler(0.0f, 0.0f, rotationZ);
        point = trans.position + ((direction * trans.up) * randomRadius);

        nodeDest = AstarPath.active.GetNearest(point).node;
        //List<GraphNode> reachableNodes = PathUtilities.GetReachableNodes(nodeCurr);
    } while (!PathUtilities.IsPathPossible(nodeCurr, nodeDest));


    return (Vector3)nodeDest.position;
    

}

Note that Random.Range returns values up to and including max.

i changed this line to :slight_smile:

return (Vector3)reachableNodes[UnityEngine.Random.Range(0, reachableNodes.Count-1)].position;

but i still get this error.

do
{

        randomRadius = Random.Range(radius / 2, radius);
        rotationZ = Random.Range(0, 360);
        direction = Quaternion.Euler(0.0f, 0.0f, rotationZ);
        point = trans.position + ((direction * trans.up) * randomRadius);

        nodeDest = AstarPath.active.GetNearest(point).node;
        
    } while (!nodeDest.Walkable && !PathUtilities.IsPathPossible(nodeCurr, nodeDest));
    Debug.Log("current path possible: " + PathUtilities.IsPathPossible(nodeCurr, nodeDest) + " is is walkable: " + nodeDest.Walkable);

surely this should never print false to the console but i am getting false readings. Am i missunderstanding how a do-while loop works? Any help is much appreciated.

Note that Random.Range returns values up to and including max.

Not for the int version of Random.Range() - the max is exclusive there.

I cant see how IsPathPossible is working. I am not using tags just layers which I have added to the GridGraph. It is making the non walkable nodes red. The agentAI is accepting the location the other side of the red nodes which it cant get to after a isPathPossible check. I am a fairly new programmer but either i am missing something or IsPathPossible isnt working with GridGraphs. Please does anyone have experience working with this that can help me. I have posted the code above.

Can anyone tell me if IsPathPossible works with grid-Graphs?

IsPathPossible does work for grid graphs.
It will return true if and only if there is a path between those two nodes that goes through only walkable nodes.
If something doesn’t work I’d recommend that you debug using e.g. Debug.DrawLine to visualize what parameters you are querying it with.

Hi BTippen,

There does not seem to be a function to direct message you in this forum?
I have the same issue of blue circle appearing on the other side of red unwalkable nodes outside of the grid graph
Are you able to solve the wander issue on a grid graph in the end?

Will appreciate it if you share with us your solution.
Thanks.