Pathfinding Bot Jitters and Rotates at Every Waypoint

I have tried a new pathfinding system, and I noticed that the bot jitters and rotates around each waypoint. Is there any way to fix that?
Code:

local CS = game:GetService("CollectionService")
local PS = game:GetService("PathfindingService")
local PLS = game:GetService("Players")

local pathfind = {}

local shouldDebugPath = true

local orbFolder = Instance.new("Folder")
orbFolder.Parent = game.Workspace
orbFolder.Name = "Debug"

local orbTemplate = Instance.new("Part")
orbTemplate.Shape = Enum.PartType.Block
orbTemplate.Size = Vector3.new(1,1,1)
orbTemplate.Anchored = true
orbTemplate.CanCollide = false

local function createWaypoint(position)
	local orb = orbTemplate:Clone()
	orb.Position = position + Vector3.new(0,2,0)
	orb.Parent = OrbFolder
	return orb
end

local params = {
	AgentHeight = 5.1,
	AgentWidth = 4.2,
	AgentCanJump = true,
	
	Costs = {
		Border = math.huge
	},
	
	PathSettings = {
		SupportPartialPath = true
	},
}

local function initializeEnemyPathfinding(enemy)
	local humanoid = enemy:WaitForChild("Humanoid")
	local rootPart = enemy:WaitForChild("HumanoidRootPart")

	local currentOrbIndex = 0
	local currentPath = {}

	local function moveToNextWaypoint(previousPointReached)
		currentOrbIndex += 1
		if currentOrbIndex > #currentPath then
			return
		end

		local nextWaypoint = currentPath[currentOrbIndex]
		humanoid:MoveTo(nextWaypoint.Position)
	end

	humanoid.MoveToFinished:Connect(moveToNextWaypoint)

	task.spawn(function()
		task.wait(1)
		local pauseLength = 0.5
		
		while task.wait(pauseLength) do
			if not enemy:IsDescendantOf(game.Workspace) then
				break
			end

			local targetCharacter = nil
			local closestDist = math.huge
			local playerList = PLS:GetPlayers()

			for _, player in pairs(playerList) do
				local character = player.Character
				if not character then
					continue
				end

				local p1 = character.PrimaryPart.Position
				local p2 = rootPart.Position
				local d = math.abs((p1 - p2).Magnitude)
				if d < closestDist then
					closestDist = d
					targetCharacter = character
				end
			end
			
			if targetCharacter == nil then
				continue
			end

			local characterPos = targetCharacter.PrimaryPart.Position
			local path = PS:CreatePath(params)
			path:ComputeAsync(rootPart.Position, characterPos)
			local waypoints = path:GetWaypoints()

			if shouldDebugPath then
				OrbFolder:ClearAllChildren()    
				for _, waypoint in pairs(waypoints) do
					createWaypoint(waypoint.Position)
				end
			end

			currentOrbIndex = 0
			currentPath = waypoints
			moveToNextWaypoint()
		end
	end)
end

for _, enemy in pairs(CS:GetTagged("Enemy")) do
	initializeEnemyPathfinding(enemy)
end

CS:GetInstanceAddedSignal("Enemy"):Connect(function(enemy)
	initializeEnemyPathfinding(enemy)
	enemy:WaitForChild("HumanoidRootPart"):SetNetworkOwner(nil)
end)

return pathfind

instead of having it move to each waypoint, have it move to every third waypoint.

Alright, ill try that (sorry for the late reply)

The bot still rotates randomly at every third waypoint

Timing is off. A jitter at waypoints is showing too much time waiting between waypoints. You also have the agent WaypointSpacing= you can play with to fine tune.

So far these are my params:

	AgentHeight = 5.1,
	AgentWidth = 4.2,
	AgentCanJump = true,

	Costs = {
		Border = math.huge
	},

	PathSettings = {
		SupportPartialPath = true
	},
}

What would you recommend a good spacing value

Maybe

local Agents = {
	WaypointSpacing = 16,
	Speed = 16,
	AgentHeight = 5.1,
	AgentWidth = 4.2,
	AgentRadius = 1,
	AgentCanJump = true,
	Costs = {
		Border = math.huge
	}
}

I’ve got a few smooth ones but I’ve never been able to completly remove all jitter of this type.

Now the pathfinding is way more jittery

You’re going to have to play around with the numbers a bit … of corse posting my nunbers will not be right. I can’t test anything over here and am just guessing… did you set this
character.HumanoidRootPart:SetNetworkOwner(nil)

Noticed your parameters should be AgentRadius, you have AgentWidth there
If that’s the width of your agent then radius should be 2.1

For the jittering – can you record what’s going on? It’s hard to understand what you mean by jitter, since that code doesn’t really produce jitter in my experience. Do you have something else making your npc move somehow

No thats the only script, and by jitter, I mean it rotates to the left at each waypoint it gets to

Here is a video:

Still haven’t fixed the problem

Fixed the issue and now i have more problems, but that’s for another post

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