A* in Godot for path finding, player detection


Path Finding and A*

When the enemy detects the player and starts chasing it, it should be able to avoid obstacles - or at least try it.

Now after I looked around I found out that there are 2 ways - the 2 best known - to make this work in Godot:

  1. Navigationa meshes
    1. Very static. In a nutshell it basically "bakes" a navigational mesh upon the selected meshes you want to make the enemy able to walk upon and search for paths. If there are obstacles, they will be basically holes in the navigation mesh.
    2. Considered to be a good solution mostly for static meshes, levels without moving obstacles.
  2. A*
    1. Basically a grid of connected points. If the enemy sees the player, the A* algorithm will calculate and give the coordinate of the point from the grid that is the closest to the player, providing a path to that points via the others.
    2. Obstacles - basically points we disable in the grid - can be added anytime, the grid doesn't need to be baked to work.

For the A* I found a great tutorial on YouTube from jmbiv:

What is even better is, that the code is available, so I could analyze it myself:

https://github.com/josephmbustamante/godot-3d-astar-tutorial

Godot basically provides the full A* algorithm, so we only need to take care of the point and we have to maintain the grid. This way we can use it without worrying about the math A* needs.

The tutorial provides a way to click somewhere on the walkable surface, so the player can use A* to find a path and go to the clicked position - which could be really useful eg. in point and click 3D games, to think about it.

For some technical info about the A*:

  1. https://www.geeksforgeeks.org/a-search-algorithm/
  2. https://en.wikipedia.org/wiki/A*_search_algorithm

Detecting and chasing player

To imagine what the grid would look like:


Now what I needed is to make the enemy able to follow the player through this grid, after the player enters the detection zone.

For the detection area, I used a flat cylinder shaped collision object and I added a ray with the radius that matches the radius of the cylinder more or less.

After the player steps in the detection area, the ray is casted right at the player and if the player is the first object the ray reaches, we basically use the player's global position as goal for the A* path finding to get the nearest grid point with a followable path.

Problems

While after a bit of a work it worked...for now path finding in my game is not really perfect, because the obstacles are more or less added...but not all the points get disabled that should be to make the enemy really avoid getting stuck or being left without a given path.

For now I add all the obstacles after the initial grid is made, disabling the points where there is an obstacle. I think I might have to reconnect the points...or just look through again the code...but for now it's a good start.

Get ShadowStalker

Leave a comment

Log in with itch.io to leave a comment.