How get mouse position scale or offset relative to a frame

I want to get the position (scale or offset in pixels) of the mouse relative a frame, i am trying with:

local function getMouseScalePos(x,y)
	local displyAP = gameDisply.AbsolutePosition
	local displyAS = gameDisply.AbsoluteSize
	
	local screenAP = mainScreen.AbsolutePosition
	local screenAS = mainScreen.AbsoluteSize
	
	local x = (x - ( (screenAS.X-displyAS.X) * 0.5) ) / displyAS.X
	local y = (y - ( (screenAS.Y-displyAS.Y) * 0.5) ) / displyAS.Y 
	local x, y = math.clamp(x,0,1), math.clamp(y,0,1)
	return Vector2.new(x,y)
end

local pos = getMouseScalePos(mouse.X+mouseInset.X,mouse.Y+mouseInset.Y)

The problem is that the code only will work with a frame with a position of (0.5,0,0.5,0) and an anchor point of (0.5,0.5), it dont works with any rotation netheir. (i dont need the rotation anyways)

Any help is welcome

Try replacing the 0.5 number in local x and y with screenAS.AnchorPoint.X and .Y respectively

2 Likes

I already try it, is not working :sob:

(stupid Gui Inset) (Still not working with rotation)

Well i did it now:

local function getMouseScalePos(x,y)
	local anchorP = gameDisply.AnchorPoint 
	local displyAP = gameDisply.AbsolutePosition + game:GetService("GuiService"):GetGuiInset()
	local displyAS = gameDisply.AbsoluteSize
	
	local screenAP = mainScreen.AbsolutePosition
	local screenAS = mainScreen.AbsoluteSize
	
	local x = (x - displyAP.X) / displyAS.X
	local y = (y - displyAP.Y) / displyAS.Y
	local x, y = math.clamp(x,0,1), math.clamp(y,0,1)

	return Vector2.new(x,y)
end
local pos = getMouseScalePos(mouse.X+mouseInset.X,mouse.Y+mouseInset.Y)
1 Like

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