Having some problems with GetConnections

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.




When searching for paths on grid graphs, it does use a special case in this exact situation so that if you request a path from P to the black square, it will end up at the green square.

GetConnections on an unwalkable node will usually return that it has no connections, because it’s not possible to move over unwalkable nodes at all. You could get all nodes around a single node using something like this:

GridNode center = GetNearest(...) as GridNode;
var graph  = center.Graph as GridGraph;
List<GraphNode> = graph.GetNodesInRegion(new IntRect(center.XCoordinateInGrid - 1, center.ZCoordinateInGrid - 1, center.XCoordinateInGrid + 1, center.ZCoordinateInGrid + 1));

Thanks Aron!!

That worked for me. I was pulling my hair out on this, and I can’t afford to loose too much more hair :slight_smile:

I appreciate your help!

1 Like