Is it possible to make pathfinding waypoints follow and stay above specific parts, and if so, how?


So I’ve been writing an AI car system for a project, and everything was going well until I discovered pathfinding. I’d like the pathfinding waypoints to follow parts called “road”, and stay on their right side. Currently the pathfinding is strange, and for some reason wants to make the car hit a give way sign, despite the destination being in a straight line, in the vehicle’s lane.


I’ve tried putting walls on the edge of the road, and that seemed to fix things a little, but the waypoints are sunken into the road, and I’d like them to be slightly higher (because directly on top or lower would make the car look as if it was sinking in the road). Putting edge walls would also take lots of time and would quickly rack up the part count.
I’m wondering if there’s a better way than just plain edge walls.
Anyways, here’s my code:

local module = {}


wait(5)

local PathfindingService = game:GetService("PathfindingService")

-- Variables for the car and destination, etc.

module.CreatePath = function(par)
local carprimary = par
local destination = game.Workspace.Destinations.South
local path = PathfindingService:CreatePath()

path:ComputeAsync(carprimary.Parent.Body.Position, destination.Position)

local waypoints = path:GetWaypoints()

for _, waypoint in pairs(waypoints) do
	local part = Instance.new("Part")
	part.Shape = "Block"
	part.Material = "Neon"
	part.Size = Vector3.new(17.95, 4.081, 2)
	part.Position = waypoint.Position
	part.Anchored = true
	part.CanCollide = false
	part.Parent = par.Points

end
end
return module
2 Likes

add a wall each side of the road

PathfindingService is designed for humanoid locomotion and therefore does not come equipped with this capability by default. What you can do is not follow the waypoints exactly but follow the road that will get you the furthest towards the waypoint. If you recalculate the pathfinding constantly it will probably eventually get you to your destination as long as that destination is on the road.

I say probably because there are still ways to trap the car. You will have to bring out a full A* implementation if you want to do this optimally.

That’s what I’ve already tried. My project has dozens of roads. If you were me, would you really be bothered to put walls on every single one of them?

not really, i don’t have many projects with pathfinding

Oh, so I’d need to place waypoints (or their location positions) at junctions, slopes and curves? Then have a script calculate the shortest route and duplicate/insert the waypoints into the car’s “Points” model? Would this be efficient, or is there a better way?

Maybe some of the waypoints that go through the sign are actually jump nodes.
Can you try passing AgentCanJump=false to CreatePath()?

If you want to share your repro place, we can take a look too.

Try messing with waypoint spacing, or make the car just follow directly to the destination when it is visible (using a raycast, maybe)

you could also try doing a full pathfinding system from scratch but it would be alot of work

roblox’s pathfind is really messy in these configuration things, so you can’t really adapt it for a car