I am trying to write a routine to find the walkable node next to a selected non-walkable node, but closest in the direction to the player, not the point clicked within the non-walkable node.
What I attempted was to get the node that was clicked, then send that node to a routine to get the connecting nodes, iterate through them, and check the distance from each of the connections to the player, and return the node with the shortest distance.
However, GetConnections doesn’t appear to be returning any connections at all.
Here is the routine I wrote. Can anyone see what I might be doing wrong, or have a better way to accomplish what I am trying to do?
Any help is appreciated.
private GraphNode GetNearestWalkableNode(GraphNode ClickedNode)
{
GraphNode closestNode = null;
float closestDistance = 9999999;
float dist;
GridNode gnode = ClickedNode as GridNode; // Will succeed for GridGraphs
gnode.GetConnections(otherNode =>
{
dist = Vector2.Distance((Vector3)otherNode.position, GameManager.Instance.Player.transform.position);
if (dist < closestDistance)
{
closestDistance = dist;
closestNode = otherNode;
}
});
return closestNode;
}
Example of what I am trying to do. In the image below, P represents the player. The red block is the unwalkable node that was clicked. The black area in the red block indicates where I clicked. The GetNearest
search for the nearest walkable node will send the player around the red block to the blue ares in the picture, the node closest to the point that was clicked. I want the player to end up in the green area of the picture, no matter where I click in the red block.
Thanks,
-Larry