I added my A* pathfinding algorithm to my enemies. This system is based on my waypoint system that I created in the previous updates of my project. I had issues with the correct pathfinding due to a small error in my script which overwrote my original path, but once that was corrected the script ran just like it is designed to.
Presented here is a small part of the pathfinding algorithm method, this is the method that will keep going through all the waypoints until the goal is found OR there are no waypoints left to use.
To know about this part of code:
- openList, visitedList and reachList are all lists which keep track of the pathfinding algorithm items. openList is used to keep track of the still to process waypoints specifically, visitedList contains all the waypoints that we already visited and are not usable anymore. reachLine is the only List that is used merely for the end result of our algorithm, it contains the final path or stays empty when no path is found.
- I reverse the reachList before returning is to set the List correctly that my unit can directly use this List for following the path.
- CheckPath(Waypoint wp, float newCost, Waypoint parent) checks if the waypoint wp is a path which is worth to follow or not
- ContinuePath(Waypoint destination) is a looping method, but has two exits by returning. The first return is triggered when we don’t have any new waypoints we can test (and as an extra safekeeper when foundGoal is true) and the second is triggered when we found our goal and created our path to follow.