Godot Version
4.3
Question
Am working on my first Godot project, using GDScript in 4.3.
The core idea at this point is a pub/bar sim, but I’m running into some challenges with NPC movement. What I essentially want to achieve is:
- NPC spawns in the bottom-left corner of the map and finds a path to an available “seat” node
- NPC should only be able to move in four directions - up, down, left and right
- NPC should move along the 16x16 grid, and always stop in the center of the grid
- NPC should pathfind around other furniture items (tables, bar segments, possibly other NPCs but I’m willing to let this go)
This should let it stop exactly on the “seat” sprite and then face the table.
I’ve tried a couple of methods of pathfinding/navigation – including AStarGrid2D and NavigationAgent with a NavigationRegion mesh.
NavigationAgent + NavigationRegion
- I couldn’t find a way to enforce orthogonal movement. I don’t want the NPCs to move in diagonals at all.
- Very unclear on which TileMapLayer it uses to get navigation data, and how to set it up to work from multiple TileMapLayers (so I can paint a “walls” layer of tiles that are not navigable on top of a “floor” layer of tiles that are)
AStarGrid2D
- This gave me the movement directions I wanted, but the NPC was failing to stop on the precise spot. Unlike the NavigationAgent, I wasn’t able to find any setting that let me define how close it needed to be for navigation to be complete. I could get it closer/further to the spot by increasing the speed on the last navigation step, but it took a lot of finessing to get it to stop exactly aligned on the grid box. And not ideal if the NPCs will have variable speed in future.
- A little clearer on how to work with data from other TileMapLayers in this system, but what about obstacles that aren’t placed as tiles? How can I instruct it to navigate around an object in a “Table” scene?
I sort-of hacky-fixed the finishing position issue by detecting if there was only one navigation point left in the point list in AStarGrid2D, and tweening to the last position instead of using move_toward. It works, but doesn’t feel correct?
Once get this handled, the next challenge is absolutely going to be how to make it work with objects that will be placeable as part of the game, so the pathfinding needs to not be locked to a static map.
This can’t be an uncommon thing to do in a 2D top-down game, I must be missing something obvious and no amount of Googling or reading the Docs has revealed the answer to me.
Most of the navigation tutorials out there use TileMap rather than TileMapLayer nodes, and most grid-based movement tutorials are written for Player movement on input rather than movement along a path.
Any advice/help/tutorials you can point me toward would be greatly appreciated.