Pathfinding to a point within player FOV

Hello, I have a simple system in which an enemy is supposed to move into the player’s FOV before firing a projectile at them. I couldn’t find anything in the documentation that let me dynamically restrict what nodes to look for like that, so I ended up essentially using some vectors to create a kind of 3x3 squeezed grid in front of the player, and then plan to use RecastGraph’s GetNearest(position) to select a node within that grid for the enemy to path towards. I originally planned to use StartPath and a callback to check the nodes found at all 9 positions to see which one has the shortest path for the enemy to travel to, but then started wondering if using StartPath 9 times like that was actually super inefficient.

On top of that, before my enemy can move to a node, I also want to test that they’re even able to attack the player with a projectile from that position. My plan for that is to test the position with a capsule collider the same size as the enemy, and then use a spherecast from that to the player to detect whether a projectile would make it from that position to the player.

So my question is this: Is it more efficient to test all 9 found nodes with the projectile checker, and then find the shortest remaining ones with StartPath, or is it perfectly fine to use StartPath 9 times to build an ordered list of the closest nodes to the enemy, and then use the projectile test?

I’ve also run into a problem when using StartPath. I use SetPath in the callback function and pass in the path, but get an error saying the path calculation hasn’t been completed.

testAgent.GetComponent<Seeker>().StartPath(testAgent.transform.position, info.position, OnPathCalculated);

void OnPathCalculated(Path path) {
        if (path.error) return;

        testAgent.GetComponent<IAstarAI>().SetPath(path);
        // SetPath throws an exception, put this path should be calculated, or else this method wouldn't run
}

As the comment says, if it’s being called within the callback function, the path must already be calculated, right? testAgent still moves to info.position (which is an NNInfo variable), but am I doing something wrong?

Hi

You might be interested in a custom PathEndingCondition. See PathEndingCondition - A* Pathfinding Project
In the beta, this can be used with the normal ABPath, in the non-beta, it can only be used with the XPath path type.

I believe this is a bug. But it has been fixed in the beta iirc. Note that if you start the path using a seeker, the movement script will pick it up automatically anyway, so you shouldn’t need to call SetPath.