Relative Touch Positions and ScreenInsets

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.

Does anyone know how to address this?

Does anyone understand my problem? Are there any 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

GetGuiInset

Tuple

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.


1 Like

Thank you. I didn’t know this function existed. I do have a separate issue that wasn’t in the original post, so I’ll make a new thread for it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.