I want to make my own OOP pathfinding zombie npc system, however the zombies get stuck on stuff like trees sometimes when they’re wandering around and it’s really annoying. I’ve tried things like limiting the distance they can wander around and tried searching it up, but to no avail. Here is an video showing it:
And this is the code:
function NPC:walkRandomly(myRoot, myHuman)
local xRand = math.random(-50,50)
local zRand = math.random(-50,50)
if myRoot then
local goal = myRoot.Position + Vector3.new(xRand,0,zRand)
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(myRoot.Position, goal)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for _, waypoint in ipairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
myHuman.Jump = true
end
myHuman:MoveTo(waypoint.Position)
local timeOut = myHuman.MoveToFinished:Wait(1)
if not timeOut then
myHuman.Jump = true
NPC:walkRandomly()
end
end
else
task.wait(1)
NPC:walkRandomly()
end
end
end