--Weight Setup--


uv = script.Parent.CFrame.upVector

local distribution = WeightDistribution --0-100%, how much of the weight is in the rear, 50 = 50%/50%

local DistA = (distribution/25)
local DistB = ((100-distribution)/25)

local Dist1 = math.max(0,DistB-1)
local Dist2 = math.min(DistA,1)

local DistF = (FL.Position+FR.Position)*Dist1
local DistR = (RL.Position+RR.Position)*Dist2
local DistD = (Dist1+Dist2)*2

local weight = Instance.new("Part")
weight.Parent = script.Parent.Parent
weight.CanCollide = false
weight.Position = script.Parent.Position
weight.CFrame = CFrame.new((DistF+DistR)/DistD)
weight.Position = weight.Position + Vector3.new(0.9,0.9,0.9)*script.Parent.CFrame.upVector
weight.Size = Vector3.new(3, 2, 4)
weight.Rotation = script.Parent.Rotation
weight.Name = "Weight"
weight.Anchored = true

local engine = Instance.new("Part") engine.Anchored = true
engine.Parent = script.Parent.Parent
engine.CanCollide = false
engine.Position = script.Parent.Position
engine.Position = engine.Position + Vector3.new(2,2,2)*script.Parent.CFrame.upVector
engine.Size = (Vector3.new(1, 0.7, 0.7)*(EngineDisplacement/4000))+Vector3.new(2, 1.4, 1.4)
engine.Rotation = script.Parent.Rotation
engine.Name = "Engine"


local fuel = Instance.new("Part") fuel.Anchored = true
fuel.Parent = script.Parent.Parent
fuel.CanCollide = false
fuel.Position = script.Parent.Position
fuel.Position = fuel.Position + Vector3.new(2.5,2.5,2.5)*script.Parent.CFrame.upVector
fuel.Size = Vector3.new(2.5, 2.5, 7)
fuel.Rotation = script.Parent.Rotation
fuel.Name = "FuelTank"


local trans = Instance.new("Part") trans.Anchored = true
trans.Parent = script.Parent.Parent
trans.CanCollide = false
trans.Position = script.Parent.Position
trans.Position = trans.Position + Vector3.new(3,3,3)*script.Parent.CFrame.upVector
trans.Size = Vector3.new(2.7, 2, 2)*(AmountOfGears/7)
trans.Rotation = script.Parent.Rotation
trans.Name = "Transmission"


local diff = Instance.new("Part") diff.Anchored = true
diff.Parent = script.Parent.Parent
diff.CanCollide = false
diff.Position = script.Parent.Position
diff.Position = diff.Position + Vector3.new(3.5,3.5,3.5)*script.Parent.CFrame.upVector
diff.Size = Vector3.new(2, 2, 2)
diff.Rotation = script.Parent.Rotation
diff.Name = "Differential"


fuel.CFrame = CFrame.new(((RL.Position)+(RR.Position))/2)
fuel.CFrame = fuel.CFrame + Vector3.new(3,3,3)*lv

if Drivetrain == "RWD" then
	diff.CFrame = CFrame.new(((RL.Position)+(RR.Position))/2)

elseif Drivetrain == "FWD" then
	diff.CFrame = CFrame.new(((FL.Position)+(FR.Position))/2)
	engine.Rotation = engine.Rotation + Vector3.new(0,90,0)

elseif Drivetrain == "AWD" then
	diff.CFrame = CFrame.new(((RL.Position)+(RR.Position))/2)
	
		if TorqueSplit < 51 then
		engine.Rotation = engine.Rotation + Vector3.new(0,90,0)
		end
end

if EngineLocation == "Front" then
	engine.CFrame = CFrame.new(((FL.Position)+(FR.Position))/2)
	engine.CFrame = engine.CFrame + Vector3.new(2,2,2)*lv
	engine.CFrame = engine.CFrame + Vector3.new(1,1,1)*uv
	trans.CFrame = engine.CFrame + Vector3.new(-1.5,-1.5,-1.5)*lv
	
elseif EngineLocation == "Mid" then
	engine.CFrame = CFrame.new(((RL.Position)+(RR.Position))/2)
	engine.CFrame = engine.CFrame + Vector3.new(2,2,2)*lv	
	engine.CFrame = engine.CFrame + Vector3.new(1,1,1)*uv
	trans.CFrame = engine.CFrame + Vector3.new(1.5,1.5,1.5)*lv
	
elseif EngineLocation == "Rear" then
	engine.CFrame = CFrame.new(((RL.Position)+(RR.Position))/2)
	engine.CFrame = engine.CFrame + Vector3.new(-2,-2,-2)*lv
	engine.CFrame = engine.CFrame + Vector3.new(1,1,1)*uv
	trans.CFrame = engine.CFrame + Vector3.new(-1.5,-1.5,-1.5)*lv
	
end

rv = script.Parent.CFrame.rightVector
