Search code examples
c#unity-game-enginepath-findinga-star

How to Find Path to a Certain Object Without Knowing Its Position in a Grid Based Video Game?


I´m in the middle of the development of a personal project . In this video game there is a grid based movement , creation and pathfinding system . Previously , I have used A* pathfinding in order to make certain characters move towards a certain known position .

But now I have encountereda big question . There is a character in my game that has to go to a certain kind of object , but this character doesn´t know the exact coordinate of such object . The easiest way of doing this would be to actually know the coordinates of those objects , calculate which one is the nearest one and use A* pathfinding in order to get to that point , but that aproach seems really rigged and not dynamic enough for the type of game I´m making.

So I would like to know if there is some pathfinding algorithm that searches around a certain point (the location of the character that is using that algorithm) , like in a radial way , and keeps searching until it finds the object type that is looking for and returns the path to that certain object.


Solution

  • Look up for Dijkstra's Algorithm. It basically covers the whole graph before calculating the shortest path to a destination, unlike A* which goes towards the destination upfront. In other words, Dijkstra finds ALL the paths to All Nodes from a source, but then choose a destination and its shortest path or least cost. There is a nice GIF on Wiki illustrates what I mean here: Click and look up A* on Wiki to see its GIF and understand this comparison.

    How Dijkstra's Algorithm approach the shortest path to a destination: enter image description here

    How A* Algorithm approach the shortest path to a destination:

    enter image description here