Why does the selectionBox move further away the higher the mouse Y position?

Here’s my code that manipulates the mouse:

local mousePositionX
local mousePositionY
local gameProcessed
function createRay()
	if mousePositionX and gameProcessed == false then
		local unitray = workspace.Camera:ViewportPointToRay(mousePositionX,mousePositionY)
		local ray = Ray.new(unitray.Origin, unitray.Direction * 500)
		local hit = workspace:FindPartOnRayWithIgnoreList(ray,IgnoreList)
		if hit then
			game.ReplicatedStorage.print:FireServer(hit.Name)
			selBox.Adornee = hit
		end
	end
end

function findHover(input,gameProcessedEvent)
	if debounceMouse == false and input.UserInputType == Enum.UserInputType.MouseMovement and not gameProcessedEvent or debounceMouse == false and input.UserInputType == Enum.UserInputType.Touch and not gameProcessedEvent then
		debounceMouse = true
		mousePositionX = input.Position.X
		mousePositionY = input.Position.Y
		gameProcessed = false
		createRay()
		debounceMouse = false
	elseif gameProcessedEvent then
		gameProcessed = true
	end
end

Here’s an example of the behaviour described:
https://gyazo.com/0028f100a18492f8866b1f61f24a70e9

I’m assuming it’s something to do with where the ray is hitting, but I’m not really sure.

Also note, the cursor is doubled because recording with gyazo in studio duplicates the mouse for some reason.

I believe this is because of the GUI inset. Some screen position functions and properties don’t take the 36-pixel topbar into account and others do.

Is there any reason you need to use this rather than Mouse.UnitRay?

Hey, I’ve heard that using UserInputService is more effective and has advantages. the script ran on Player:GetMouse() before but I’ve decided to rewrite it using UIS as a learning experience.

Hmm, you were right about the Topbar 36 pixels, so I’ve just added 36 to the mousePositionY and now it works!

Here’s the updated portion of the code:

function findHover(input,gameProcessedEvent)
	if debounceMouse == false and input.UserInputType == Enum.UserInputType.MouseMovement and not gameProcessedEvent or debounceMouse == false and input.UserInputType == Enum.UserInputType.Touch and not gameProcessedEvent then
		debounceMouse = true
		mousePositionX = input.Position.X
		mousePositionY = input.Position.Y + 36
		gameProcessed = false
		createRay()
		debounceMouse = false
	elseif gameProcessedEvent then
		gameProcessed = true
	end
1 Like

Just as an FYI, you could also replace ViewportPointToRay with ScreenPointToRay to accomplish this without adding 36px to the mouse position. The only difference being, as the devhub states, that ScreenPointToRay accounts for the topbar.

4 Likes

Oh, that’s splendid, thank you