Optimize infinite pathfinding / enemy AI

I have problems while setting up a map with infinite pathfinding: it lags and tried a lot of things but it doesn’t get solved 100%


This is the idea: the enemy ( red circle ) follows square while square moves.

The main cause of lag is when the player gets too close to a building and it is impossible to find any route

Look at reducing the virtual cell size in the pathfinding behaviour on the enemy.

Already done, but not working

What size are you using? And are the dimensions of your enemy?

So the game is searching continuously for a path? Maybe look at altering your algorithm to search after an interval, rather than every frame.

No, thats wrong. Path Found is used after the move command. The Move to a position tries to find a path. The path found is to confirm one has been calculated.

This works almost perfectly, but starts lagging after some time and when im close to buildings

And this is what a mod suggested, but not working:

60x60, both grid and enemy

I think this is because the virtual cells don’t allow it to get to the player position. The cell is blocked from getting closer to the player because it’s edges hit the pathfinding obstacle, and the player position is somewhere between the enemy and the obstacle.

Try a smaller virtual grid, starting with your player’s dimensions. If that doesn’t work, keep reducing it by 2 for each dimension until you get the result you’re after.