Stuck with A* Pathfinding Script

I’ve posted this question on Stack Exchange, but I’m not getting much help. Hopefully there’s a pathfinding guru somewhere on this forum who can help me out! The question:

I’m using Sebastian Lagues’ pathfinding system found in these tutorials: A* Pathfinding Tutorial (Unity) - YouTube.

I’ve started extending his code for a dynamic RTS type game, but I’ve run into a problem. The units do not land on the target node, but one node before. This is problematic as for my game I need the units to land directly on the target node.

I’m usually happy to solve problems alone, but I don’t even know where to begin fixing this. The project is found here (episode 5): GitHub - SebLague/Pathfinding

Thanks in advance for any help or suggestions.

I’ve not downloaded the project to try, but had quick look at the source on github and there’s one thing that jumps out at me as a likely cause, your retrace function.

Vector3[] RetracePath(Node startNode, Node endNode) {
		List<Node> path = new List<Node>();
		Node currentNode = endNode;
		
		while (currentNode != startNode) {
			path.Add(currentNode);
			currentNode = currentNode.parent;
		}
		Vector3[] waypoints = SimplifyPath(path);
		Array.Reverse(waypoints);
		return waypoints;
		
	}

Notice that once currentNode == startNode you break out of the while loop, but in doing so do not then add the currentNode to the path, meaning that “startNode” never makes it into the list. so once reversed you would be 1 node short.

Try adding a path.Add(CurrentNode);after your while loop before you simplify your path, and see if that sorts it out, unless that node is being added to the list elsewhere and I missed it, this should solve the problem.

Success! ok so I had a look over it, had to take a few parts out one by one to work out where the issue was coming from, I’ve found it!.

In the Simplify path method you start your for loop from index 1 (so that you can always do i-1 to i for direction). However because of this, path[0] never makes it through the simplify stage, and always get culled out of the list. So all i did was update the Simplify method add path[0] to the waypoints list just before the for loop starts up.

(Note I did still have the previous addition mentioned in my answer, I’m unsure why this causes you issues, but in any case it does not appear to make a difference to the final target location).

just put -1 on the path index :3