Connect 2 parts without a gap

I’m making a waypoint system like GTA 5, but the waypoint parts look really weird when the road turns a bit too much. Here’s some examples.

image
image
image

Here’s a snippet of my code.

local pathfinder = require(script.Pathfinder)
pathfinder:Init()

local pathwidth = 25 -- studs

wait(5)

--local start = workspace.WaypointParts.Start.Position

while wait(.5) do
	
	if workspace:FindFirstChild("VisualPath") then
		workspace.VisualPath:Destroy()
	end
	
	local start = workspace:WaitForChild("Rixxles").HumanoidRootPart.Position
	local goal = workspace.WaypointParts.End.Position

	local path = pathfinder:FindPath(start, goal)

	if (path) then
		table.insert(path, 1, start)
		table.insert(path, goal)
		local pathfolder = Instance.new("Folder", workspace)
		pathfolder.Name = "VisualPath"
		for i = 2,#path do
			local p1 = path[i - 1]
			local p2 = path[i]
			local p = Instance.new("Part", pathfolder)
			p.CanCollide = false
			p.Anchored = true
			p.TopSurface, p.BottomSurface = Enum.SurfaceType.Smooth, Enum.SurfaceType.Smooth
			p.Color = Color3.fromRGB(192, 3, 255)
			p.Size = Vector3.new(pathwidth,1, (p2 - p1).Magnitude)
			p.CFrame = CFrame.new(p1:Lerp(p2, 0.5), p2)
		end
	else
		warn("Failed to find Path.")
	end
end

How can I prevent / make something to fix this?

1 Like

I really only use regular pathfindingservice, so if this is anything different i’m not too sure but. The way I visualise the path is like this:

local pfs = game:GetService("Pathfindingservice")
local path = pfs:CreatePath()
path:ComputeAsync(start, goal)

for every, waypoint in path:GetWaypoints() do
local p = Instance.new("Part", pathfolder)
			p.CanCollide = false
			p.Anchored = true
			p.TopSurface, p.BottomSurface = Enum.SurfaceType.Smooth
			p.Color = Color3.fromRGB(192, 3, 255)
			p.Size = Vector3.new(pathwidth,1, (p2 - p1).Magnitude)
p.Position = waypoint.Position + Vector3.new(0,2,0)
end
1 Like

quick update I think it may be due to how you are using the for loop, why not do path:GetWaypoints() and see how that turns out?

It’s a custom made module, not Roblox’s pathfinding service so it has different functions.

I’ll try this and let you know how it works.

But do you get the waypoints using the default path:GetWaypoints() thing in the module?

Alright then. Good luck.

30char

I don’t think that’d work due to you using regular pathfindingservice

Well it’s worked for me alot, here’s an example video of an NPC I made with regular pfs just now with a little turn:

Screenshot 2024-06-08 113648

Originally, you were right. it didn’t work. But what I changed was the waypoint spacing. The default is 4, but I changed it to 6 through AgentParameters. Seen here:

local pfs = game:GetService("PathfindingService")

local path = pfs:CreatePath({
	AgentRadius = 2,
	AgentHeight = 5,
	AgentCanJump = true,
	AgentCanClimb = false,
	WaypointSpacing = 6 -- Change waypoint spacing to allow clean turns
}
)
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, workspace.Goal.Position)
local wps = path:GetWaypoints()

for i, wp in wps do
	script.Parent.Humanoid:MoveTo(wp.Position)
	local part = Instance.new("Part", workspace)
	part.Anchored = true
	part.CanCollide = false
	part.Shape = Enum.PartType.Ball
	part.Material = Enum.Material.Neon
	part.Position = wp.Position + Vector3.new(0,2,0)
	script.Parent.Humanoid.MoveToFinished:Wait()
end

By the way this isn’t how I really code I just made it in the moment and did it quickly for you.

Also you could make improve this even more by visualising the waypoints after movetofinished has been completed.

1 Like

Ohh, what I’m trying to talk about, is when 2 lines intersect, I want them to connect in the further corners. This is how it looks like now:

What I want it to look like:

(I used resizealign to do this, but idk how I’d do it in the code.)

Yeah I think you can do that with the use of waypoint spacing. To get it closer and then use some sort of other function.

What I’m using right now are nodes, which the pathfinding module uses A* to find the nearest node closest to the player, and draws a part from the player to that node, then continues to make another part from that node, to the next one, slowly getting to the destination.

Ah, never used nodes before. But can’t you use waypoint spacing in that too? I assume you could.

Well, nodes are basically parts which are welded (connected) together (as an example) which are written into values instead of physical parts.

The system finds the nearest node which is connected (welded) to the current node, this is because if the player is driving on a road, I don’t want it to direct the player offroad as a shortcut on a corner.

I placed the nodes myself instead of using code for it.

Here’s an example of the system.

Here’s the plugin which I used to create the nodes, and an example of how it works.

1 Like

As shown in the plugin video, I created nodes which are connected together. Some are, some aren’t. For example, these are some connected nodes at an intersection.

image

This is what the path would look like once it’s calculated.
image

If I didn’t use nodes, it would look something like this.
image

Can’t you use a costs table in the agent parameters to make the path traverse over more realistic materials like the road?

I’m going to use the same road material on other objects too, and I’m also doing this with dirt paths, which uses the same material as mud.

Ah alright, well then I saw in your code you have something called “pathwidth” maybe you can adjust that?

That just changes the width of the part, not length

It’s currently the width of the roads