Constant Chasing Pathfinding AI Inconsistency

i am developing a game where you fight off random bosses and, uhh thats pretty much it. but i can’t seem to fix a problem in my AI system;

while task.wait() do
	local target = CommonAI:FindNearestPlayerWithinRange(AGGRO_RADIUS)
		
	if not target or not target.Character then continue end
		
	local character = target.Character
		
	if target:DistanceFromCharacter(Rig.PrimaryPart.Position) < ATTK_RANGE then
		character.Humanoid:TakeDamage(DAMAGE)
		continue
	end
				
	if DIRECT_CHASE then
		MoveTo(character.PrimaryPart.Position)
	else
		params.FilterDescendantsInstances = {character, Rig}
			
		local result = workspace:Spherecast(Rig.PrimaryPart.Position, SIGHT_RADIUS, (character.PrimaryPart.Position - Rig.PrimaryPart.Position), params)
			
		if result and result.Instance.Anchored then
			local path = CommonAI:CreatePath(character.PrimaryPart.Position)
			path:ComputeAsync(Rig.PrimaryPart.Position, character.PrimaryPart.Position)

			-- back up when no path
			if path.Status == Enum.PathStatus.NoPath then
				print('no path')
				MoveToAsync(character.PrimaryPart.Position - (Rig.PrimaryPart.Position * 10))
				continue
			end

			-- pathfind thing
			local waypoints = path:GetWaypoints()

			for _, waypoint: PathWaypoint in waypoints do
				MoveToAsync(waypoint.Position)
			end
		else
			MoveTo(character.HumanoidRootPart.Position + character.HumanoidRootPart.Velocity / 30)
		end
	end
end
Variable Value
CommonAI a module that helps me do repetitive task
DIRECT_CHASE a boolean setting
params a raycast params

the ai just wouldn’t run smooth and sometimes jitter, i’ve tried to only use 2 waypoints per iteration and changed the while loop to a Heartbeat event

this is not my first encounter on this problem; i can’t seem to find any tutorials nor any posts that could possibly fix this;

(yes i did set the network owner of the root to the server)


thank you

i would take a pseudocode as a solution IF it did work
1 Like

Hello, so from what I can see in the script, the jittery movement might be caused by the continuous and instant changing of the target positions in the MoveTo() and MoveToAsync() function calls. I believe that solution to this could be to only update the AI’s path at certain intervals or under certain conditions, instead of on every frame or iteration of the loop.

Try modifying the waypoint loop as follows:

for _, waypoint: PathWaypoint in waypoints do
	MoveToAsync(waypoint.Position)
	while (Rig.PrimaryPart.Position - waypoint.Position).Magnitude > SOME_THRESHOLD do
		wait()
	end
end

This loop will now wait for the AI to get close enough to the waypoint before moving on to the next one.

If this did not work maybe try this solution: you might add a prediction of where the player will be, based on their current velocity. Something like this:

local predictedPosition = character.HumanoidRootPart.Position + character.Humanoid

You would use this predictedPosition as the target for MoveTo() , MoveToAsync() , or for the CreatePath() call. Hope something from this will help you :slight_smile:

1 Like

should’ve said this before but, the MoveToAsync() function already waits for the humanoid to finish moving; i don’t think i need to do the while loop?

the condition required for it to do a pathfind is DIRECT_CHASE being true and result’s Instance being anchored (result = check if theres a object obstructing the rig view); now, I don’t know when to update the path when chasing

(red is the boss/rig; black is the player/current path; green is the desired path; bluish purple is the obstacle)

1 Like