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.
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.
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)
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?
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
Your question is a little vague, but, here’s what I think you’re trying to achieve:
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
)