• Godot Help
  • AI pathfinding is laggy in godot 3.5, can't fix

My AI pathfinding when ever you spawn a object, it should pathfind towards you after 20 seconds. that does happen, except extremely laggy. can anyone help?

How do you mean 'laggy'? That's a pretty vague description, if there's a delay in how the AI moves that could be down to your settings and things like that. You'll need to explain in more detail what's going on, the problem with the current navmesh setup is very often the navmesh itself and the calculations are all working correctly, however if your settings or alternatively setup is wrong it's very easy to misinterpret as a bug or issue with the actual engine when it's not.

make sure you're not resetting the destination with set_target_location() every frame.

ah that could be the problem. I am going to show some code which is for the `pathfinding:

extends KinematicBody

onready var agent : NavigationAgent = $agent
onready var target : Node = $"../../../Player"
onready var timer : Timer = $Timer2
var rampage = 0

func _physics_process(delta) -> void:
	if rampage == 1:
		#agent.set_target_location(target.transform.origin)
		#var next = agent.get_next_location()
		#var velocity = (next - transform.origin).normalized() * 1
		#move_and_collide(velocity)
		#timer.start()
		print("rampage")


func _on_Starving_rampage():
	rampage = 1


func _on_Timer2_timeout():
	rampage = 0

by laggy I mean that the camera is jittery and slow, and if the player moves, it takes a long time for it to do so.

could someone help fix it? it has to only happen when the variable rampage = 1 though.

You might want to post a sample project somewhere showing the issue. I doubt that anyone can troubleshoot it based on the code you've posted.

What you're describing sounds like a setup issue rather than anything to do with the navmesh itself.

Here's what you need to know: the stuff that happens when the npc starts to rampage: some of it just needs to happen once and some of it will need to happen every frame (basically all the time).
Things that will just need to happen once:
In _on_Starving_rampage():

  • set target location
  • set rampage to 1
  • start a timer

Things that will need to happen all the time:

  • If real distance to object is further than x: reset target location again
  • get next location
  • move toward that location