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