Hi! Not sure if this is the correct place to post this, but i was thinking that people in this sub-forum are most interested about this kind of stuff. Unreal engine is currently missing the working 2D-pathfinding algorithm. The Unreal system works great in 3D-games, but it is not so great with 2D:s.
So here it goes:
I am creating a A* Pathfinding algorithm for 2D games. My idea is that the pathfinding would work with the X and the Z axis and it would be used to build games played as “Front view”. Algorithm should be easily used, so i created a Component that would handle the path finding. Users could just place the component inside Actor and set the target and that’s it.
The pathfinding almost working now, it gets the walkable nodes, target node and the end node. algorithm gets the neighbours from the start-point, but there is something wrong with tracing to target. The “current”-node is jumping mindlessly to neighbours. Here is the layout of the project:
GridActor: Generates the grid. Sets the X and Y values for the generated grid.
Check Obstacles: Goes through the nodes. Node calls the function that checks if there is something overlapping it and marks it.
BP_Destination: Just an actor to test the path finding. This could be anything later.
This class will use FindPath component to get path. In this class we visualize the target and start by changing the node-color.
No functions! Using the component “BP_FindPath” that will handle path finding.
BP_Player: Player, AKA end of the trace..
No further code
BP_FindPath: This will handle all the logic for the pathfinding
GetStartAndEnd: This function will use the given parameters to find the node under the location.
GetDistance: Calculates distance between two nodes.
FindPath: Algorithm that will calculate the path.
Retrace Path: Traces the path
BP_Node: Node in the grid
Check Overlaps: Check what this node is overlapping.
Get NeighBours: Returns nodes next to this node (X and Z)
Calculate F-Cost: Calculates H-cost + G-cost
Check Node Near Location: Checks if this node is near given location
Obiviously. There is LOT to change and optimize and build. This is nowhere near ready. However. This is a very solid layout that we can use to build this even further. First thing to do is to get the algorithm working correctly. I think that the problem lies in the BP_FindPath and in the red-commented section. What do you think?
To do list after the basic pathfinding is working
-Add diagonal movement(should be simpe to add)
-Get better algorithm for looping the nodes
-Add nodes that are more costly
-Visualize grid before playing.
Other things to notice:
-Open map “StartMap” in Content
-Play through simulate to see everything.
-Used this tutorial as a guide-point: https://www.youtube.com/watch?v=mZfyt03LDH4 . Changed things to better fit with the
The First trace will always work. The second trace isn’t working. The current node is not jumping to the closest neighbours for some reason:
Get the project here (22mb):