Roblox PathfindingService

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to use the pathfinding service without having large distances between waypoints on terrain.

  2. What is the issue? Include screenshots / videos if possible!
    At the moment, the path created is a zig-zag pattern over terrain, in spite of it being able to take straighter paths to it’s destination. Black line in the image shows what I want the path to be.


    image

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried using agentparams waypoint spacing, and I assumed it’d fix the problem, but it hasn’t.
    I can’t find a comprehensive list or explanation on agentparams and costs despite looking for a few hours, not sure what might be able to make the path generate as straight as possible.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local function pathTo(char,target)
	local path = pathfindService:CreatePath({
		["WaypointSpacing"] = 100
	})
	path:ComputeAsync(char.HumanoidRootPart.Position,target)
	local waypoints = path:GetWaypoints()
	local hum = char.Humanoid
	for i,v in pairs(waypoints) do
		local part = Instance.new("Part")
		part.CanCollide = false
		part.Anchored = true
		part.Size = Vector3.new(0.5,0.5,0.5)
		part.Position = v.Position
		part.Parent = game.Workspace
		
	end
	for i,v in pairs(waypoints) do
		if v.Action == Enum.PathWaypointAction.Jump then
			hum.Jump = true
		end
		hum:MoveTo(v.Position)
		hum.MoveToFinished:wait()
		if v.Action ~= Enum.PathWaypointAction.Jump then
			task.wait(1)
		end
	end
	return
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Yeah uh, pathfinding tends to do that, making weird patterns or paths.

What I would do is make a character walk in a straight line towards the target and perform raycasts on the way.
If the raycast hits anything we can determine that the path might be blocked and have to walk around said blockage.

Kinda what I do for things like zombies.
There’s no reason to use slow and inefficient pathfinding if the target can be reached in a straight line.

While zombie walks forward, you can do several raycasts forward to check if anything blocks the way.
As a small bonus you can also raycast towards the ground in case you want to check for ledges or places where the zombie could fall off as well.

If a raycast hits a wall in the direction the character is moving you can assume that you most likely won’t be able to walk through it and then use pathfinding.

While your character follows the path, you can continue raycasting so that if the obstacle is avoided and the target is in a direct line of sight again, you can continue walking straight forward.

Thanks for the reply!
My main concern is that I intend to add formations to the dummies, meaning that if there are 16 of them they will walk around in a 4x4 grid together.
Not sure whether or not raycasting would work in that scenario?
If it would I’m not seeing how.

If you’re concerned about dummies running into each other just parent them to a folder/model and add it to the blacklist or make a whitelist for only the things you want the raycast to detect.

If you want dummies to follow each other, you could make them search for the nearest dummy, make that dummy the leader of the pack and just have them follow that dummy with an offset position.

Originally I tried doing something similar to that.
I calculated the original path and made all the dummies walk to the waypoint + an offset.
They kept falling over while it was happening, and/or giving up, and not making it to the end.
Will definitely try the blacklist idea.

1 Like