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.
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)