Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How could I make my A* pathfinding NPC better?

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

2 comments

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.

1 answer

✓ Accepted answer

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.

3 comments

0

just realized I wrote your script a year ago the day the pathfinding API was released.

0

Haha, thanks. I'm doing this for a commission and I had no idea how pathfinding works. Thanks!

0

I updated the script from the place: http://www.roblox.com/games/168156797/Pathfinding made some improvements on it.

This discussion is preserved as it was. New answers, comments, and votes are closed.