Support Forum

Failing to find a path in distance of an Object

Hey

I’ve overrided the AIPath so my agents find paths depending on that if they have a target(which they interact with - like attack etc). Here’s the code of SearchPath():

        public override void SearchPath()
        {
            if (float.IsPositiveInfinity(destination.x) || pObj.isDead()) return;
            if (onSearchPath != null) onSearchPath();

            lastRepath = Time.time;
            if(true) //Nie wypierdalać
            {
                waitingForPathCalculation = true;
                ////if (!order)
                    seeker.CancelCurrentPathRequest();
            }
            canMove = true;

            shouldSearch = true;
            if (pObj.target != null)
            {

                if ((transform.position - destination).magnitude < pObj.person.activeInteraction.activationDistance)
                {
                    canMove = false;
                    shouldSearch = false;
                }
                else
                {
                    float maxDistance = pObj.person.activeInteraction.activationDistance * 0.7f;
                    seeker.startEndModifier.exactEndPoint = StartEndModifier.Exactness.Original;
                    customPath = XPath.Construct(tr.position + tr.up * (rvo.center - rvo.height * 0.5f), destination);
                    customPath.endingCondition = new EndingConditionProximity(customPath, maxDistance);
                }
            }
            else
            {
                seeker.startEndModifier.exactEndPoint = StartEndModifier.Exactness.Original;
                customPath = XPath.Construct(tr.position + tr.up * (rvo.center - rvo.height * 0.5f), destination);
                if (destination == transform.position)
                    shouldSearch = false;
            }

            if (shouldSearch)
            {
                seeker.StartPath(customPath);
				if (order)
                {
					AstarPath.BlockUntilCalculated(customPath);
                    if (clickParticle != null) Destroy(clickParticle);
					if (customPath.vectorPath.Count > 0)
                    {
                        Debug.LogError("ICG.Testing - Path was found");

                        Vector3 InstantiationPosition = customPath.vectorPath[customPath.vectorPath.Count - 1];
						clickParticle = Instantiate(CGC.i.PrefabsManager.Particles.ClickedOnMap, InstantiationPosition + Vector3.up * 0.5f, Quaternion.identity);
						correctPath = customPath.vectorPath;
                    }
                    else
                    {
                        Debug.LogError("ICG.Testing - Path wasnt found");
                        clickParticle = Instantiate(CGC.i.PrefabsManager.Particles.ClickedOnMap, destination + Vector3.up * 0.5f, Quaternion.identity);
                    }

					movementPathController.DrawMovementPath (correctPath);

                    order = false;

                }

            }

        }

The problem is that when the agent has pObj.target and he’s trying to find a path to place around the target, it often does not. I’m getting warning:

Path Failed : Computation Time 2.00 ms Searched Nodes 2003
Error: Searched whole area but could not find target
Path Number 3361 (unique id)
UnityEngine.Debug:LogWarning(Object)
AstarPath:LogPathResults(Path) (at Assets/AstarPathfindingProject/Core/AstarPath.cs:745)
AstarPath:m__2(Path) (at Assets/AstarPathfindingProject/Core/AstarPath.cs:1147)
Pathfinding.PathProcessor:CalculatePathsThreaded(PathHandler) (at Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs:358)
Pathfinding.c__AnonStorey1:<>m__0() (at Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs:95)

I’m using recast graph if that’s important.
Does anyone know how to solve this?

Okey, the problem was that I was using Xpath endingcondition instead of ABPath endingcondition. :wink:

I’m facing a similar issue, in that it always prints the same ‘Searched whole area but could not find target’ for xPath with proximity condition on a recast graph. I suspect it’s because it needs alternative nodes in range to work, but that’s just speculation.
The answer in this thread suggests that there might be another solution, but i don’t really understand its description (only xpath seems to have an endingcondition, how would i create an ABPath ending condition for proximity and use that instead?).

Any hints on how i might interpret this answer as a solution to my problem?

Hi

That error message means that the path searched all possible nodes it could reach from the start node, but none of those nodes were deemed acceptable by the ending condition. Most likely this means that your ending condition range needs to be expanded. Using the ProximityCondition on a recast graph is a bit tricky since nodes can be very large in that graph. The ProximityEndingCondition only checks the distance to the node center, however for a recast graph it might be more appropriate to check the distance to the closet point on the node (though this has a bit of a performance cost):

/** Ending condition which stops a fixed distance from the target point */
public class EndingConditionProximityRecast : ABPathEndingCondition {
    /** Maximum world distance to the target node before terminating the path */
    public float maxDistance = 10;

    public EndingConditionProximityRecast (ABPath p, float maxDistance) : base(p) {
        this.maxDistance = maxDistance;
    }

    public override bool TargetFound (PathNode node) {
        var tnode = node.node as TriangleMeshNode;
        return tnode != null && (tnode.ClosestPointOnNode(abPath.originalEndPoint) - abPath.originalEndPoint).sqrMagnitude <= maxDistance*maxDistance;
    }
}

I had hoped there would be a cheaper way that could make use of some implicitly calculated heuristic values or something like that. But with this custom condition, i can imagine it even being a little cheaper to just manually compare distance with the moving object instead of doing that on every path recalculation for the full paths. But thanks for answering the question.