How could I make the picker still follow the mouse when not in the color wheel?

Hello, better scripters! So, I’m currently working on a color wheel system and I achieved to make the picker follow the mouse when the mouse is in the color wheel, but now I want the picker to still follow the mouse even when the mouse is not in the color wheel. So how could I do that.

Here’s what I want to achieve :

Here’s what I have right now :

So if anyone can help me, I am not really good at math.

Are you able to provide a script?

Yes, of course!

Here is my script :

local uis = game:GetService("UserInputService")
local run = game:GetService("RunService")

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local colorWheel = script.Parent:WaitForChild("ColourWheel")
local wheelPicker = colorWheel:WaitForChild("Picker")

local darknessPicker = script.Parent:WaitForChild("DarknessPicker")
local darknessSlider = darknessPicker:WaitForChild("Slider")

local colorDisplay = script.Parent:WaitForChild("ColourDisplay")

local buttonDown = false
local movingSlider = false

colorWheel.MouseButton1Down:Connect(function()
	buttonDown = true
end)

colorWheel.MouseButton1Up:Connect(function()
	buttonDown = false
	movingSlider = false
end)

darknessPicker.MouseButton1Down:Connect(function()
	movingSlider = true
end)

darknessPicker.MouseButton1Up:Connect(function()
	movingSlider = false
	buttonDown = false
end)

mouse.Button1Up:Connect(function()
	movingSlider = false
	buttonDown = false
end)

run.RenderStepped:Connect(function()
	local mousePos = uis:GetMouseLocation() - Vector2.new(0, game:GetService("GuiService"):GetGuiInset().Y)
	
	local centerOfWheel = Vector2.new(colorWheel.AbsolutePosition.X + (colorWheel.AbsoluteSize.X/2), colorWheel.AbsolutePosition.Y + (colorWheel.AbsoluteSize.Y/2))
	
	local distanceFromCenter = (mousePos - centerOfWheel).Magnitude
	
	if buttonDown then
		if distanceFromCenter <= colorWheel.AbsoluteSize.X/2 then
			wheelPicker.Position = UDim2.new(0, mousePos.X - colorWheel.AbsolutePosition.X, 0, mousePos.Y - colorWheel.AbsolutePosition.Y)
		else
			
		end
	end
end)
1 Like
function clampLength(v: Vector2, maxLength: number): Vector2
    if v.Magnitud <= maxLength then
        return v
    else
        return v.Unit * maxLength
    end
end

...

if buttonDown then
    local mousePosRelative = mousePos - colorWheel.AbsolutePosition
    local mousePosClamped = clampLength(mousePosRelative, colorWheel.AbsoluteSize.X/2)
    wheelPicker.Position = UDim2.fromOffset(mousePosClamped.X, mousePosClamped.Y)
end

Oh, I’m sorry I should of closed this post, but I forgot. Someone helped me solve it.

Here is my entire script for the ones that want to do the same :

local uis = game:GetService("UserInputService")
local run = game:GetService("RunService")
local gs = game:GetService("GuiService")

local mouseOffset = Vector2.yAxis * -gs:GetGuiInset().Y

local parent = script.Parent
local colorWheel = parent.ColorWheel
local colorPicker = parent.Picker

local draggingPicker = false

local function getColor()
	local h
	
	local s
	
	local v
end

local function restrictPointToRadius(radius: number, origin: Vector2, destination: Vector2): Vector2
	local offset = origin - destination
	
	return origin + offset.Unit * -math.min(radius, offset.Magnitude)
end

local function getCenterPoint(gui: GuiObject): Vector2
	local absolutePosition = gui.AbsolutePosition
	local absoluteSize = gui.AbsoluteSize / 2
	
	return Vector2.new(absolutePosition.X + absoluteSize.X, absolutePosition.Y + absoluteSize.Y)
end

local function updatePicker()
	draggingPicker = true
	
	while draggingPicker do
		local origin = getCenterPoint(colorWheel)

		local point = uis:GetMouseLocation() + mouseOffset
		
		local distanceFromCenter = (point - origin).Magnitude
		
		if distanceFromCenter <= colorWheel.AbsoluteSize.X / 2 then
			colorPicker.Position = UDim2.fromOffset(point.X, point.Y + 36)
		else
			local radius = colorWheel.AbsoluteSize.X / 2

			local restrictedPoint = restrictPointToRadius(radius, origin, point)

			colorPicker.Position = UDim2.fromOffset(restrictedPoint.X, restrictedPoint.Y + 36)
		end
		run.RenderStepped:Wait()
	end
end

local function onInputBegan(input: InputObject)
	if input.UserInputType ~= Enum.UserInputType.MouseButton1 then
		return
	end
	
	updatePicker()
end

local function onInputEnded(input: InputObject)
	if input.UserInputType ~= Enum.UserInputType.MouseButton1 then
		return
	end
	
	draggingPicker = false
end

colorWheel.InputBegan:Connect(onInputBegan)
colorWheel.InputEnded:Connect(onInputEnded)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.