Check if object can be reached

I have an enemy that shoots rocks at the player. When it runs out of rocks, it finds the nearest rock on the ground and sets it as its pathfinding target and goes to pick it up.

I have dynamic grid obstacles that move around so I have encountered situations in which a rock is caught between obstacles and the enemy can no longer reach it.

I want the enemy to be able to search for the nearest rock that it can reach.

I’ve been digging through the documentation but can’t figure this one out.

How can I check if an object is reachable?


You can check if an object is reachable using PathUtilities.IsPathPossible
GraphNode a = ( transform.position, NNConstraint.Default ).node;

GraphNode b = ( someObject.position, NNConstraint.Default ).node;

if ( PathUtilities.IsPathPossible (a,b) ) {
Debug.Log (“We can reach it!”);

You might want to check out the MultiTargetPath which can find the closest of a number of possible targets:

Thank you!
I’m using javascript for my game, so I had to modify what you posted a bit.
For anybody that needs this to work in javascript, this is what I ended up using that worked, where aStarPath is the AstarPath script in my scene.

var enemyNode = ( transform.position).node;
var rockNode = ( rock.transform.position).node;
if(Pathfinding.PathUtilities.IsPathPossible(archerNode, rockNode))
print(“rock found”);

Does this look sound?