How do I make it so that the car's position on the road stays the same as before?

I have a self-driving car script, and I made the car follow the road’s orientation at curves. It works well, but the problem is that as it follows the curve, the car slowly drifts away from the original position, sometimes as far as crossing the center line.


The car’s original position is 4.83 studs away from the left edge of the road.

Could someone help me or give some advice on how I could I maintain it’s position?
Here’s the script:

local Debounce = false

function WheelDetection(hit)
	if hit.Material == Enum.Material.Concrete  and not Debounce then
		Debounce = true
		if hit.Orientation == Vector3.new(0,0,0) then
		else
			local Dot = script.Parent.Parent.PrimaryPart.CFrame.RightVector:Dot(hit.CFrame.LookVector)
			script.Parent.Parent:SetPrimaryPartCFrame(CFrame.new(script.Parent.Parent.PrimaryPart.CFrame.Position)*CFrame.Angles(0,math.rad(hit.Orientation.Y),0))					
			if Dot < 0 then		
			  script.Parent.Parent:SetPrimaryPartCFrame(script.Parent.Parent.PrimaryPart.CFrame*CFrame.Angles(0,math.rad(-90),0)) --Left curve
			else
			  script.Parent.Parent:SetPrimaryPartCFrame(script.Parent.Parent.PrimaryPart.CFrame*CFrame.Angles(0,math.rad(90),0)) --Right curve
			end
		end
		wait(0.2)
		Debounce = false
	end	
end	
script.Parent.Parent.Wheels.FL.Touched:Connect(WheelDetection)
1 Like

Best way possible is checking how far you are from the white stripes and the edge of the road to determine how much you need to steer to stay on position.

The white stripes are a texture, not a part. Also I’m not steering the car, I’m merely setting it’s orientation.

Well in that case find the distance between the edge of the road and the car to change the rotation depending if you are closer or further away.

When you build the road, is it constructed from duplicated parts that are rotated to make the curve?

Kind of like this?

Yes, it is.
(useless text to meet the characters requirement)

I suggest making a triangle using three points the part’s position, the edge relative to the part’s position and the lookvector of the parts position. (The part is the car’s primary part)

if you wanted it to drive down the middle with the detection part shouldn’t you just be able to expand the detection part to be a little bigger on each side or would that not work

I don’t think so. The “detection part” in this case is the left front wheel.

oh ok from the video it looks like the detection part is the big part sticking out infront of the car

You are correct, but it’s for a different purpose not related to the curve.

You mean a literal triangle (as in one made up of parts)?

Basically getting the angle can help you to determine how much to rotate to stay on the same distance like so;

—If it is not a unit vector
function Angle(vectorA, vectorB)
   return math.acos(vectorA:Dot(vectorB) / (vectorA.Magnitude * vectorB.Magnitude))
end
— If it is a unit vector 
function UnitAngle(vectorA, vectorB)
   return math.acos(vectorA:Dot(vectorB))
end

Unit Angle( (edge.Postion-part.Postion).Unit,part.Cframe.Lookvector)
1 Like

Not a literal triangle but like a fictional triangle that is used to only find the angle and nothing else.

It’s not rotation that is the problem, it’s the position. The rotation part of the script works perfectly as intended, it sets the car to the road’s orientation. What I’d like to know is how I could incorporate the road edge offset into that line? (Line 8)

How are you moving the car by it’s lookvector or something else that is not using the car to help move itself.

It’s the A-Chassis script if you don’t know. It has a loop that spins the wheels according to the tune if the _GThrot value is more than 0 (1 in my case).

So based off my order inference the chasis script might move depending on the lookvector of the part because of how the wheels work. Also from looking back on the video it seems as if your car is not rotating on every turn because it might not detect the touch.

Put a print on the function, count how many road parts there are and see if it touched them all to confirm it is turning when touched.

I tried using a loop instead of Touched, but it didn’t help. Same position problem.