How do you create drag logic (Dragable UI with Math.Clamp)?

Hello there, I’m currently making a mobile thumbstick for my bullet hell game and I’m confused on how I would create the dragging system of the stick. I would like to drag the stick UI when pressed, even if it leaves the frame, the stick UI should still try to get to where the mouse is, but it will be stopped at the edge of the frame due to Math.Clamp(). Only when the finger or mouth is lifted, the stick UI will snap back to the origin.
Some Noteable features before I request help:

  • Position is scaled and not based off on offset
  • Stick UI is parented to the frame

Code:

BackgroundFrame.InputBegan:Connect(function(inputObj: InputObject, gp)
	if not usingThumbstick and inputObj.UserInputType == Enum.UserInputType.Touch then
		usingThumbstick = true
		currentTouch = inputObj
		
		print(currentTouch.Name or currentTouch)
		
		ThumbstickGuiObject.Position = UDim2.fromScale(0,0) -- (0,0) is placeholder

	end
end)

UserInputService.InputEnded:Connect(function(inputObj: InputObject, gp)
	if usingThumbstick and inputObj == currentTouch then
		usingThumbstick = false
		currentTouch = nil
		
		print("Ended")
		
		ThumbstickGuiObject.Position = UDim2.fromScale(0.5,0.5)
		
	end
end)