local A=game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")local n=game.Workspace.CurrentCamera local M=game.Players.LocalPlayer local N=M.Character local y=N.HumanoidRootPart local S=(script:WaitForChild("\067\097\114")).Value local Q=S:WaitForChild("\067\111\110\102\105\103\117\114\097\116\105\111\110\115")local p=require(S.CarScript.RaycastModule)local z=Enum.CameraType.Follow local k=Vector2.new()local I=S:WaitForChild("\068\114\105\118\101\083\101\097\116")I.Changed:Connect(function(A)if A=="\084\104\114\111\116\116\108\101"then k=Vector2.new(k.X,I.Throttle)end if A=="\083\116\101\101\114"then k=Vector2.new(I.Steer,k.Y)end end)local w=0 local j=0 local q=0 for A,n in pairs(S:GetChildren())do if n:IsA("\066\097\115\101\080\097\114\116")then q=q+n:GetMass()*196.2 end end w=q*Q.Suspension.Value j=w/Q.Bounce.Value local d=Instance.new("\066\111\100\121\086\101\108\111\099\105\116\121",S.Chassis)d.velocity=Vector3.new(0,0,0)d.maxForce=Vector3.new(0,0,0)local B=Instance.new("\066\111\100\121\065\110\103\117\108\097\114\086\101\108\111\099\105\116\121",S.Chassis)B.angularvelocity=Vector3.new(0,0,0)B.maxTorque=Vector3.new(0,0,0)local H=0 local function D(A)local n=A:FindFirstChild("\066\111\100\121\084\104\114\117\115\116")if not n then n=Instance.new("\066\111\100\121\084\104\114\117\115\116",A)end local M,N=p.new(A.Position,A.CFrame:vectorToWorldSpace(Vector3.new(0,-1,0))*Q.Height.Value)local y=(N-A.Position).magnitude if M and M.CanCollide then n.force=Vector3.new(0,(Q.Height.Value-y)^2*(w/Q.Height.Value^2),0)local M=(A.CFrame:toObjectSpace(CFrame.new(A.Velocity+A.Position))).p*j n.force=n.force-Vector3.new(0,M.Y,0)else n.force=Vector3.new(0,0,0)end local z=A:FindFirstChild("\087\104\101\101\108\087\101\108\100")if z then z.C0=CFrame.new(0,-math.min(y,Q.Height.Value*.8)+z.Part1.Size.Y/2,0)local n=S.Chassis.CFrame:inverse()*A.CFrame local M=S.Chassis.CFrame:vectorToObjectSpace(S.Chassis.Velocity)if n.Z<0 then local A=1 if M.Z>0 then A=-1 end z.C0=z.C0*CFrame.Angles(0,(S.Chassis.RotVelocity.Y/2)*A,0)end z.C0=z.C0*CFrame.Angles(H,0,0)end end

local userInputService = game:GetService("UserInputService")
local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local character = player.Character
local humanoidRootPart = character.HumanoidRootPart
local car = script:WaitForChild("Car").Value
local stats = car:WaitForChild("Configurations")
local Raycast = require(car.CarScript.RaycastModule)

local cameraType = Enum.CameraType.Follow

local movement = Vector2.new()

local seat = car:WaitForChild("DriveSeat")

seat.Changed:Connect(function(property)
	if property == "Throttle" then
		movement = Vector2.new(movement.X, seat.Throttle)
	end
	if property == "Steer" then
		movement = Vector2.new(seat.Steer, movement.Y)
	end
end)

local force = 0
local damping = 0

local mass = 0

for i, v in pairs(car:GetChildren()) do
	if v:IsA("BasePart") then
		mass = mass + (v:GetMass() * 196.2)
	end
end

force = mass * stats.Suspension.Value
damping = force / stats.Bounce.Value

local bodyVelocity = Instance.new("BodyVelocity", car.Chassis)
bodyVelocity.velocity = Vector3.new(0, 0, 0)
bodyVelocity.maxForce = Vector3.new(0, 0, 0)

local bodyAngularVelocity = Instance.new("BodyAngularVelocity", car.Chassis)
bodyAngularVelocity.angularvelocity = Vector3.new(0, 0, 0)
bodyAngularVelocity.maxTorque = Vector3.new(0, 0, 0)

local rotation = 0

local function UpdateThruster(thruster)
	--Make sure we have a bodythrust to move the wheel
	local bodyThrust = thruster:FindFirstChild("BodyThrust")
	if not bodyThrust then
		bodyThrust = Instance.new("BodyThrust", thruster)
	end
	--Do some raycasting to get the height of the wheel
	local hit, position = Raycast.new(thruster.Position, thruster.CFrame:vectorToWorldSpace(Vector3.new(0, -1, 0)) * stats.Height.Value)
	local thrusterHeight = (position - thruster.Position).magnitude
	if hit and hit.CanCollide then
		--If we're on the ground, apply some forces to push the wheel up
		bodyThrust.force = Vector3.new(0, ((stats.Height.Value - thrusterHeight)^2) * (force / stats.Height.Value^2), 0)
		local thrusterDamping = thruster.CFrame:toObjectSpace(CFrame.new(thruster.Velocity + thruster.Position)).p * damping
		bodyThrust.force = bodyThrust.force - Vector3.new(0, thrusterDamping.Y, 0)
	else
		bodyThrust.force = Vector3.new(0, 0, 0)
	end
	
	--Wheels
	local wheelWeld = thruster:FindFirstChild("WheelWeld")
	if wheelWeld then
		wheelWeld.C0 = CFrame.new(0, -math.min(thrusterHeight, stats.Height.Value * 0.8) + (wheelWeld.Part1.Size.Y / 2), 0)
		-- Wheel turning
		local offset = car.Chassis.CFrame:inverse() * thruster.CFrame
		local speed = car.Chassis.CFrame:vectorToObjectSpace(car.Chassis.Velocity)
		if offset.Z < 0 then
			local direction = 1
			if speed.Z > 0 then
				direction = -1
			end
			wheelWeld.C0 = wheelWeld.C0 * CFrame.Angles(0, (car.Chassis.RotVelocity.Y / 2) * direction, 0)
		end
		wheelWeld.C0 = wheelWeld.C0 * CFrame.Angles(rotation, 0, 0)
	end
end
