Get center of GuiObject?

I’ve been trying to get the center of any type of frame, but I literally can’t.
I’ve tried to use the AbsolutePosition and AbsoluteSize to track the middle of any type of Frame, but everytime I change the size, it doesn’t track the middle.
Is there any way I could get any type of center from any sized GuiObject?

Along the lines of:

Centre = {
    X = AbsolutePosition.X + (AbsoluteSize.X / 2),
    Y = AbsolutePosition.Y + (AbsoluteSize.Y / 2)
}

and make sure to update on .Changed or :GetPropertyChangedSignal('Size' or 'Position').

2 Likes

Setting the anchor point to 0.5, 0.5 will make positioning of the GUI element based on the center of the frame.

The default is the left upper corner I believe. Just take the sizes and divide them by two and either add or subtract.

1 Like

I tried your idea and it seems to go here.
image
I don’t know if it was supposed to go into Scale or Offset, but I did offset.

What “sizes” am I dividing exactly? X and Y of the AbsoluteSize?

you’d have to set the AnchorPoint of the grey frame to 0.5, 0.5 for mine to work in that scenario.

The thing is, I’m not using the gray frame. It’s to show where the center of the frame is. Even if the gray frame is in the middle, the UDim position is not.

The maths in my post will return the middle of the frame, regardless of its size or position, however, the grey one will need it’s anchor point changed to display correctly. the UDim position should be set using offsets, not scale.

1 Like

As I said, I’m using the gray point to see where the center is. It’s only used for checking if it is in the middle of the part. If that wasn’t obvious, I apologize. I am using the gray frame to point where the frame’s center is, then when it is correct everytime, I will be removing it.

i understand that, but only the top left corner of the grey frame will be at the centre unless the anchor point is changed

1 Like

I get that. Your solution with the gray frame worked fine, but it was a little too high. Aside from that, it did indeed work how it was intended to.

You might have to offset the Y value with the height of the topbar (iirc it was 36px).

I know, but I don’t want to offset the Y value because the gray frame is going to be useless later on. As I said:

I’m trying to do this without the gray frame having an anchorpoint.

This is an older topic, but I thought it was pretty inconclusive.

To Clarify:

AbsolutePosition is the top left corner location (rotation doesn’t affect this) ⠀⠀⠀⠀ -Relative to Screen
Position is the AnchorPoint location ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀-Relative to Parent

Getting TopLeft:

⠀To get the Center using the Position property, use this:

--//Gets the TopLeft Corner with an AbsoluteSize, Position Offset, and AnchorPoint
function GetCorner(sz, pos, anch)
	pos = typeof(pos) == 'UDim2' and Vector2.new(pos.X.Offset, pos.Y.Offset) or pos
	anch = anch or Vector2.new(0,0)
	return pos+(sz*anch)
end

local obj = game.StarterGui.ScreenGui.Frame
local Corner = GetCorner(obj.AbsoluteSize, obj.Position, obj.AnchorPoint)

local Center = Corner+(obj.AbsoluteSize/2)

Use Cases:

⠀You might need to use this when trying to find the center without applying it to a GuiObject, If you’d rather not use Roblox’s AbsolutePosition property. Also, getting the center of a GuiObject, while it’s nested in another, because the AbsolutePosition property equals a screen position, while the Position property equals a nest relative.

3 Likes