Hey! To put it simply:
- I'm making a 2D top down game
- Tilemap & Navigation2D are done
- I have a player, and several enemies that can successfully chase the player around the map (they avoid walls and all)
However, if there is half a dozen enemies or more, the game becomes extremely laggy. After some trial and error, I discovered that it's caused by getsimplepath, used in enemy.gd. I'm pretty much sure I'm using it wrong.
Basically, what I do is:
var stuck_distance = 0.3
var stuck = global_position.distance_to(last_position) <= stuck_distance
path = ELEMENTS.paths.get_simple_path(global_position, ELEMENTS.player.global_position, stuck)
last_position = global_position
move_and_slide((path - global_position).normalized() * speed)
I guess I shouldn't call getsimplepath every time physicprocess is called, but how else am I supposed to update my enemies' pathfinding?
I tried putting a small time restriction (just on the getsimplepath and path.remove(0) lines), but it makes the enemy's movements buggy.