function DynamicThumbstick.OnInputEnded(M)M.moveTouchObject=nil M.moveVector=ZERO_VECTOR3 M:FadeThumbstick(false)M.thumbstickFrame.Active=true end function DynamicThumbstick.FadeThumbstick(J,M)if not M and J.moveTouchObject then return end if J.isFirstTouch then return end if J.startImageFadeTween then J.startImageFadeTween:Cancel()end if J.endImageFadeTween then J.endImageFadeTween:Cancel()end for M=1,#J.middleImages,1 do if J.middleImageFadeTweens[M]then J.middleImageFadeTweens[M]:Cancel()end end if M then J.startImageFadeTween=TweenService:Create(J.startImage,ThumbstickFadeTweenInfo,{ImageTransparency=0})J.startImageFadeTween:Play()J.endImageFadeTween=TweenService:Create(J.endImage,ThumbstickFadeTweenInfo,{ImageTransparency=.2})J.endImageFadeTween:Play()for M=1,#J.middleImages,1 do J.middleImageFadeTweens[M]=TweenService:Create(J.middleImages[M],ThumbstickFadeTweenInfo,{ImageTransparency=MIDDLE_TRANSPARENCIES[M]})J.middleImageFadeTweens[M]:Play()end else J.startImageFadeTween=TweenService:Create(J.startImage,ThumbstickFadeTweenInfo,{ImageTransparency=1})J.startImageFadeTween:Play()J.endImageFadeTween=TweenService:Create(J.endImage,ThumbstickFadeTweenInfo,{ImageTransparency=1})J.endImageFadeTween:Play()for M=1,#J.middleImages,1 do J.middleImageFadeTweens[M]=TweenService:Create(J.middleImages[M],ThumbstickFadeTweenInfo,{ImageTransparency=1})J.middleImageFadeTweens[M]:Play()end end end function DynamicThumbstick.FadeThumbstickFrame(C,M,J)C.fadeInAndOutHalfDuration=M*.5 C.fadeInAndOutBalance=J C.tweenInAlphaStart=tick()end function DynamicThumbstick.Create(J,M)if J.thumbstickFrame then J.thumbstickFrame:Destroy()J.thumbstickFrame=nil if J.onTouchMovedConn then J.onTouchMovedConn:Disconnect()J.onTouchMovedConn=nil end if J.onTouchEndedConn then J.onTouchEndedCon:Disconnect()J.onTouchEndedCon=nil end if J.onRenderSteppedConn then J.onRenderSteppedConn:Disconnect()J.onRenderSteppedConn=nil end if J.onTouchActivateConn then J.onTouchActivateConn:Disconnect()J.onTouchActivateConn=nil end end local C=45 local G=20 local W=10 local V=W+4 local j=2 local N=20 local t=M.AbsoluteSize local v=math.min(t.x,t.y)>500 if v then C=C*2 G=G*2 W=W*2 V=V*2 j=j*2 N=N*2 end local function Z(M)if M then J.thumbstickFrame.Size=UDim2.new(1,0,.4,0)J.thumbstickFrame.Position=UDim2.new(0,0,.6,0)else J.thumbstickFrame.Size=UDim2.new(.4,0,.66666666666667,0)J.thumbstickFrame.Position=UDim2.new(0,0,.33333333333333,0)end end J.thumbstickFrame=Instance.new("\084\101\120\116\066\117\116\116\111\110")J.thumbstickFrame.Text=""J.thumbstickFrame.Name="\068\121\110\097\109\105\099\084\104\117\109\098\115\116\105\099\107\070\114\097\109\101"J.thumbstickFrame.Visible=false J.thumbstickFrame.BackgroundTransparency=1.0 J.thumbstickFrame.BackgroundColor3=Color3.fromRGB(0,0,0)Z(false)J.startImage=Instance.new("\073\109\097\103\101\076\097\098\101\108")J.startImage.Name="\084\104\117\109\098\115\116\105\099\107\083\116\097\114\116"J.startImage.Visible=true J.startImage.BackgroundTransparency=1 J.startImage.Image=TOUCH_CONTROLS_SHEET J.startImage.ImageRectOffset=Vector2.new(1,1)J.startImage.ImageRectSize=Vector2.new(144,144)J.startImage.ImageColor3=Color3.new(0,0,0)J.startImage.AnchorPoint=Vector2.new(.5,.5)J.startImage.Position=UDim2.new(0,G*3.3,1,-G*2.8)J.startImage.Size=UDim2.new(0,G*3.7,0,G*3.7)J.startImage.ZIndex=10 J.startImage.Parent=J.thumbstickFrame J.endImage=Instance.new("\073\109\097\103\101\076\097\098\101\108")J.endImage.Name="\084\104\117\109\098\115\116\105\099\107\069\110\100"J.endImage.Visible=true J.endImage.BackgroundTransparency=1 J.endImage.Image=TOUCH_CONTROLS_SHEET J.endImage.ImageRectOffset=Vector2.new(1,1)J.endImage.ImageRectSize=Vector2.new(144,144)J.endImage.AnchorPoint=Vector2.new(.5,.5)J.endImage.Position=J.startImage.Position J.endImage.Size=UDim2.new(0,C*.8,0,C*.8)J.endImage.ZIndex=10 J.endImage.Parent=J.thumbstickFrame for M=1,NUM_MIDDLE_IMAGES,1 do J.middleImages[M]=Instance.new("\073\109\097\103\101\076\097\098\101\108")J.middleImages[M].Name="\084\104\117\109\098\115\116\105\099\107\077\105\100\100\108\101"J.middleImages[M].Visible=false J.middleImages[M].BackgroundTransparency=1 J.middleImages[M].Image=TOUCH_CONTROLS_SHEET J.middleImages[M].ImageRectOffset=Vector2.new(1,1)J.middleImages[M].ImageRectSize=Vector2.new(144,144)J.middleImages[M].ImageTransparency=MIDDLE_TRANSPARENCIES[M]J.middleImages[M].AnchorPoint=Vector2.new(.5,.5)J.middleImages[M].ZIndex=9 J.middleImages[M].Parent=J.thumbstickFrame end local L=nil local function h()if L then L:Disconnect()L=nil end local M=workspace.CurrentCamera if M then local function J()local J=M.ViewportSize local C=J.X<J.Y Z(C)end L=(M:GetPropertyChangedSignal("\086\105\101\119\112\111\114\116\083\105\122\101")):Connect(J)J()end end;(workspace:GetPropertyChangedSignal("\067\117\114\114\101\110\116\067\097\109\101\114\097")):Connect(h)if workspace.CurrentCamera then h()end J.moveTouchStartPosition=nil J.startImageFadeTween=nil J.endImageFadeTween=nil J.middleImageFadeTweens={}local function y(M)local C=M local G=C.magnitude if G<j then C=Vector3.new()else C=C.unit*(1-math.max(0,(N-C.magnitude)/N))C=Vector3.new(C.x,0,C.y)end J.moveVector=C end local function b(M,j)local N=C/2+W local t=j-M local v=(t.magnitude-G/2)-W local Z=t.unit local L=V*NUM_MIDDLE_IMAGES local h=V if L<v then h=v/NUM_MIDDLE_IMAGES end for M=1,NUM_MIDDLE_IMAGES,1 do local C=J.middleImages[M]local G=N+h*(M-2)local V=N+h*(M-1)if G<v then local M=j-Z*V local J=math.clamp(1-(V-v)/h,0,1)C.Visible=true C.Position=UDim2.new(0,M.X,0,M.Y)C.Size=UDim2.new(0,W*J,0,W*J)else C.Visible=false end end end local function x(M)local C=Vector2.new(J.moveTouchStartPosition.X,J.moveTouchStartPosition.Y)-J.thumbstickFrame.AbsolutePosition local G=Vector2.new(M.X,M.Y)-J.thumbstickFrame.AbsolutePosition J.endImage.Position=UDim2.new(0,G.X,0,G.Y)b(C,G)end J.thumbstickFrame.InputBegan:Connect(function(M)if M.UserInputType~=Enum.UserInputType.Touch or M.UserInputState~=Enum.UserInputState.Begin then return end if J.moveTouchObject then return end if J.isFirstTouch then J.isFirstTouch=false local M=TweenInfo.new(.5,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0);(TweenService:Create(J.startImage,M,{Size=UDim2.new(0,0,0,0)})):Play();(TweenService:Create(J.endImage,M,{Size=UDim2.new(0,C,0,C),ImageColor3=Color3.new(0,0,0)})):Play()end J.moveTouchObject=M J.moveTouchStartPosition=M.Position local G=Vector2.new(M.Position.X-J.thumbstickFrame.AbsolutePosition.X,M.Position.Y-J.thumbstickFrame.AbsolutePosition.Y)J.startImage.Visible=true J.startImage.Position=UDim2.new(0,G.X,0,G.Y)J.endImage.Visible=true J.endImage.Position=J.startImage.Position J:FadeThumbstick(true)x(M.Position)if FADE_IN_OUT_BACKGROUND then local M=Players.LocalPlayer:FindFirstChildOfClass("\080\108\097\121\101\114\071\117\105")local C=false if M then if M.CurrentScreenOrientation==Enum.ScreenOrientation.LandscapeLeft or M.CurrentScreenOrientation==Enum.ScreenOrientation.LandscapeRight then C=J.hasFadedBackgroundInLandscape J.hasFadedBackgroundInLandscape=true elseif M.CurrentScreenOrientation==Enum.ScreenOrientation.Portrait then C=J.hasFadedBackgroundInPortrait J.hasFadedBackgroundInPortrait=true end end if not C then J.fadeInAndOutHalfDuration=FADE_IN_OUT_HALF_DURATION_DEFAULT J.fadeInAndOutBalance=FADE_IN_OUT_BALANCE_DEFAULT J.tweenInAlphaStart=tick()end end end)J.onTouchMovedConn=UserInputService.TouchMoved:connect(function(M)if M==J.moveTouchObject then J.thumbstickFrame.Active=false local C=Vector2.new(M.Position.x-J.moveTouchStartPosition.x,M.Position.y-J.moveTouchStartPosition.y)if math.abs(C.x)>0 or math.abs(C.y)>0 then y(C)x(M.Position)end end end)J.onRenderSteppedConn=RunService.RenderStepped:Connect(function()if J.tweenInAlphaStart~=nil then local M=tick()-J.tweenInAlphaStart local C=(J.fadeInAndOutHalfDuration*2)*J.fadeInAndOutBalance J.thumbstickFrame.BackgroundTransparency=1-FADE_IN_OUT_MAX_ALPHA*math.min(M/C,1)if M>C then J.tweenOutAlphaStart=tick()J.tweenInAlphaStart=nil end elseif J.tweenOutAlphaStart~=nil then local M=tick()-J.tweenOutAlphaStart local C=J.fadeInAndOutHalfDuration*2-(J.fadeInAndOutHalfDuration*2)*J.fadeInAndOutBalance J.thumbstickFrame.BackgroundTransparency=(1-FADE_IN_OUT_MAX_ALPHA)+FADE_IN_OUT_MAX_ALPHA*math.min(M/C,1)if M>C then J.tweenOutAlphaStart=nil end end end)J.onTouchEndedConn=UserInputService.TouchEnded:connect(function(M)if M==J.moveTouchObject then J:OnInputEnded()end end)GuiService.MenuOpened:connect(function()if J.moveTouchObject then J:OnInputEnded()end end)local R=Players.LocalPlayer:FindFirstChildOfClass("\080\108\097\121\101\114\071\117\105")while not R do Players.LocalPlayer.ChildAdded:wait()R=Players.LocalPlayer:FindFirstChildOfClass("\080\108\097\121\101\114\071\117\105")end local u=nil local P=R.CurrentScreenOrientation==Enum.ScreenOrientation.LandscapeLeft or R.CurrentScreenOrientation==Enum.ScreenOrientation.LandscapeRight local function m()J.fadeInAndOutHalfDuration=2.5 J.fadeInAndOutBalance=.05 J.tweenInAlphaStart=tick()end u=R.Changed:connect(function(M)if M=="\067\117\114\114\101\110\116\083\099\114\101\101\110\079\114\105\101\110\116\097\116\105\111\110"then if P and R.CurrentScreenOrientation==Enum.ScreenOrientation.Portrait or not P and R.CurrentScreenOrientation~=Enum.ScreenOrientation.Portrait then u:disconnect()m()if P then J.hasFadedBackgroundInPortrait=true else J.hasFadedBackgroundInLandscape=true end end end end)J.thumbstickFrame.Parent=M spawn(function()if game:IsLoaded()then m()else game.Loaded:wait()m()end end)end return DynamicThumbstick

function DynamicThumbstick:OnInputEnded()
	self.moveTouchObject = nil
	self.moveVector = ZERO_VECTOR3
	self:FadeThumbstick(false)
	self.thumbstickFrame.Active = true
end

function DynamicThumbstick:FadeThumbstick(visible)
	if not visible and self.moveTouchObject then
		return
	end
	if self.isFirstTouch then return end

	if self.startImageFadeTween then
		self.startImageFadeTween:Cancel()
	end
	if self.endImageFadeTween then
		self.endImageFadeTween:Cancel()
	end
	for i = 1, #self.middleImages do
		if self. middleImageFadeTweens[i] then
			self.middleImageFadeTweens[i]:Cancel()
		end
	end

	if visible then
		self.startImageFadeTween = TweenService:Create(self.startImage, ThumbstickFadeTweenInfo, { ImageTransparency = 0 })
		self.startImageFadeTween:Play()

		self.endImageFadeTween = TweenService:Create(self.endImage, ThumbstickFadeTweenInfo, { ImageTransparency = 0.2 })
		self.endImageFadeTween:Play()

		for i = 1, #self.middleImages do
			self.middleImageFadeTweens[i] = TweenService:Create(self.middleImages[i], ThumbstickFadeTweenInfo, { ImageTransparency = MIDDLE_TRANSPARENCIES[i] })
			self.middleImageFadeTweens[i]:Play()
		end
	else
		self.startImageFadeTween = TweenService:Create(self.startImage, ThumbstickFadeTweenInfo, { ImageTransparency = 1 })
		self.startImageFadeTween:Play()

		self.endImageFadeTween = TweenService:Create(self.endImage, ThumbstickFadeTweenInfo, { ImageTransparency = 1 })
		self.endImageFadeTween:Play()

		for i = 1, #self.middleImages do
			self.middleImageFadeTweens[i] = TweenService:Create(self.middleImages[i], ThumbstickFadeTweenInfo, { ImageTransparency = 1 })
			self.middleImageFadeTweens[i]:Play()
		end
	end
end

function DynamicThumbstick:FadeThumbstickFrame(fadeDuration, fadeRatio)
	self.fadeInAndOutHalfDuration = fadeDuration * 0.5
	self.fadeInAndOutBalance = fadeRatio
	self.tweenInAlphaStart = tick()
end

function DynamicThumbstick:Create(parentFrame)
	if self.thumbstickFrame then
		self.thumbstickFrame:Destroy()
		self.thumbstickFrame = nil
		if self.onTouchMovedConn then
			self.onTouchMovedConn:Disconnect()
			self.onTouchMovedConn = nil
		end
		if self.onTouchEndedConn then
			self.onTouchEndedCon:Disconnect()
			self.onTouchEndedCon = nil
		end
		if self.onRenderSteppedConn then
			self.onRenderSteppedConn:Disconnect()
			self.onRenderSteppedConn = nil
		end
		if self.onTouchActivateConn then
			self.onTouchActivateConn:Disconnect()
			self.onTouchActivateConn = nil
		end
	end

	local ThumbstickSize = 45
	local ThumbstickRingSize = 20
	local MiddleSize = 10
	local MiddleSpacing = MiddleSize + 4
	local RadiusOfDeadZone = 2
	local RadiusOfMaxSpeed = 20

	local screenSize = parentFrame.AbsoluteSize
	local isBigScreen = math.min(screenSize.x, screenSize.y) > 500
	if isBigScreen then
		ThumbstickSize = ThumbstickSize * 2
		ThumbstickRingSize = ThumbstickRingSize * 2
		MiddleSize = MiddleSize * 2
		MiddleSpacing = MiddleSpacing * 2
		RadiusOfDeadZone = RadiusOfDeadZone * 2
		RadiusOfMaxSpeed = RadiusOfMaxSpeed * 2
	end

	local function layoutThumbstickFrame(portraitMode)
		if portraitMode then
			self.thumbstickFrame.Size = UDim2.new(1, 0, 0.4, 0)
			self.thumbstickFrame.Position = UDim2.new(0, 0, 0.6, 0)
		else
			self.thumbstickFrame.Size = UDim2.new(0.4, 0, 2/3, 0)
			self.thumbstickFrame.Position = UDim2.new(0, 0, 1/3, 0)
		end
	end

	self.thumbstickFrame = Instance.new("TextButton")
	self.thumbstickFrame.Text = ""
	self.thumbstickFrame.Name = "DynamicThumbstickFrame"
	self.thumbstickFrame.Visible = false
	self.thumbstickFrame.BackgroundTransparency = 1.0
	self.thumbstickFrame.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
	layoutThumbstickFrame(false)

	self.startImage = Instance.new("ImageLabel")
	self.startImage.Name = "ThumbstickStart"
	self.startImage.Visible = true
	self.startImage.BackgroundTransparency = 1
	self.startImage.Image = TOUCH_CONTROLS_SHEET
	self.startImage.ImageRectOffset = Vector2.new(1,1)
	self.startImage.ImageRectSize = Vector2.new(144, 144)
	self.startImage.ImageColor3 = Color3.new(0, 0, 0)
	self.startImage.AnchorPoint = Vector2.new(0.5, 0.5)
	self.startImage.Position = UDim2.new(0, ThumbstickRingSize * 3.3, 1, -ThumbstickRingSize * 2.8)
	self.startImage.Size = UDim2.new(0, ThumbstickRingSize * 3.7, 0, ThumbstickRingSize * 3.7)
	self.startImage.ZIndex = 10
	self.startImage.Parent = self.thumbstickFrame

	self.endImage = Instance.new("ImageLabel")
	self.endImage.Name = "ThumbstickEnd"
	self.endImage.Visible = true
	self.endImage.BackgroundTransparency = 1
	self.endImage.Image = TOUCH_CONTROLS_SHEET
	self.endImage.ImageRectOffset = Vector2.new(1,1)
	self.endImage.ImageRectSize =  Vector2.new(144, 144)
	self.endImage.AnchorPoint = Vector2.new(0.5, 0.5)
	self.endImage.Position = self.startImage.Position
	self.endImage.Size = UDim2.new(0, ThumbstickSize * 0.8, 0, ThumbstickSize * 0.8)
	self.endImage.ZIndex = 10
	self.endImage.Parent = self.thumbstickFrame

	for i = 1, NUM_MIDDLE_IMAGES do
		self.middleImages[i] = Instance.new("ImageLabel")
		self.middleImages[i].Name = "ThumbstickMiddle"
		self.middleImages[i].Visible = false
		self.middleImages[i].BackgroundTransparency = 1
		self.middleImages[i].Image = TOUCH_CONTROLS_SHEET
		self.middleImages[i].ImageRectOffset = Vector2.new(1,1)
		self.middleImages[i].ImageRectSize = Vector2.new(144, 144)
		self.middleImages[i].ImageTransparency = MIDDLE_TRANSPARENCIES[i]
		self.middleImages[i].AnchorPoint = Vector2.new(0.5, 0.5)
		self.middleImages[i].ZIndex = 9
		self.middleImages[i].Parent = self.thumbstickFrame
	end

	local CameraChangedConn = nil
	local function onCurrentCameraChanged()
		if CameraChangedConn then
			CameraChangedConn:Disconnect()
			CameraChangedConn = nil
		end
		local newCamera = workspace.CurrentCamera
		if newCamera then
			local function onViewportSizeChanged()
				local size = newCamera.ViewportSize
				local portraitMode = size.X < size.Y
				layoutThumbstickFrame(portraitMode)
			end
			CameraChangedConn = newCamera:GetPropertyChangedSignal("ViewportSize"):Connect(onViewportSizeChanged)
			onViewportSizeChanged()
		end
	end
	workspace:GetPropertyChangedSignal("CurrentCamera"):Connect(onCurrentCameraChanged)
	if workspace.CurrentCamera then
		onCurrentCameraChanged()
	end

	self.moveTouchStartPosition = nil

	self.startImageFadeTween = nil
	self.endImageFadeTween = nil
	self.middleImageFadeTweens = {}

	local function doMove(direction)
		local currentMoveVector = direction

		-- Scaled Radial Dead Zone
		local inputAxisMagnitude = currentMoveVector.magnitude
		if inputAxisMagnitude < RadiusOfDeadZone then
			currentMoveVector = Vector3.new()
		else
			currentMoveVector = currentMoveVector.unit*(1 - math.max(0, (RadiusOfMaxSpeed - currentMoveVector.magnitude)/RadiusOfMaxSpeed))
			currentMoveVector = Vector3.new(currentMoveVector.x, 0, currentMoveVector.y)
		end

		self.moveVector = currentMoveVector
	end

	local function layoutMiddleImages(startPos, endPos)
		local startDist = (ThumbstickSize / 2) + MiddleSize
		local vector = endPos - startPos
		local distAvailable = vector.magnitude - (ThumbstickRingSize / 2) - MiddleSize
		local direction = vector.unit

		local distNeeded = MiddleSpacing * NUM_MIDDLE_IMAGES
		local spacing = MiddleSpacing

		if distNeeded < distAvailable then
			spacing = distAvailable / NUM_MIDDLE_IMAGES
		end

		for i = 1, NUM_MIDDLE_IMAGES do
			local image = self.middleImages[i]
			local distWithout = startDist + (spacing * (i - 2))
			local currentDist = startDist + (spacing * (i - 1))

			if distWithout < distAvailable then
				local pos = endPos - direction * currentDist
				local exposedFraction = math.clamp(1 - ((currentDist - distAvailable) / spacing), 0, 1)

				image.Visible = true
				image.Position = UDim2.new(0, pos.X, 0, pos.Y)
				image.Size = UDim2.new(0, MiddleSize * exposedFraction, 0, MiddleSize * exposedFraction)
			else
				image.Visible = false
			end
		end
	end

	local function moveStick(pos)
		local startPos = Vector2.new(self.moveTouchStartPosition.X, self.moveTouchStartPosition.Y) - self.thumbstickFrame.AbsolutePosition
		local endPos = Vector2.new(pos.X, pos.Y) - self.thumbstickFrame.AbsolutePosition
		self.endImage.Position = UDim2.new(0, endPos.X, 0, endPos.Y)
		layoutMiddleImages(startPos, endPos)
	end

	-- input connections
	self.thumbstickFrame.InputBegan:Connect(function(inputObject)
		if inputObject.UserInputType ~= Enum.UserInputType.Touch or inputObject.UserInputState ~= Enum.UserInputState.Begin then
			return
		end
		if self.moveTouchObject then
			return
		end

		if self.isFirstTouch then
			self.isFirstTouch = false
			local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out,0,false,0)
			TweenService:Create(self.startImage, tweenInfo, {Size = UDim2.new(0, 0, 0, 0)}):Play()
			TweenService:Create(self.endImage, tweenInfo, {Size = UDim2.new(0, ThumbstickSize, 0, ThumbstickSize), ImageColor3 = Color3.new(0,0,0)}):Play()
		end

		self.moveTouchObject = inputObject
		self.moveTouchStartPosition = inputObject.Position
		local startPosVec2 = Vector2.new(inputObject.Position.X - self.thumbstickFrame.AbsolutePosition.X, inputObject.Position.Y - self.thumbstickFrame.AbsolutePosition.Y)

		self.startImage.Visible = true
		self.startImage.Position = UDim2.new(0, startPosVec2.X, 0, startPosVec2.Y)
		self.endImage.Visible = true
		self.endImage.Position = self.startImage.Position

		self:FadeThumbstick(true)
		moveStick(inputObject.Position)

		if FADE_IN_OUT_BACKGROUND then
			local playerGui = Players.LocalPlayer:FindFirstChildOfClass("PlayerGui")
			local hasFadedBackgroundInOrientation = false

			-- only fade in/out the background once per orientation
			if playerGui then
				if playerGui.CurrentScreenOrientation == Enum.ScreenOrientation.LandscapeLeft or
					playerGui.CurrentScreenOrientation == Enum.ScreenOrientation.LandscapeRight then
						hasFadedBackgroundInOrientation = self.hasFadedBackgroundInLandscape
						self.hasFadedBackgroundInLandscape = true
				elseif playerGui.CurrentScreenOrientation == Enum.ScreenOrientation.Portrait then
						hasFadedBackgroundInOrientation = self.hasFadedBackgroundInPortrait
						self.hasFadedBackgroundInPortrait = true
				end
			end

			if not hasFadedBackgroundInOrientation then
				self.fadeInAndOutHalfDuration = FADE_IN_OUT_HALF_DURATION_DEFAULT
				self.fadeInAndOutBalance = FADE_IN_OUT_BALANCE_DEFAULT
				self.tweenInAlphaStart = tick()
			end
		end
	end)

	self.onTouchMovedConn = UserInputService.TouchMoved:connect(function(inputObject)
		if inputObject == self.moveTouchObject then
			self.thumbstickFrame.Active = false
			local direction = Vector2.new(inputObject.Position.x - self.moveTouchStartPosition.x, inputObject.Position.y - self.moveTouchStartPosition.y)
			if math.abs(direction.x) > 0 or math.abs(direction.y) > 0 then
				doMove(direction)
				moveStick(inputObject.Position)
			end
		end
	end)

	self.onRenderSteppedConn = RunService.RenderStepped:Connect(function()
		if self.tweenInAlphaStart ~= nil then
			local delta = tick() - self.tweenInAlphaStart
			local fadeInTime = (self.fadeInAndOutHalfDuration * 2 * self.fadeInAndOutBalance)
			self.thumbstickFrame.BackgroundTransparency = 1 - FADE_IN_OUT_MAX_ALPHA*math.min(delta/fadeInTime, 1)
			if delta > fadeInTime then
				self.tweenOutAlphaStart = tick()
				self.tweenInAlphaStart = nil
			end
		elseif self.tweenOutAlphaStart ~= nil then
			local delta = tick() - self.tweenOutAlphaStart
			local fadeOutTime = (self.fadeInAndOutHalfDuration * 2) - (self.fadeInAndOutHalfDuration * 2 * self.fadeInAndOutBalance)
			self.thumbstickFrame.BackgroundTransparency = 1 - FADE_IN_OUT_MAX_ALPHA + FADE_IN_OUT_MAX_ALPHA*math.min(delta/fadeOutTime, 1)
			if delta > fadeOutTime  then
				self.tweenOutAlphaStart = nil
			end
		end
	end)

	self.onTouchEndedConn = UserInputService.TouchEnded:connect(function(inputObject)
		if inputObject == self.moveTouchObject then
			self:OnInputEnded()
		end
	end)

	GuiService.MenuOpened:connect(function()
		if self.moveTouchObject then
			self:OnInputEnded()
		end
	end)

	local playerGui = Players.LocalPlayer:FindFirstChildOfClass("PlayerGui")
	while not playerGui do
		Players.LocalPlayer.ChildAdded:wait()
		playerGui = Players.LocalPlayer:FindFirstChildOfClass("PlayerGui")
	end

	local playerGuiChangedConn = nil
	local originalScreenOrientationWasLandscape =	playerGui.CurrentScreenOrientation == Enum.ScreenOrientation.LandscapeLeft or
													playerGui.CurrentScreenOrientation == Enum.ScreenOrientation.LandscapeRight

	local function longShowBackground()
		self.fadeInAndOutHalfDuration = 2.5
		self.fadeInAndOutBalance = 0.05
		self.tweenInAlphaStart = tick()
	end

	playerGuiChangedConn = playerGui.Changed:connect(function(prop)
		if prop == "CurrentScreenOrientation" then
			if (originalScreenOrientationWasLandscape and playerGui.CurrentScreenOrientation == Enum.ScreenOrientation.Portrait) or
				(not originalScreenOrientationWasLandscape and playerGui.CurrentScreenOrientation ~= Enum.ScreenOrientation.Portrait) then

				playerGuiChangedConn:disconnect()
				longShowBackground()

				if originalScreenOrientationWasLandscape then
					self.hasFadedBackgroundInPortrait = true
				else
					self.hasFadedBackgroundInLandscape = true
				end
			end
		end
	end)

	self.thumbstickFrame.Parent = parentFrame

	spawn(function()
		if game:IsLoaded() then
			longShowBackground()
		else
			game.Loaded:wait()
			longShowBackground()
		end
	end)
end

return DynamicThumbstick
