Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How could I make my A* pathfinding NPC better?

Asked by 8 years ago

My NPC get's the job done, but sometimes he gets stuck around corners trying to cut it too close, and get's stuck.

local p=game:GetService("PathfindingService")
local nodes={}
for _,v in pairs(workspace.Nodes:GetChildren())do
    if v.Name=="Node"then
        nodes[#nodes+1]=v
    end
end

for _,v in pairs(workspace.NPCs:GetChildren())do
    wait(.1)
    Spawn(function()
        while wait()do
            local target=nodes[math.random(1,#nodes)]
            local path=p:ComputeRawPathAsync(v.Torso.Position,target.Position,500)
            if path.Status==Enum.PathStatus.Success then
                local points=path:GetPointCoordinates()
                for _,point in pairs(points)do
                    local part,P=workspace:FindPartOnRay(Ray.new(point,Vector3.new(0,-10,0)))
                    if part then
                        point=P
                        points[_]=P
                    end
                end
                local br=false
                local distance;
                for _=1,#points,2 do
                    local point=points[_]
                    if br then
                        break
                    end
                    local count=0
                    repeat
                        count=count+1
                        if count>100 then
                            br=true
                            break
                        end
                        v.Humanoid:MoveTo(points[math.min(#points,_+1)])
                        if point.Y>=v.Humanoid.Torso.Position.Y-1 then
                            v.Humanoid.Jump=true
                        end
                        distance=(point*Vector3.new(1,0,1)-v.Torso.Position*Vector3.new(1,0,1)).magnitude
                        wait()
                    until distance<5
                end
            else
                v.Humanoid:MoveTo(v.Torso.CFrame*Vector3.new(0,0,5))
            end
        end
    end)
end

In other words, is there a way to make this script LESS accurate? (so he won't get stuck around corners or try to go through tight spaces)

UPDATE: Here is a picture: http://s11.postimg.org/603rmqd4j/Screen_Shot_2015_10_26_at_5_14_38_PM.png

0
Do you have a screenshot of how you placed your nodes? I'm not sure how you placed them but maybe you might want to bring the ones near the corners farther out diagonally. Legojoker 345 — 8y

1 answer

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

If you set game:GetService("PathfindingService").EmptyCutoff to 0, any voxel that is not completely empty will not be a part of the path.

If you add p.EmptyCutoff=0 after line 1, the path should stop trying to cut through corners.

0
just realized I wrote your script a year ago the day the pathfinding API was released. 1waffle1 2908 — 8y
0
Haha, thanks. I'm doing this for a commission and I had no idea how pathfinding works. Thanks! iUnEarthly 85 — 8y
0
I updated the script from the place: http://www.roblox.com/games/168156797/Pathfinding made some improvements on it. 1waffle1 2908 — 8y
Ad

Answer this question