Navigation in games is always a key feature often needed to be in place early on in development. Being a tower defence game, a pathfinding system needs to be defined to allow enemy units to go from the start point to the end point. Unreal has it's own navigation system, however, I decided to use my own. This allowed me to start building on my C++ knowledge from early on, and simply give me more control.
A design choice was made to have a dynamic pathing system vs a static path for the units follow. This was to give the pathfinding an actual use if it was made. One of the best pathfinding algorithms to find the shortest path from A to B is A*. A simple version of the algorithm was then implemented into the project, and a test scene created to show it's capability.
The implementation is based off three scripts. A grid script, a node script and a pathfinding script. The gird script defined a set boundary, and took a radius to loop through the world and creating nodes (a struct defining world position, grid position, pathfinding costs and whether it is walkable). The pathfinding algorithm then has a FindPath() function, that takes into two nodes, and following the algorithm calculates the shortest path to take avoiding obstacles.
Pictured below is the final result. In the test scene there are red obstacles are scattered onto a plane that define the unwalkable areas. There are then 14 green test units placed around the edge of the map. These test units store a reference to the pathfinding script and find a path from their location to a set target location. This returns an array of waypoints that defines the path units can follow. Units then follow this path, by looking at each waypoint and moving towards it until they reach their target destination.
Comentarios