How do you make an AI Pathfinding Car?

So I’ve been wanting to make a pathfinding car, but I just don’t know how to do it. Theres little people who’ve done this recently, the only resources that I can find date back to years ago.

So my problem is that when I use carprimary:MoveTo(waypoint.Position), the car just teleports to the waypoints:

So the logical thing to do here is use a Humanoid right? Tried it. Failed. Car didn’t move, at all. So scrap the humanoid car idea, onto Tween.

So funny thing, when I try to tween the car, the body literally gets grabbed by some invisible god. It looks funny but isn’t what I’m trying to achieve.

The Tween basically does the same thing as the Humanoid, except that it literally yoinks the base of the car instead of just not doing anything to the car.

So all of my methods at making the car simulate a real player driving it, has failed. My main goal here is to make the car pathfind to a goalblock, but look like its actually driving.

Heres my script (has both tweenservice and moveto in there but one of them is commented out):

wait(5)

local PathfindingService = game:GetService("PathfindingService")
local TweenService = game:GetService("TweenService")

-- Variables for the car and destination, etc.
local car = script.Parent.VehicleSeat
local carprimary = script.Parent
local destination = game.Workspace.pathfindgoal


local path = PathfindingService:CreatePath()

path:ComputeAsync(carprimary.PrimaryPart.Position, destination.Position)

local waypoints = path:GetWaypoints()

for _, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace

--carprimary:MoveTo(waypoint.Position)
local tween = TweenService:Create(carprimary.PrimaryPart, TweenInfo.new(1), {Position = Vector3.new(waypoint.Position)})

tween:Play()
wait(1)
end

This is what the workspace looks like:
image

5 Likes

Do you mind providing a video of what the car is doing while using tween serice?

1 Like

i did, and its literally nothing. it just doesn’t work. if it does work, then its tearing off the base of the car. look before i show my script

1 Like

Oh, I’m sorry! I didn’t see the files.

1 Like

wouldn’t velocities fix this because Tweening a car is just tbh a not good method

1 Like

Try welding all of the parts to the primary part. Then see if the whole model moves with. If weld’s don’t work use Motor6D’s or weld constraints.

And the reason why your pathfinding is being weird is because you have

local tween = TweenService:Create(carprimary.PrimaryPart, TweenInfo.new(1), {Position = Vector3.new(waypoint.Position)})

--Change to:
local tween = TweenService:Create(carprimary.PrimaryPart, TweenInfo.new(1), {Position = waypoint.Position})

The position is a Vector3, and your trying to set an x value to a Vector3. That’s why it’s setting its position to 0, 0, 0.

4 Likes

Oh hey! The tween works. Thanks! Now all thats needed to do is somehow work out the welds like you said.

No problem! I recommend using [Moon Animator]( Moon Animator - Roblox) to weld your parts.
To make welds click the wrench icon when you have installed Moon Animator. Or you could weld all the parts your self which is very time confusing in my opinion.
image

2 Likes

I just accidently uploaded the wrong link to moon animator. Make sure to use the new link.

1 Like

i’ve tried out the plugin but the wheels and vehicleseat continue to stay in place. dont know why that is. should i have the base part anchored or unanchored?

Probably anchored. Make sure you have the Welds under the PrimaryPart.
For example: image

1 Like

oh it works! thanks a lot man… but do you know how i can make it look like its on the baseplate? because right now its kinda like plowing through it

No problem! I’m glad to help. For your issue, you could try lowering the destination and the model to the same Y position. That might make it look better.

1 Like

hm tried that but it seems like the pathfinder still has a preference for making the car plow through the ground

It seems like the pathfinding is trying to level it out on the parts under it. Try putting this in your pathfinding script.

carprimary.PrimaryPart.Changed:Connect(function()
    carprimary.PrimaryPart:SetPrimaryPartCFrame(CFrame.new(carprimary.PrimaryPart.Position.X, destination.Position.Y, carprimary.PrimaryPart.Position.Z))
end)

It might work, I haven’t tested it so I don’t know for sure.

2 Likes

it works… but the result is the car drifting its way to the goalblock. looks really funny lol. but its not exactly facing the block.
image
wonder if i could make a script that makes the car face the block after every waypoint tween

Oh sorry I messed up the rotation.

carprimary.PrimaryPart.Changed:Connect(function()
    local NewCFrame = CFrame.new(carprimary.PrimaryPart.Position.X, destination.Position.Y, carprimary.PrimaryPart.Position.Z) * CFrame.Angles(math.rad(carprimary.PrimaryPart.Orientation.X), math.rad(carprimary.PrimaryPart.Orientation.Y), math.rad(carprimary.PrimaryPart.Orientation.Z))
    carprimary.PrimaryPart:SetPrimaryPartCFrame(NewCFrame)
end)

If the rotation is weird then I probably messed up the rotation. I have multiple of methods of achieving the rotation. If that script doesn’t work, I’ll list all of the methods I am currently thinking of.

3 Likes

my guys got a whole library of rotation methods in his brain. i’ll try it out lol

1 Like

yep it works. thanks for the help man!

No problem! I’m glad to help you.

2 Likes