So, I’m new to making cars and I’m wondering if I’m doing this wrong or if there is an easier way to accomplish car physics. Currently The car can’t go backwards, floats in air (I already know why, but don’t know the solution.), and is just very buggy. Any help and tips will help.
Code:
local Seat = script.Parent.VehicleSeat
local acceleration = 0
local AmountofTires = 0
local CollectionService = game:GetService("CollectionService")
function UpdateSpeed(Part)
local Deceleration = AmountofTires / 4
local Rotation = .5 * acceleration
local GOOOO = acceleration / 4
local Weld = Part:FindFirstChildWhichIsA("ManualWeld")
if Seat.ThrottleFloat == 1 then
Part.AssemblyLinearVelocity = -Part.CFrame.LookVector * Seat.Throttle * GOOOO * AmountofTires
else
Part.AssemblyLinearVelocity = Part.CFrame.LookVector * Seat.Throttle * GOOOO * AmountofTires
end
Weld.C1 = Weld.C1 * CFrame.Angles(0, 0, math.rad(Rotation))
end
while true do
wait()
AmountofTires = 0
AmountofTires += #CollectionService:GetTagged("Tire")
local Deceleration = AmountofTires / 4
if Seat.ThrottleFloat ~= 0 then
if Seat.ThrottleFloat == 1 then
if acceleration < Seat.MaxSpeed * Deceleration then
acceleration += 1 * Deceleration
end
else
if acceleration < -Seat.MaxSpeed * Deceleration then
acceleration -= 1 * Deceleration
end
end
else
if acceleration > 1 then
acceleration -= 1
end
if acceleration < 1 then
acceleration += 1
end
end
local Tires = {}
for _, New in pairs(CollectionService:GetTagged("Tire")) do
table.insert(Tires,New)
end
for _, New in pairs(Tires) do
if New:FindFirstChild("WheelWeld") then
UpdateSpeed(New)
end
end
end