I’m working on a UI-only game. I have a ScreenGui using DeviceSafeInsets. When I TouchTap a frame, I want to get the position relative and scaled to the frame, but I keep incurring a device offset (due to a screen cutout for the camera) that I cannot account for.
The AbsolutePosition of the ScreenGui does not account for the X-offset caused by the device inset, so whenever I try to TouchTap, it’s always offset. See image below.
The coding assistant doesn’t even check the API because it thought UserInputService had a GetDeviceInsets method, which it doesn’t, so I’m out of ideas.
You get the GUIInset, and when setting the position of an element you subtract the position by GUIInset.
local GuiInset = GuiService:GetGuiInset()
local function isInside(rbx,x,y) -- rbx is the UI element. x,y are returned position after click.
local absPos = rbx.AbsolutePosition
local absSize = rbx.AbsoluteSize
local topLeft = absPos
local bottomRight = absPos + absSize
local y = y - GuiInset.Y -- Account for GuiInset.
local isInside = x > topLeft.X and
y > topLeft.Y and
x <= bottomRight.X and
y <= bottomRight.Y
return isInside
end
Returns two Vector2 values representing the inset of user GUIs in pixels, from the top left corner of the screen and the bottom right corner of the screen respectively.