Fixing Navigation2D Performance

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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
:bust_in_silhouette: Reply From: deaton64

Hi,

I’m guessing here, but maybe it’s the loop…
I based my path following on this Kids Can Code example. I don’t have loads of enemies on the screen, but I don’t see any slow down.

Well that example doesn’t use pathfinding at all. It just makes it so a node can travel along a per-determined path.

neverbegameover | 2021-01-18 11:39