Nav2D pathing bug

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Matt’sGames

I Weird nav2d path loop

On Godot3.1
Keeps changing its mind about which path to take
not sure if this is a bug.
doesn’t happen if the simple path has optimize=true
but I don’t like the look of that path as much :frowning:

func _ready():
		
	get_parent().get_parent().connect("placetile", self, "updatepath")
	
	self.position = get_parent().get_node("spawn").global_position
	goal = get_parent().get_node("end").global_position
	nav  = get_parent().get_node("Navigation2D")
	path = nav.get_simple_path(self.global_position, goal, false)
	get_parent().get_node("Line2D").points = PoolVector2Array(path)
	get_parent().get_node("Line2D").show()

			
func _process(delta: float) -> void:
	if !path:
		
		return
	if path.size() > 0:
		var d: float = self.global_position.distance_to(path[0])
		print(path)
		if d > 10:
			 self.position =  self.global_position.linear_interpolate(path[0], (speed * delta)/d)
		else:
			path.remove(0)
:bust_in_silhouette: Reply From: Zylann

I think it might happen due to float precision, however I would suggest you don’t recalculate the path every frame, just remember it in a variable so you have it once and for all. If it needs to update, just do it a few times per seconds instead, or when the character or its target moved by a minimum distance.

Yeah I came to the same conclusion. Still it would be interesting to know what was behind such a strange behavior.

Matt’sGames | 2019-12-06 05:22