Hello, I am facing issues with Roblox pathfinding API.
I use a simple script to compute path for NPC, there’s nothing unusual. Then I have piece of my code that renders waypoints, which are then followed by the NPC. I also have a function in place which prevents jittery movement, but that doesn’t mess with waypoint placement at all. Placement of these waypoints is mostly off and sometimes doesn’t make any sense.
I know that this is an issue which probably has to be fixed by Roblox, but I’d like to know whether there are more people facing the same issue.
If anybody has an idea how to fix this or how to make a great workaround, I am more than willing to listen. I need to keep performance in mind.
Here’s a demonstration video:
Here’s what navigation mesh looks like:
Code
-- Create path
local path: Path = pathfinding:CreatePath({
Costs = {
Bypass096 = 1e+308
},
--WaypointSpacing = 10,
AgentHeight = 5 * self.Fuel / 10,
AgentRadius = 2 * self.Fuel / 10,
AgentCanJump = false,
})
path:ComputeAsync(ourPos, target.Character.HumanoidRootPart.Position)
if path.Status == Enum.PathStatus.NoPath then table.insert(BL, target.Character) continue end
self.Target = target.Character
local waypoints = path:GetWaypoints()
self.Waypoints = waypoints
-- Visualize path
for _, p in pairs(pathParts) do p:Destroy() end pathParts = {}
for _, wp in pairs(waypoints) do
local p = Instance.new("Part")
p.Anchored = true
p.CanCollide = false
p.Transparency = 0.5
p.Position = wp.Position
p.Size = Vector3.one * .5
p.Color = Color3.new(1, 0, 0)
p.Parent = workspace
table.insert(pathParts, p)
end
-- Follow path
task.spawn(function()
local currWP = 0
local blockedCon = path.Blocked:Connect(function(wpIndex)
if wpIndex > currWP then
self.Waypoints = {}
if self.Waypoints == waypoints then
self.Humanoid:MoveTo(self.RootPart.Position)
end
end
end)
for i, waypoint in pairs(waypoints) do
local nwp = waypoints[i + 1]
if i == 1 and nwp and not self:IsWaypointValid(waypoint, nwp) then continue end
currWP = i
self.Humanoid:MoveTo(waypoint.Position)
self.Humanoid.MoveToFinished:Wait()
if self.Waypoints ~= waypoints then
break
end
end
blockedCon:Disconnect()
end)
Thank you for reading.