--[[Steering]]



	function Steering(dt)
		local deltaTime = (60/(1/dt))
		--Mouse Steer
		if _MSteer then
			local msWidth = math.max(1,mouse.ViewSizeX*_Tune.Peripherals.MSteerWidth/200)
			local mdZone = _Tune.Peripherals.MSteerDZone/100
			local mST = ((mouse.X-mouse.ViewSizeX/2)/msWidth)
			if math.abs(mST)<=mdZone then
				_GSteerT = 0
			else
				_GSteerT = (math.max(math.min((math.abs(mST)-mdZone),(1-mdZone)),0)/(1-mdZone))^_Tune.MSteerExp * (mST / math.abs(mST))
			end
		end
		
		_GSteerC = (_GSteerC+((_GSteerT-_GSteerC)*(_Tune.LeanSpeed*deltaTime)))
		
		--Apply Steering
		bike.Body.bal.LeanGyro.cframe = CFrame.new(bike.Body.bal.CFrame.p,bike.Body.bal.CFrame.p+bike.Body.bal.CFrame.lookVector)*CFrame.Angles(0,0,((_GSteerC*_Tune.LeanProgressiveness)*(math.rad(_Tune.MaxLean)/(bike.DriveSeat.Velocity.Magnitude+_Tune.LeanProgressiveness))-(_GSteerC*math.rad(_Tune.MaxLean))))
		
		--Apply Low Speed Steering
		bike.FrontSection.TripleTreeHinge.SteeringGyro.CFrame=bike.Body.SteeringHinge.CFrame*CFrame.Angles(math.rad(_GSteerT*_Tune.SteerAngle),0,0)
		bike.FrontSection.TripleTreeHinge.SteeringGyro.MaxTorque = Vector3.new(_Tune.SteerMaxTorque*((math.max((_Tune.LowSpeedCut-bike.DriveSeat.Velocity.Magnitude)/_Tune.LowSpeedCut,0))^2),0,0)
	end

if UserInputService.TouchEnabled then
	local Buttons = script.Parent:WaitForChild("Buttons")
	local Left = Buttons:WaitForChild("Left")
	local Right = Buttons:WaitForChild("Right")
	local Brake = Buttons:WaitForChild("Brake")
	local Gas = Buttons:WaitForChild("Gas")
	local Downshift = Buttons.Downshift
	local Upshift = Buttons.Upshift
	local Handbrake = Buttons:WaitForChild("Handbrake")

	local Thread = coroutine.wrap(function()
		while true do
			wait(0.5)
			if _TMode == "Manual" or _TMode == "DCT" then
				Downshift.Visible = true
				Upshift.Visible = true
			else
				Downshift.Visible = false
				Upshift.Visible = false
			end
		end
	end)
	Thread()
	Buttons.Visible = true

	local function LeftTurn(Touch, GPE)
		if Touch.UserInputState == Enum.UserInputState.Begin then
			_GSteerT = -1
			_SteerL = true
		else
			if _SteerR then
				_GSteerT = 1
			else
				_GSteerT = 0
			end
			_SteerL = false
		end
	end
	Left.InputBegan:Connect(LeftTurn)
	Left.InputEnded:Connect(LeftTurn)
	--Left.InputChanged:Connect(LeftTurn)

	local function RightTurn(Touch, GPE)
		if Touch.UserInputState == Enum.UserInputState.Begin then
			_GSteerT = 1
			_SteerR = true
		else
			if _SteerL then
				_GSteerT = -1
			else
				_GSteerT = 0
			end
			_SteerR = false
		end
	end
	Right.InputBegan:Connect(RightTurn)
	Right.InputEnded:Connect(RightTurn)
	--Right.InputChanged:Connect(RightTurn)

	local function TouchThrottle(input, GPE)
		if input.UserInputState == Enum.UserInputState.Begin then
			_InThrot = 1
			_TTick = tick()
		else
			_InThrot = _Tune.IdleThrottle/100
			_BTick = tick()
		end
	end
	Gas.InputBegan:Connect(TouchThrottle)
	Gas.InputEnded:Connect(TouchThrottle)
	--Gas.InputChanged:Connect(TouchThrottle)

	local function TouchBrake(input, GPE)
		if input.UserInputState == Enum.UserInputState.Begin then
			_InBrake = 1
		else
			_InBrake = 0
		end
	end
	Brake.InputBegan:Connect(TouchBrake)
	Brake.InputEnded:Connect(TouchBrake)
	--Brake.InputChanged:Connect(TouchBrake)

	local function TouchHandbrake(input, GPE)
		if input.UserInputState == Enum.UserInputState.Begin then
			_PBrake = not _PBrake
		elseif input.UserInputState == Enum.UserInputState.End then
			if bike.DriveSeat.Velocity.Magnitude > 5 then
				_PBrake = false
			end
		end
	end
	Handbrake.InputBegan:Connect(TouchHandbrake)
	Handbrake.InputEnded:Connect(TouchHandbrake)


	local function TouchDownshift(input, GPE)
		if ((_IsOn and ((_TMode == "Auto" and _CGear <= 1) and _Tune.AutoShiftVers == "New") or _TMode == "DCT") or _TMode == "Manual") and input.UserInputState == Enum.UserInputState.Begin then
			if not _ShiftDn then
				_ShiftDn = true
			end
		end
	end

	Downshift.InputBegan:Connect(TouchDownshift)

	local function TouchUpshift(input, GPE)
		if ((_IsOn and ((_TMode == "Auto" and _CGear < 1) and _Tune.AutoShiftVers == "New") or _TMode == "DCT") or _TMode == "Manual") and input.UserInputState == Enum.UserInputState.Begin then
			if not _ShiftUp then
				_ShiftUp = true
			end
		end
	end
	Upshift.InputBegan:Connect(TouchUpshift)

end