I have this script, its purpose is to guide a car alongside a path of checkpoints, however the wheels do not turn where I need them to, video showcasing the issue. I am bad at math, the calculation that is currently being done is copied from a similar topic, so any mathematical replies that aren’t the corrected code I would not understand.
local checkpoints = game.Workspace.CarCheckpoints
local Force = 0
local MaxSteer = 15
local MaxSpeedKM = 50
local car = script.Parent.Parent
local seat = script.Parent
local leftMotor = car.FrontLeftWheel.LeftMotor
local rightMotor = car.FrontRightWheel.RightMotor
local leftServo = car.BackleftWheel.LeftServo
local rightServo = car.BackRightWheel.RightServo
local VF = car.Base.VectorForce
local VF2 = car.Base.VectorForce2
local targetCheckpoint = nil :: BasePart
local speedSet
function goToCheckpoint(checkpointName)
print(checkpointName)
targetCheckpoint = checkpoints:FindFirstChild(checkpointName)
speedSet = seat.ThrottleFloat * Force
targetCheckpoint.Touched:Connect(function(hit)
if hit and hit.Parent == car then
if checkpoints:FindFirstChild(checkpointName + 1) then
goToCheckpoint(checkpointName + 1)
else
targetCheckpoint = nil
end
end
end)
end
goToCheckpoint("1")
game["Run Service"].Heartbeat:Connect(function()
if targetCheckpoint ~= nil then
local checkpointPosition = targetCheckpoint.Position
local seatPosition = seat.Position
local deltaX = checkpointPosition.X - seatPosition.X
local deltaZ = checkpointPosition.Z - seatPosition.Z
local angle = math.deg(math.atan2(-deltaX, -deltaZ))
print(angle)
leftMotor.TargetAngle = angle
rightMotor.TargetAngle = -(angle)
speedSet = 0.8 * Force
local speed = math.max(math.abs(seat.AssemblyLinearVelocity.X), math.abs(seat.AssemblyLinearVelocity.Z))
if speed > MaxSpeedKM then
VF.Force = Vector3.new(speedSet-speedSet/3,0,0)
VF2.Force = Vector3.new(speedSet-speedSet/3,0,0)
else
VF.Force = Vector3.new(speedSet,0,0)
VF2.Force = Vector3.new(speedSet,0,0)
end
end
end)
What do the variable you created represent, I don’t understand nearly enough about math to figure out what they are
I also made this code:
local checkpoints = game.Workspace.CarCheckpoints
local Force = 4000
local MaxSteer = 15
local MaxSpeedKM = 50
local car = script.Parent.Parent
local seat = script.Parent
local leftMotor = car.FrontLeftWheel.LeftMotor
local rightMotor = car.FrontRightWheel.RightMotor
local leftServo = car.BackleftWheel.LeftServo
local rightServo = car.BackRightWheel.RightServo
local VF = car.Base.VectorForce
local VF2 = car.Base.VectorForce2
local targetCheckpoint = nil :: BasePart
local speedSet
function goToCheckpoint(checkpointName)
print(checkpointName)
targetCheckpoint = checkpoints:FindFirstChild(checkpointName)
speedSet = seat.ThrottleFloat * Force
targetCheckpoint.Touched:Connect(function(hit)
if hit and hit.Parent == car then
if checkpoints:FindFirstChild(checkpointName + 1) then
goToCheckpoint(checkpointName + 1)
else
targetCheckpoint = nil
end
end
end)
end
goToCheckpoint("1")
game["Run Service"].Heartbeat:Connect(function()
print(targetCheckpoint)
if targetCheckpoint ~= nil then
local checkpointPosition = targetCheckpoint.Position
local seatPosition = seat.Position
local hyp = math.sqrt((seatPosition.X - checkpointPosition.X)^2 + (seatPosition.Z - checkpointPosition.Z)^2)
local sad = seatPosition.X - checkpointPosition.X
local angle = math.deg(math.asin(sad/hyp))
leftMotor.TargetAngle = angle
rightMotor.TargetAngle = -(angle)
speedSet = 0.8 * Force
local speed = math.max(math.abs(seat.AssemblyLinearVelocity.X), math.abs(seat.AssemblyLinearVelocity.Z))
if speed > MaxSpeedKM then
VF.Force = Vector3.new(speedSet-speedSet/3,0,0)
VF2.Force = Vector3.new(speedSet-speedSet/3,0,0)
else
VF.Force = Vector3.new(speedSet,0,0)
VF2.Force = Vector3.new(speedSet,0,0)
end
end
end)
which when setting the force to 0 correctly orientates both wheels, but when setting the force to the indented amount the vehicle immediately starts spinning on the spot
x1 and y1 would be the wheel you are NOT getting the angle of, x2 and y2 would be the angle of the wheel you ARE getting the angle of, x3 and y3 would be your checkpoint position.