I want my NPC pathfinding system to be able to successfully pathfind between 2 points. It works for the most part but I noticed some issues happening often.
The NPC’s pathfinding would hit close to the corner of a wall, and then not generate any waypoints after that until the end. This would cause the NPC to keep on walking into the wall forever.
Examples:
On another note, unrelated but I also noticed the nodes got pretty close to the corner of the wall at times, even when the AgentRadius was large.
My Code:
local pathfinder = {}
local pathfindingService = game:GetService("PathfindingService")
local agentParams = {
AgentRadius = 5,
AgentHeight = 5,
AgentWalkableClimb = 2,
AgentCollisionGroupName = "Player",
AgentCanJump = false,
WaypointSpacing = 2,
}
function pathfinder.FindPath(startPos, endPos)
if not startPos or not endPos then return end
for _, v in pairs(workspace.ww:GetChildren()) do
v:Destroy()
end
local path = pathfindingService:CreatePath(agentParams)
local success, errorMessage = pcall(function()
path:ComputeAsync(startPos, endPos)
end)
if success then
local pathTable = {}
for _, t in ipairs(path:GetWaypoints()) do
if t.Position then
table.insert(pathTable, t.Position)
end
end
for _, v in ipairs(pathTable) do
local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false
part.CanTouch = false
part.BrickColor = BrickColor.new("Neon green")
part.Size = Vector3.new(1,1,1)
part.Shape = Enum.PartType.Ball
part.Material = Enum.Material.Neon
part.Position = v
part.Parent = workspace.ww
end
return path, pathTable
else
warn("Pathfinding failed:", errorMessage)
end
return nil, nil
end
return pathfinder