![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Ortcel |
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 get_simple_path, used in enemy.gd. I’m pretty much sure I’m using it wrong.
Basically, what I do is:
var path
func _physic_process(delta):
# [Things...]
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
if path.size():
path.remove(0)
move_and_slide((path[0] - global_position).normalized() * speed)
# [Things...]
I guess I shouldn’t call get_simple_path every time _physic_process is called, but how else am I supposed to update my enemies’ pathfinding?
I tried putting a small time restriction (just on the get_simple_path and path.remove(0) lines), but it makes the enemy’s movements buggy.