Converting mouse position relative to frame scale?

Hello!

For the past day I have been attempting to convert the mouse position relative to a frame that I have. Inside of the frame there are icons that snap to different positions relative to the frame using scale. Currently, the mouse is converted to the scale of the screen size. I have tried converting using the frame’s absolute size, but that has not worked. Here is my current code:

local camera = game:GetService("Workspace").CurrentCamera
local viewSize = camera.ViewportSize

local mousePos = UserInputService:GetMouseLocation()
local frame = ["PathToFrame"]

local convertedMouse = UDim2.new(mousePos.X/viewSize.X, mousePos.Y/viewSize.Y)

There is more code below which handles the snapping. The above is where the issue seems to occur.

6 Likes

There is a way better alternative for getting the mouse position on the screen.

It returns the value as Vector2, which is similar to Udim2.

Example snippet:

local UserInputService = game:GetService("UserInputService ")

local mouseLocation = UserInputService:GetMouseLocation()


local converted = Udim2.new(0, mouseLocation.X, 0, mouseLocation.Y)

If you every worry about the values being on the offset side, as the mouse location depends on the screen size, the positions will be fixed and will be accurate in all devices.

2 Likes

If this works for you, please mark it as the solution :smiley:.

3 Likes

if i understand you correctly what you want to do, then you are half way there

All you need to do is subtract the mouse position with frame absolute position and then divide the outcome by the viewportSize

local camera = game:GetService("Workspace").CurrentCamera
local viewSize = camera.ViewportSize

local mousePos = UserInputService:GetMouseLocation()
local frame = ["PathToFrame"]
local frameSize = frame.AbsoluteSize

local Offset = mousePos - frameSize

local convertedMouse = UDim2.fromScale(Offset.X / viewSize.X, Offset.Y / viewSize.Y)
2 Likes

This has seemed to work the best so far of the two options I have been given, however, the scale seems to be off slightly. The position on the x-axis reads .25 where it should be .16. Similarly, on the y-axis it reads -.15 where it should be .1. Is there any way to correct it more?

2 Likes

Oh im silly its supposed to be the AbsolutePosition of the frame not the size

1 Like

This seems to fix some of the issue, but now it seems as if it thinks the frame is positioned in a different spot. When my mouse is over where the frame is, on the far left of the frame (0,0), it prints “0,0” - roughly. As I move my mouse away from that position and closer to the other end of the frame, it prints something like (.25,0) where it should be (1,0). Same thing on the y-axis.

EDIT: Here is my code:

local viewSize = camera.ViewportSize
				
local mousePos = uis:GetMouseLocation()
local frame = ["PathToFrame"]
local frameAbsPos = frame.AbsolutePosition
				
local offset = mousePos - frameAbsPos
				
local convertedMouse = UDim2.fromScale(offset.X/viewSize.X, offset.Y/viewSize.Y)
				
print(convertedMouse)
				
for _, position: UDim2 in possiblePositions do
	if (Vector2.new(convertedMouse.X.Scale, convertedMouse.Y.Scale) - Vector2.new(position.X.Scale, position.Y.Scale)).Magnitude <= CONFIG.iconMoveDelta then
		icon.Position = position
	end
end
2 Likes

This didn’t work at all. How does it relate to the scale of a device? At (0,0) it prints (-.7, -.3)

2 Likes

It’s my silliness. I couldn’t really understand what you were trying to say. Sorry. :frowning:. I think I won’t be able to assist you with this.

3 Likes

Basically, what I am trying to do is, if the mouse is inside of the frame then it will be to scale with the frame.

Ex: If the mouse position is in the middle of the frame then the mouse position will get converted to (.5,0,.5,0)

1 Like

Your question is a little vague, but, here’s what I think you’re trying to achieve:

image

You want to get the percent relative position of the mouse (pink) in the frame (blue). If you want to achieve this, you can do:

local Camera = workspace.CurrentCamera
local Frame = --> frame

--> removing the offset (x, y) from both the mouse and frame position
local mousePos = UserInputService:GetMouseLocation() - frame.AbsolutePosition
local frameBottomRight = Frame.AbsoluteSize

local convertedMouse = UDim2.fromScale(
    mousePos.X / frameBottomRight.X,
    mousePos.Y / frameBottomRight.Y
)
4 Likes

This worked perfectly. Thank you!!!

EDIT: I love the visual by the way. That is exactly what I was looking for I just had no idea how to explain it lol

2 Likes

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