So I am making enemies for my game and I’ve made a state for navigating them when they see the player.
but whenever I get to a place that they can’t reach the game starts lagging so bad
I have an rtx 4070 and it goes from 2000-3000 fps to 60-40.
And sometimes they get stuck on next to the walls.
I will attach a video link and in the game, on the right top of the screen I have an fps counter that only displays the fps rates that are bellow 200 and I also show the issue in the video also the code to navigate the ai.
When a position can not be reached in order to confirm this the pathfinding needs to search a lot more navigation mesh polygons on the navigation map before it gives up. If a position can be reached quickly by A* the pathfinding does early exit.
This early-exit in ideal situations can hide the true performance cost of a too large and complex navigation mesh layout for a long time. Still, the mesh layout was already an issue and had too many polygons to seach through for so many agents in the same frame. It was all just hidden behind optimizations that can not be applied in all situations.
If the agent is already at the closest position that it can reach the old pathfollow ended and it will query a new path when get_next_path_position() is called with a target position to far away. Calling for a new path in physics process is certain performance death with so many agents when the position can not be reached because so many polygons need to be searched for each agent.
What you can do for performance immediately is to stop calling get_next_path_position() every physics process when the position and path has not changed and only try a new query after some time.
That agents get stuck is a physics shape setup problem and movement code problem. You need to make sure that the parent node of the NavigationAgent can reach the path point to advance the path index. If physics collision blocks this the agent can never advance the path.