Hello, my pathfinding is working fine, however, it cuts corners. Right now I have the cost of the concrete (the road) to 0.1, and the cost of Fabric (the blocks material) to 0.01. When going to the waypoint, it cuts corners, even though it stays on the road. I want it to follow the green blocks instead to get to its end destination. How can I do this?
What are you using for path finding. You talk about waypoint spacing and cost, what code are you using that will need these values in relation to the pathfinding?
local PathFindingService = game:GetService("PathfindingService")
local NODES = game:GetService("Workspace").NODES
local pathfinding = {}
pathfinding.Path = function(npc)
local randomNode = nil
local success = false
local pathIsSuccessful = false
repeat
randomNode = NODES:GetChildren()[math.random(1, #NODES:GetChildren())]
local partsIn = game.Workspace:GetPartsInPart(randomNode)
for i, v in pairs(partsIn) do
if v.Parent == npc then
success = false
continue
end
end
success = true
until success == true
local function walk()
local path:Path
local AgentParameters = {
WaypointSpacing = 4,
Costs = {
Fabric = 0.01,
Concrete = 0.1
}
}
path = PathFindingService:CreatePath(AgentParameters)
local humanoidRootPart = npc:WaitForChild("HumanoidRootPart")
local connection
local pathBlockedConnection
local RETRY_NUM = 0
local success,errormessage
repeat
RETRY_NUM += 1
success, errormessage = pcall(path.ComputeAsync, path, humanoidRootPart.Position, randomNode.Position)
if not success then
task.wait(.5)
end
until success or RETRY_NUM > 5
if success then
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
local currentIndex = 2
if not connection then
connection = npc.Humanoid.MoveToFinished:Connect(function(reached)
if reached and currentIndex < #waypoints then
currentIndex += 1
npc.Humanoid:MoveTo(waypoints[currentIndex].Position)
if waypoints[currentIndex].Action == Enum.PathWaypointAction.Jump then
npc.Humanoid.Jump = true
end
else
print("Disconnection connection")
connection:Disconnect()
connection = nil
pathBlockedConnection:Disconnect()
pathBlockedConnection = nil
pathIsSuccessful = true
end
end)
pathBlockedConnection = path.Blocked:Connect(function(waypointNumber)
if waypointNumber > currentIndex then
print("Disconnection path block")
connection:Disconnect()
pathBlockedConnection:Disconnect()
connection = nil
pathBlockedConnection = nil
walk()
end
end)
else
print("Connection exists")
end
npc.Humanoid:MoveTo(waypoints[currentIndex].Position)
if waypoints[currentIndex].Action == Enum.PathWaypointAction.Jump then
npc.Humanoid.Jump = true
end
else
warn("Path was not a success")
end
else
warn("Path computing error", errormessage)
return
end
end
walk()
repeat task.wait(.2) until pathIsSuccessful == true
return pathIsSuccessful
end
return pathfinding