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.
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
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:
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.
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.
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.
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.
This is what the path would look like once it’s calculated.
If I didn’t use nodes, it would look something like this.