Frame not at mouse center

I am trying to position the frame at the center of the mouse but there seems to be an offset that is keeping it above the mouse position.

Anchorpoint: 0.5, 0.5

Untitled video - Made with Clipchamp (3)

Code:

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local gui = script.Parent
local frame = gui.Frame.Frame
local parent = frame.Parent

RunService.Heartbeat:Connect(function()
	local mouseLocation = UserInputService:GetMouseLocation()
	local targetPos = mouseLocation - parent.AbsolutePosition

	frame.Position = UDim2.fromScale(targetPos.X / parent.AbsoluteSize.X, targetPos.Y / parent.AbsoluteSize.Y)
end)

Trying tweaking the anchor point to different values

Yes I tried that. I tried 0,0 which makes the center the top left corner and still above the mouse position

Is IgnoreGuiInset on? If it isn’t, turn it on for the ScreenGui you’re using.

Yes, I have IgnoreGuiInset on.

It seems like you’re parenting the frame to another frame. You don’t need to do this to get the screen size. Instead, use the Camera.ViewportSize property and calculate scale based on that.

local mousePos = UserInputService:GetMouseLocation()
local viewportSize = Camera.ViewportSize
local scalePosition = UDim2.fromScale(mousePos.X / viewportSize.X, mousePos.Y / viewportSize.Y)

yeah your right it should be camera viewport

That wouldn’t achieve what I want. I want the frame to position itself to the mouse position even if the frame is inside another frame that is small. That code does follow the mouse but it keeps itself inside the parent.

Ah, I see what you mean.

GetMouselocation does not account for GuiInset. You’re seeing ~60 pixel gap between where it is and where it’s supposed to be. To fix this, you have to manually account for the GuiInset with GuiService:GetGuiInset():

local targetPos = mouseLocation - parent.AbsolutePosition - GuiService:GetGuiInset()

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