# Self driving car issue

I tried to make a car that follows a road with curves and stuff, however, there are two main issues that I don’t know how to solve.

1. It doesn’t follow the road correctly. As you can see in the video below, it goes offroad.
2. The wheels are in the ground. I don’t know how to align the wheels to the road.
Keep in mind for the second issue, the wheels need to adjust to different heights, such as uphill/downhill.

Video example:

Script:

``````local car = script.Parent
local speed = 10
local resolution = 150
local path = game.Workspace.Path
local waypoints = {}

local pathPoints = path:GetChildren()

function modelvec(model)
local cf = model:GetPivot()
return Vector3.new(cf.x, cf.y, cf.z)
end

function cftovec(a)
return Vector3.new(a.x, a.y, a.z)
end

function vectocf(a)
return CFrame.new(a.x, a.y, a.z)
end

for i = 1, #pathPoints - 1 do
local startPoint = modelvec(pathPoints[i])
local endPoint = modelvec(pathPoints[i + 1])
local direction = (endPoint - startPoint).Unit
local distance = (endPoint - startPoint).Magnitude

for j = 1, resolution do
local factor = j / resolution
local position = startPoint + direction * (distance * factor)
table.insert(waypoints, position)
end
end

local cWI = 1

game:GetService("RunService").Heartbeat:Connect(function()
local currentWaypoint = waypoints[cWI]
local nextWaypoint = waypoints[cWI + 1]
local direction = (nextWaypoint - currentWaypoint).Unit
local distance = (nextWaypoint - currentWaypoint).Magnitude

local f = speed / distance

local newPosition = currentWaypoint:lerp(nextWaypoint, f)

local leftOffset = CFrame.new(direction * 5) * newPosition

local rotation = CFrame.new(newPosition, newPosition + direction) * CFrame.new(0, math.pi / 2, 0) * CFrame.Angles(0, 0, 0)

car:PivotTo(vectocf(leftOffset) * rotation)

if f >= 1 then
cWI = cWI + 1

if cWI > #waypoints then
cWI = 1
end
end
end)
``````
1 Like

Where are your waypoints? If they are at ground level then the Humanoid in the Roblox Jeep is going to try to get it’s height to where the waypoint is.
I believe the suspension on those Jeeps is set up with raycasting. If the vehicle is already in the ground then the suspension is going to push up as far as the raycasting section of the script will allow.
Put an anchored, cancollide false Part at each of your waypoints to see where they actually are.

Also, where are the Pivot points of the road sections? Are they offset or at the actual center of the Parts? You can see this if you select the road with your Move tool and you’ll see where the Pivot tool is centered. To reset it go to your pivot editor and reset the pivot.

2 Likes

Lmk if you can’t see the video. The green parts are the startPoints, the brown parts are the endPoints, and the red parts are the waypoint positions.
I guess I need a better calculation for the waypoints?

I’d say that’s a good starting point.

This may be helpful to people who stumble upon this post.

Sorry for being late, but you should use nodes for this. I recommend Sleitnick’s Node Mapper Plugin

You place nodes along the road, with like 10 studs between each one, and connect them. Then use the A* Module included with the plugin to find the shortest path, and create a script to make the vehicle follow it.

Here’s a script which makes an NPC follow the path, (as shown in the video.)

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

local char = workspace:WaitForChild("Noob")
local humNPC = char:WaitForChild("Humanoid")

local start = Vector3.new(char.HumanoidRootPart.Position.X, char.HumanoidRootPart.Position.Y - lowNumber, char.HumanoidRootPart.Position.Z)
local goal = game.Workspace.End.Position -- Position of part named "End" in workspace.

local path = pathfinder:FindPath(start, goal) -- Finds nearest path using the module.

if (path) then
table.insert(path, 1, start)
table.insert(path, goal)
for i = 2,#path do
local p1 = path[i - 1]
local p2 = path[i]

humNPC:MoveTo(p2)
humNPC.MoveToFinished:Wait()
end
else
warn("Failed to find Path.")
end
``````
2 Likes

bro’s gonna get his post taken down for necrobumping

Sorry, I just wanted to help if he was still struggling with the issue, or for other people who stumbled upon the post while finding a solution, like I did.

1 Like