Posted August 03, 2022 by Atanii
#prototype
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:
For the A* I found a great tutorial on YouTube from jmbiv:
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*:
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.
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.