Could you give us the code? I think you’ll have to manually set some boundaries for the circle. And If it is a ImageLabel, try making it a Frame with a UiCorner.
In a circle, all points on its line are at the same distance as the circle so just clamp it so that the position does not go further than the distance between the circle end and centre.
Basically, add x and y and see if it goes further and if it does reduce both.
Hope this helps.
local function UDim2ToVector2(UDim2)
return Vector2.new(UDim2.X.Offset, UDim2.Y.Offset)
end
local function OnUpdate()
local delta = uis:GetMouseDelta()
local deltay = math.clamp(delta.Y,-10,10)
local deltax = math.clamp(delta.X,-10,10)
local X = circle.Position.X.Offset + deltax
local Y = circle.Position.Y.Offset + deltay
local nextPos = UDim2.fromOffset(X,Y)
local nextVector = UDim2ToVector2(nextPos)
local mag = (centerVector - nextVector).Magnitude
local unit = (centerVector - nextVector).Unit
print(mag)
circle.Position = UDim2.new(0.5, unit * mag, 0.5, unit * mag)
end
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local BlackCircle = script.Parent:WaitForChild("BlackCircle")
local WhiteCircle = BlackCircle:WaitForChild("WhiteCircle")
local function calculateWhiteCircleOffset(whiteCircle, blackCircle)
local mouseDelta = UserInputService:GetMouseDelta()
-- Vector2.new().Unit returns {NAN, NAN} and if we set a frame's position to
-- {NAN, NAN} then it'll disappear
if mouseDelta == Vector2.new() then
return Vector2.new()
end
-- value between 0 and 1
local distFromOriginAlpha = math.min(
mouseDelta.Magnitude / Vector2.new(10, 10).Magnitude,
Vector2.new(10, 10).Magnitude,
)
local whiteCircleRadius = whiteCircle.AbsoluteSize.X * 0.5
local blackCircleRadius = blackCircle.AbsoluteSize.X * 0.5
-- we're subtracting the white circle radius from the black circle radius
-- so that the white circle doesn't pop out from the black circle at high
-- mouse deltas
local maxDistFromOrigin = blackCircleRadius - whiteCircleRadius
local offset = mouseDelta.Unit * maxDistFromOrigin * distFromOriginAlpha
return offset
end
RunService.Heartbeat:Connect(function()
local whiteOffset = calculateWhiteCircleOffset(WhiteCircle, BlackCircle)
WhiteCircle.Position = UDim2.new(0.5, whiteOffset.X, 0.5, whiteOffset.Y)
end)
here is my attempt at solving your problem.
you may also want to do some smoothing so it isn’t jerky
This is my code that I made for my custom joystick and it works perfectly.
local MaxPosX, MaxPosY = BackgroundFrame.AbsoluteSize.X / 2, BackgroundFrame.AbsoluteSize.Y/ 2
--// BackgroundFrame is the dark background, direction is the same as unit in your script
Circle.Position = UDim2.new(
0.5,
(direction * math.min(magnitude, MaxPosX)).X,
0.5,
(direction * math.min(magnitude, MaxPosY)).Y
)