Need guidance with basic vehicle ai

okay so, long story short

ive been into trying to code vehicle ai since pretty much a year or two ago, unfortunately with no, if not partial success

ive gotten some of the logic down, i just cant seem to figure out some things, one being mainly figuring out steering (or at least smoothing it out)

(oh and by the way, im working with the default roblox chassis, the one with the -1 to 1 steering or throttle values)

ive tried a lot of things, from dampening, to straight up trying to learn things like vehicle kinematics for this, with aaaalmost no success

dampening does help, but theres still problems with it

the car either gets off the line too much to the point of divebombing the racing line (or the waypoints), oversteers or understeers
in some rare cases, it even starts swerving while following the line for a reason that i cant seem to figure out

ive tried searching through the devforum or even the internet to see if i could find anyone having a similar problem but almost all of them just lead to people saying that they should use pathfinding, lerping cframes, some sort of neuro networks that i dont understand even a little or whatever which is not what im looking for at all (or i simply didnt search deep enough, dunno)

all i need is to figure out the steering, thats it.
just calculations for setting some steering value from -1 to 1

if anyone with experience could guide me with this (links, forum pages, youtube videos, legit anything), id heavily heavily HEAVILY appreciate, because ive been doing this for months with almost no success at ALL.

apologies if this isnt the correct place to be asking for this, or if i didnt explain myself correctly, im just desperate at this point

if required, i can send sections of the code if anyone is willing to help
if you also need explanation on some things that i said please quote them

1 Like

What are you using to steer the car since we don’t know exactly which ‘default roblox chassis’ you are using.
Post the script since you are asking for help with it. Put 3 backticks (```) before and after your script so it formats properly here.

If the steering is controlled by Servo HingeConstraint steering Parts, try decreasing their AngularSpeed so they don’t jerk back and forth with the 1, -1 inputs.
Since you are dealing with an AI car you could also steer directly with the Servo TargetAngle instead of the 1, -1 inputs by finding out where the car needs to steer to, then aiming the TargetAngle at that Position.

1 Like

Okay well lemme clear out some things first cuz some are wrong

answering your question, its the basic chassis that uses the vehicleseat instance

ok now time to clear out some things

the steering is not entirely bad, its sort of okay, and if i were to do just around 2-3 ai cars they could run for a lot of laps. but the problem starts in a bigger pack of cars where the swerving becomes a problem

for example, in this video:

you can see that the cars swerve a lot on the turns, and as i mentioned they sort of understeer and oversteer in some cases if they get too off the line

ive been looking for ways to diminish this swerving since it still happens with dampening, and if i were to increase or decrease the dampening then the steering would become more uncontrollable

as for the code, its sort of long so i cant post all of it, but i can post the loop and the function that handle the main steering logic

function turn(dif, dist, radius, smallTurn, mediumTurn, turnD, LowTurn, MedTurn, HighTurn)
	local t = 0
	local absDif = math.abs(dif)

	if not InTurn then
		if absDif <= smallTurn then
			seat.TurnSpeed = LowTurn
		elseif absDif <= mediumTurn then
			seat.TurnSpeed = MedTurn
		else
			seat.TurnSpeed = HighTurn
		end
	else
		if absDif >= mediumTurn then
			seat.TurnSpeed = HighTurn
		elseif absDif >= smallTurn then
			seat.TurnSpeed = MedTurn
		end
	end
	
	if not (absDif < math.deg(math.atan2(radius, dist))) or not (absDif < turnD) then
		if dif > 0 then
			t = 1
		else
			t = -1
		end
	end
	seat.Steer = t
end

while true do 
	local smallTurn = script.Parent.SmallTurn.Value
	local mediumTurn = script.Parent.MediumTurn.Value
	local goDegrees = script.Parent.GoDegrees.Value
	local turnD = script.Parent.TurnDampenDeg.Value
	local speedD = script.Parent.SpeedDampenDeg.Value
	local TurnValues = script.Parent.TurnValues
	
	TurnChecking()

	if (seat.Position - lastPos).magnitude < 1 and not script.Parent.Stopped.Value then 
		check = check + 1
	else
		check = 0
	end
	
	switchLane()
	
	local radius = cTarget.Size.x
	local dist = (seat.Position - cTarget.Position).magnitude
	local proximityMultiplier = 3.25

	if dist < radius * proximityMultiplier then
		tNum = tNum + 1
		if tNum > #currentline then tNum = 1 end
		cTarget = currentline[tNum]
	end
	
	
	local one = getRotation(seat.CFrame, true) 
	local two = getRotation(CFrame.new(seat.Position, cTarget.Position), true)
	
	if one - two > 360 then
		two = two + 360
	elseif one - two < -360 then
		one = one + 360
	end
	
	local dif = one - two
	local ak = math.floor(dif + 0.5)
	
	if dif > 180 then
		dif = dif - 360
	elseif dif < -180 then
		dif = dif + 360
	end
	
	if math.abs(dif) > 180 then
		if dif < 0 then 
			dif = 360 + dif
		elseif dif > 0 then
			dif = 360 - dif
		end
	end
	
	if check == 20 then
		turnD = 0
		turn(dif, dist, radius, smallTurn, mediumTurn, turnD, TurnValues.LowTurn.Value, TurnValues.MediumTurn.Value, TurnValues.HighTurn.Value)
		check = 0
		seat.Throttle = -1
		seat.Steer = seat.Steer * -1
		task.wait(1)
		turnD = script.Parent.TurnDampenDeg.Value
	end
	
	lastPos = seat.Position
	turn(dif, dist, radius, smallTurn, mediumTurn, turnD, TurnValues.LowTurn.Value, TurnValues.MediumTurn.Value, TurnValues.HighTurn.Value)
	if DebugPrints ~= false then
		print("checkpoint Dif value is:", dif)
	end
	regSpeed(math.abs(dif), goDegrees, speedD)
	task.wait(0.1)
end 

now again, this all could maybe just be because my cars are incredibly easy to be spun around, or the wheel grip setup they have is not fit for this kind of racing, but ive mainly seen that its the steering thats causing all of this.

by the way, this is not 100% my code, its taken from an old model that a person gave me in dms which was based off of a mario kart game, and edited to fit my needs

1 Like

Have you tried playing with the Density and Friction of the wheels? I find this is one of the things most missed by people trying to tune a vehicle’s movement.
Could the other issue be the difference between smallTurn and mediumTurn being switched at speed? By that I mean if small is 50% of medium and switching from one to the other at that critical moment is causing the car to suddenly swerve more which throws it out of control. Try experimenting with different values to see if it makes it better or worse.

you might be right actually, ill try this and update you about it

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.