I’m not going to mark this as a bug, but rather support because I suppose I’m doing something wrong.
My issue: I’m trying to make a GPS-like system that takes a start and the destination and use the right side of the road to get there. The lanes have a cost of 1 while the baseplate and the concrete itself have a cost of 20 (to ensure the car doesn’t just drive in the median). I used pathfinding links which have BiDirectional disabled so the cars drive on the right side.
Here is the route the PathfindingService created [in red]
My desired path in green
In the photo: the service was told to create two different paths: one that starts at the beginning and goes to the first goal, and one that goes from the first goal to the second. Instead of using the road when possible, it just took the shortest path.
Here is my code below.
local pathfind=game:GetService('PathfindingService')
local path=pathfind:CreatePath({WaypointSpacing=1,AgentCanJump=false,AgentCanClimb=false,Costs={Road=1,Plastic=20,Concrete=20}})
path:ComputeAsync(script.Parent.Rig.HumanoidRootPart.Position,script.Parent.goal.Position)
for _, point in path:GetWaypoints()do
script.Parent.Rig.Humanoid:MoveTo(point.Position)
script.Parent.Rig.Humanoid.MoveToFinished:Wait()
end
path:ComputeAsync(script.Parent.Rig.HumanoidRootPart.Position,script.Parent.goal2.Position)
for _, point in path:GetWaypoints()do
script.Parent.Rig.Humanoid:MoveTo(point.Position)
script.Parent.Rig.Humanoid.MoveToFinished:Wait()
end
I appreciate all of your input! Thanks in advance.
local pathfind = game:GetService("PathfindingService")
local path = pathfind:CreatePath({
AgentCanJump = false,
AgentCanClimb = false,
WaypointSpacing = 1,
Costs = {
Road = 1,
Plastic = 100,
Concrete = 100
}
})
function go(target)
path:ComputeAsync(script.Parent.Rig.HumanoidRootPart.Position, target.Position)
for _, pt in path:GetWaypoints() do
script.Parent.Rig.Humanoid:MoveTo(pt.Position)
script.Parent.Rig.Humanoid.MoveToFinished:Wait()
end
end
go(script.Parent.goal)
go(script.Parent.goal2)
Roblox’s PathfindingService does not guarantee selection of the lowest total cost path… It finds the shortest navigable route unless obstacle costs dramatically affect it. Your cost table influences the pathing only when multiple paths exist, and all else is equal.
Key word here is dramatically.
This may be too tight … WaypointSpacing = 1, You’ll have to play with it.
I found out that it generates the costs on a 1x1 grid regardless of what the WaypointSpacing is, so in order for it to work correctly I have to apply a grid to all of the roads