Hello, It’s me again.
I am trying to make a car that drives itself for my upcoming game that needs to follow the roads, interact with players and stop at traffic lights.
All the methods I have tried either
A - Go off course after a bit and get lost
B - Is fixed so all turnings have to be the same size and ends up driving off the map
C - Can’t navigate to it’s marker
D - Don’t move in the first place
(NOTE : All of this work here is only to follow the road)
I have tried to use .Touched to see if it is on a road and whether it needs to turn which lead to result B.
I also tried getting it to run through a series of nodes and either turn left or right a bit to to stay on track which eventually runs off course and gets too far away from the node for the script to mark the destination as reached (Result A) I tried another system more the machine learning side where if it hit a wall, it would leave a marker there and respawn but the issue was that cars don’t have built-in pathfinding and I wasn’t able to produce pathfinding so it couldn’t navigate to it’s point (Result C). Finally as a last resort I tried tweening it (which I really don’t want to do as it doesn’t look real) but that didn’t move at all (Result D) (I would not like help with tweening, I don’t think it is practical for this use)
My code:
Node tracking:
for i,v in pairs(nodes:GetChildren()) do
local mag
repeat
mag = (seat.Position - v.Position).Magnitude
seat.ThrottleFloat = 0.7
--print(mag)
if last then
cf = CFrame.new(last.Position, v.Position)
--print(cf)
print(v.Attachment.WorldPosition.X)
print(last.Attachment.WorldPosition.X)
if v.Attachment.WorldPosition.X > last.Attachment.WorldPosition.X then
seat.SteerFloat = math.abs(cf.Y)*-0.3
else
seat.SteerFloat = cf.Y*0.3
end
end
wait()
until mag > 2 and mag < 7 or mag > 20
--seat.Steer = 0
seat.ThrottleFloat = -0.3
repeat wait() until mag > 2 and mag < 7
seat.Throttle = 0
--print(mag)
last = v
end
Machine Learning:
script.Parent.Hitter.Touched:Connect(function(hit)
if hit.Parent ~= workspace.RoadNodes then
seat.Throttle = -0.2
wait(1)
seat.Throttle = 0
table.insert(points, {script.Parent.NodeCreator.Position.X, script.Parent.NodeCreator.Position.Z})
end
end)
Check to see if it’s on road:
frontWheel.Touched:Connect(function(hit)
if hit.Parent == workspace.Road then
seat.Throttle = 1
if hit.Name == "Left" then
seat.SteerFloat = -0.269067
elseif hit.Name == "Right" then
seat.SteerFloat = 0.269067
else
seat.SteerFloat = 0
end
last = hit
else
seat.Throttle = -2
end
end)
Tweening:
for i,v in pairs(nodes:GetChildren()) do
print(i)
if last then
cf = CFrame.new(last.Position, v.Position)
local distance =(v.Position - seat.Position).Magnitude
print(distance)
tweenService:Create(base, TweenInfo.new(distance,Enum.EasingStyle.Linear), {CFrame = v.CFrame}):Play()
local cr = coroutine.create(function()
print(base.Position)
end)
coroutine.resume(cr)
wait(distance/2)
end
last = v
end
I’ve just about had it with these cars. If anyone could help me with Self-Driving cars it would be very much appreciated. I’ll say it again down here, I’d rather not use the tweening method.
Thanks.