Limit frame following mouse in circular zone

I am doing a frame that follow mouse limited by his parent size, but I want get his parent size like it is a circle, how could I make this?

Example:

What I’ve already done:

             local mousPos =  UDim2.new(0, Mouse.X - Frame.AbsolutePosition.X, 0, Mouse.Y - Frame.AbsolutePosition.Y)
						
						local vectorX = mousPos.X.Offset
						local vectorY = mousPos.Y.Offset

						local maxX = Frame.AbsoluteSize.X
						local maxY = Frame.AbsoluteSize.Y

						if vectorX > maxX then
							local difference = vectorX-maxX
							mousPos -= UDim2.fromOffset(difference, 0)
						end
						
						if vectorX < 0 then
							local difference = vectorX
							mousPos -= UDim2.fromOffset(difference, 0)
						end
						
						if vectorY > maxY then
							local difference = vectorY-maxY
							mousPos -= UDim2.fromOffset(0, difference)
						end

						if vectorY < 0 then
							local difference = vectorY
							mousPos -= UDim2.fromOffset(0, difference)
						end

						Button.Position = mousPos

Video:

1 Like

heres some pseudocode

--if the mouse is outside of the circle, using the distance formula
local distance = math.sqrt(distanceX^2 + distanceY^2)
if distance > maxDistance then

  --get the angle between the center of the circle and the mouse
  local angle = math.atan2(distanceY, distanceX)

  --move the circle with to a distance with magnitude maxDistace and with the angle
  local newPosition = UDim2.new(
    0, centerX + math.cos(angle)*maxDistance, 
    0, centerY + math.sin(angle)*maxDistance,
  )
else
  --move normally
end
2 Likes

How do I get values ​​like: “distanceY”, “distanceX” or “maxDistance”?

I get it

						local mousPos =  UDim2.new(0, Mouse.X - Frame.AbsolutePosition.X, 0, Mouse.Y - Frame.AbsolutePosition.Y)
						
						local distanceX = (mousPos.X.Offset - CenterPosition.X.Offset)
						local distanceY = (mousPos.Y.Offset - CenterPosition.Y.Offset)
						local maxDistance = Frame.AbsoluteSize.X / 2

						--if the mouse is outside of the circle, using the distance formula
						local distance = math.sqrt(distanceX^2 + distanceY^2)
						if distance > maxDistance then
							local angle = math.atan2(distanceY, distanceX)

							--move the circle with to a distance with magnitude maxDistace and with the angle
							local newPosition = UDim2.new(
								0, CenterPosition.X.Offset + math.cos(angle)*maxDistance, 
								0, CenterPosition.Y.Offset + math.sin(angle)*maxDistance
							)
							
							Button.Position = newPosition
						else
							Button.Position = mousPos
						end
1 Like