Hey there! I would like to make a car that can drive itself and keep itself on the right side of the road for my upcoming game.
The methods I am trying and know of are unreliable and difficult to use in a proper game without bugs.
At first I tried tinkering with Neural Networks but there is little doccumentation on acatually making a car of it so I tried 2 solutions, using the .touched
event to detect whether it is on a road and whether it needs to turn (which is unreliable because it would only work in perfect conditions) and I also tried a system that uses CFrame
to try to steer to the nearest point but it is janky and eventually runs off course
Touched Method
local car = script.Parent
local base = car.Base
local seat = car.Base.VehicleSeat
local frontWheel = car.WheelFR.Wheel.Hub.WeldConstraint.Tire
local last
function turn(current, last)
print("Last = ",last.Orientation)
print("Current = ",current.Orientation)
local difference = current.Orientation.Y - last.Orientation.Y
print("Difference = ",difference)
end
frontWheel.Touched:Connect(function(hit)
if hit.Parent == workspace.Road then
seat.Throttle = 1
if last then
turn(hit, last)
end
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)
CFrame Method
local last
local cf
local car = script.Parent
local base = car.Base
local seat = base.VehicleSeat
local road = workspace.RoadMK2
local nodes = road.Nodes
-- Build Path
for i,v in pairs(nodes:GetChildren()) do
v.Transparency = 1a
v.CanCollide = false
local attach = Instance.new("Attachment")
attach.Parent = v
if last then
local rod = Instance.new("RodConstraint")
rod.Attachment1 = v.Attachment
rod.Attachment0 = last.Attachment
rod.Visible = true
rod.Parent = v
end
last = v
end
-- Run
last = nil
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.35
else
seat.SteerFloat = cf.Y*0.35
end
end
wait()
until mag > 2 and mag < 7 or mag > 23
seat.Steer = 0
seat.Throttle = 0
--print(mag)
last = v
end
Not sure how I can do this. I really don’t want to use tweens as it would look un-natural. I want to make something on the fly but I just don’t know how.
If someone could give me some tips it would be greatly appreciated