How to get the 4 corners of a gui?

The title is self explanatory.

From what I know there’s no way of getting the corners of a GUI, if I’m jot wrong it’s simply impossible, sorry

It is possible, I believe the formula is something like (x_pos-x_size/2)-(y_pos-y_size/2) for the top left, but it’s not working for me. It’s some math with the positions and sizes.

Are you trying to get the raw coordinates from the top-left of the screen or are you making other GUI elements based off the corners of another object?

The raw coordinates of all corners.

If I’m not wrong the formula would be something like: sizeX/2 and sizeY/2 for the top left, and multiply for the down right corner if I’m into wrong

local topLeft = gui.AbsolutePosition
local topRight = topLeft + Vector2.new(gui.AbsoluteSize.X, 0)
local bottomLeft = topLeft + Vector2.new(0, gui.AbsoluteSize.Y)
local bottomRight = topLeft + Vector2.new(gui.AbsoluteSize.X, gui.AbsoluteSize.Y)

These give Vector2’s that represent the difference of a corner’s position from the origin of the screen.

Just a note, in your original post, you use halfs of the Size, which is normally something I only see when working with Parts. GuiObjects for the most part only start at their top left corner unless you set AnchorPoint to something other than (0,0).

2 Likes

image
It doesn’t seem to be the exact position.

How are you using the coordinates? If you’re just directly using it in the Position, it’ll be offset by its Parent.

I plan to use it for GUI collision detection, but now I’m just using frames to see where the points are.

If you’re using a script to set up this frame, show me the line that’s setting the Position and Size. If you’re just doing this in the property window, show me both values.

Since they’re raw screen coordinates, your frame needs to either have no other GuiObject ancestors or you need to subtract the Parent’s AbsolutePosition to just get the offset (if it’s deep in some structure).

GUI collision detection logic has almost certainly already been published by other developers. You might want to take a look at their code to see what details you’re missing.

The frame’s parent is a ScreenGUI.

script.Parent.Parent.Frame.Position = UDim2.fromOffset(topLeft.X, topLeft.Y)

Unless you’ve edited AnchorPoint or Rotation, this Frame’s top-left corner is exactly where the original GUI’s top corner is. Could you provide a cropped screenshot of your Explorer with all the relevant instances, and another screenshot of the properties window for the original GUI that you’re getting the corner from.

image

Your script is setting “Frame”'s (Frame named “Frame”) Position to the top-left corner, yet the properties screenshot you provided shows a name of “Frame”. Are there two Frames named “Frame”? Which frame is the one that you’re trying to fetch the corner of, and which frame is supposed to be using the corner information?
The script also uses script.Parent.Parent.Frame which makes sense if there’s another script inside the script shown in the screenshot. If there isn’t, then it’s accessing a Frame which is a sibling of ScreenGui which would definitely not be relevant because you didn’t include it in the screenshot.

1 Like

Oh wait, I gave you a screenshot of the properties of the frame lol. Here’s the one of the frame that I’m getting the corner from. There’s this frame, and there’s the other frame that I’m setting the position to the top left corner of this frame.

1 Like

When setting the position, make sure to adjust the anchor point; this is critical for the behavior you’re looking for (assuming that behavior is keeping the frame in the parent are).

To adjust the anchor point programmatically, get the “percent” value of how far it is on the x and y axis. For example, if your sub-frame is positioned 75% towards the right and 50% towards the bottom, your anchor point should be 0.75, 0.5.

1 Like