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:
- Navigationa meshes
- 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.
- Considered to be a good solution mostly for static meshes, levels without moving obstacles.
- A*
- 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.
- 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*:
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
ShadowStalker
64x64 FPS for LOWREZJAM2022
Status | In development |
Author | Atanii |
Genre | Adventure |
Tags | 3D, First-Person, Metroidvania, Pixel Art, Singleplayer |
Languages | English |
More posts
- First playable versionAug 13, 2022
- Adding sounds, explosive ammo, testing maps...Aug 13, 2022
- Slowly putting the pieces together (very short devlog)Aug 11, 2022
- More or less a break dayAug 10, 2022
- Again...still modelling, but with some programming as wellAug 09, 2022
- Still modelling the Dead City (very short devlog)Aug 08, 2022
- Modelling the city level, making new textures, modelsAug 07, 2022
- Working on the textures for the Dead City (short devlog)Aug 06, 2022
- Multi-phase boss fight, pickable items...Aug 06, 2022
- Connecting objects (eg. destroying a fuse box to open a door)Aug 04, 2022
Leave a comment
Log in with itch.io to leave a comment.