UserInputService to Get mouse location

When using userinputservice I try to get the player’s mouse location in a 3D space yet the ray that gets casted seems to be lower than the player’s mouse by what seems around a stud

robloxapp-20200506-1824181.wmv (731.9 KB)

				local camera = workspace.CurrentCamera
				local mouse = uis:GetMouseLocation()
				local ray = camera:ScreenPointToRay(mouse.X, mouse.Y, 1)
				local ray = Ray.new(ray.Origin,ray.Direction * 999)
				local part,pos = workspace:FindPartOnRayWithIgnoreList(ray,ignore)
1 Like

Have you tried using camera:ViewportPointToRay() instead of camera:ScreenPointToRay? I haven’t had that happen to me before.

1 Like

That works, but I was wondering why it wouldn’t work with screenpointtoray?

That’s probably because GetMouseLocation does not account for GUI inset, to fix that you can try subtracting, (if i remember correctly) something like 36 pixels from the mouse position or use the GuiService:GetGuiInset() function.


So Something like:

local mouse = uis:GetMouseLocation() - Vector2.new(0,36)

or

local inset = GuiService:GetGuiInset()
local mouse = uis:GetMouseLocation() - inset
3 Likes

One of them doesn’t take into account the top bar, which is a CoreGui. They are used separately
depending on how you get the mouse location.

1 Like

Strange. The developer docs indirectly say that ScreenPointToRay should be more accurate (see the reasoning in the image), but for you, it’s less accurate than ViewportPointToRay. When I tried it, neither of them had any offset.

It might have something to do with the third parameter of the PointToRay functions:

1 Like