Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | neverbegameover |
Hello, I am using the get_simple_path() function with Nav2D and tilemaps so that enemies can search and chase the player. However, since I have a lot of enemies in my project, this causes a massive performance drop (60 fps drops to 6 - 7). How do I improve the performance of this? I’ve seen people say to only update it periodically, but how do I do this within the physics process since it always updates every frame?
Here’s my pathfinding code for reference:
var path = nav_2d.get_simple_path(global_position, player_pos, false)
var start_point = position
var distance = MOVE_SPEED * delta
for i in range(path.size()):
var distance_to_next = start_point.distance_to(path[0])
if distance <= distance_to_next and distance >= 0.0:
position = start_point.linear_interpolate(path[0], distance / distance_to_next)
break
distance -= distance_to_next
start_point = path[0]
path.remove(0)
if path.size() == 0:
#Change state here etc...
return