Nav Agent 3D freaks out when I try to get it to wait

4.5 Stable

Howdy, I’m trying to get this state to have my enemy loiter around the player until the timer runs out, which is working fine. However, every method I try to get it to pause after reaching a destination before going to the next causes it to freak out and spin wildly, occasionally getting stuck. Ive tried puting an await get_tree().create_timer(1.0).timeout both in the loiter func, and after is_navigation_finished, to the same result


Any help would be appreciated, either in how to totally remake the loitering to work, or get some the thing to actually pause for a second after reaching target_pos, before picking its next one

Since Update() has a delta parameter, I’m assuming it’s getting called in every _physics_process()?

await get_tree().create_timer(1.0).timeout puts a one second delay in the functions execution, but the function will still be called every physics tick then. For example, if you would implement it at the beginning of the loiter() function, you would delay the new target position by one second, but then you would get a new target position every physics tick during the following second.

Also, there will likely be some timing issues, because it takes at least one additional physics tick to update the target position.

You can probably fix this if you use an additional bool to prevent multiple timers, and shortly delay that bool’s reset:

var is_awaiting := false

func loiter():
	
	# ...
	
	# I think you should wait at least 2 physics ticks here? If the value is higher
	# it should only matter when the new target position is close to the current
	# position though, which you might want to prevent by other means.
	await get_tree().create_timer(0.1).timeout
	is_awaiting = false

func Update(delta):
	#...
		else:
			Global.en_speed = 2
			if is_awaiting == false:
				is_awaiting = true
				get_tree().create_timer(1.0).timeout.connect(loiter)