Navigation Agent speeds up after I turn the speed to 0 then back to normal speed

Godot Version

4.3.stable

Question

Hi! I’ve been stuggling with this. My enemy Navigation AI spript has a stun variable, and when the stun variable is on, it turns the speed variable to 0.
After a few seconds the stun variable truns off, and speed is turned back to it normal speed, 560.

However, for some reason the enemy’s AI speeds up for a second after stun is off, then turns back to regular speed. I think the enemy’s Navigation Agent is trying to catch up with it path by speeding up.

Does anyone know how to fix this?

Code:

	if move == true and stun == false:
		SPEED = 560.0
		silly = true
		navigationrunner.target_position = target.global_position
		velocity = global_position.direction_to(navigationrunner.get_next_path_position()) * SPEED
	if hitlimit < 1:
		playerchef.health.value += 5
		queue_free()
	if stun == true:
		SPEED = 0
		await get_tree().create_timer(0.5).timeout
		stun = false
		

You’re updating the SPEED in two cases:

if move == true and stun == false

and

if stun == true

However, you only update the velocity in one case. Try setting the velocity to 0 in the last case.

Just solved it by using making a timer istead of using await. Thanks fo the help though!

func _physics_process(delta: float) -> void:
	marker.look_at(playerchef.global_position) 
	if move == true and stun == false:
		SPEED = 560.0
		silly = true
		velocity = global_position.direction_to(navigationrunner.get_next_path_position()) * SPEED
	if hitlimit < 1:
		playerchef.health.value += 5
		queue_free()
	if move == true and stun == true:
		SPEED = 0
		navigationrunner.target_position = global_position
		velocity = global_position.direction_to(navigationrunner.get_next_path_position()) * 0
		var timer : Timer = Timer.new()
		add_child(timer)
		timer.one_shot = true
		timer.autostart = false
		timer.wait_time = 0.5
		timer.timeout.connect(_timer_timeout)
		timer.start()
		
	if navigationrunner.is_navigation_finished():
		return
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.