Hello, I have an issue with NPC pathfinding whereby the rig seems to be taking really strange routes which could be a lot more simple. I am not sure if there’s something missing, but would really appreciate some help.
Initially, the NPC was taking a really bizarre route where it would go around a part for absolutely no reason (see video #1 below). I then looked into pathfinding links, and managed to somewhat get around this, however the NPC still takes weird routes, especially on the way back to the original position (see vid #2 below) and often gets stuck when trying to move past certain parts.
Is there any way to get around this and make it so that the NPC is not taking these strange routes, and avoids colliding with random objects and getting suck?
Videos: ROBLOX Pathfinding Demos - Album on Imgur
Move Function:
function moveNPC(NPC, Part)
local Humanoid = NPC:WaitForChild("Humanoid")
local Animator = Humanoid.Animator
local WalkAnim = Animator:LoadAnimation(NPC.Animate.walk.WalkAnim)
local waypoints = calculatePath(NPC, Part)
WalkAnim:Play()
local currentPos = NPC.HumanoidRootPart.Position
for i, waypoint in ipairs(waypoints) do
wait()
local newPos = waypoint
-- Move the NPC
local success, message = pcall(function()
Humanoid:MoveTo(newPos)
Humanoid.MoveToFinished:Wait()
end)
if not success then
print("Error moving NPC:", message)
end
currentPos = newPos
end
WalkAnim:Stop()
wait(1)
end
BEFORE Pathfinding Link:
function calculatePath(NPC, Part)
local Path = game:GetService("PathfindingService"):CreatePath()
Path:ComputeAsync(NPC.HumanoidRootPart.Position, Part.Position)
local waypoints = Path:GetWaypoints()
local pathPositions = {}
for _, waypoint in ipairs(waypoints) do
table.insert(pathPositions, waypoint.Position)
end
return pathPositions
end
AFTER Pathfinding Link:
function calculatePath(NPC, Part)
local AgentParameters = {
AgentRadius = 4,
WaypointSpacing = 3,
Costs = {
Link = 2,
}
}
local Path = game:GetService("PathfindingService"):CreatePath(AgentParameters)
Path:ComputeAsync(NPC.HumanoidRootPart.Position, Part.Position)
local waypoints = Path:GetWaypoints()
local pathPositions = {}
for _, waypoint in ipairs(waypoints) do
table.insert(pathPositions, waypoint.Position)
end
return pathPositions
end
Thank you for reading,
Cody