Custom mouse handler getting offset?

I’m currently using a custom mouse handler in my game due to how the existing mouse is limited. It’s purpose is to mainly add new features to make a system similar to the existing mouse, but with room for adding things that are needed in my game. The only issue is at long distances from the player, the position my mouse script returns is offset.

Here’s the code for my mouse module:

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

local camera = workspace.CurrentCamera
local lengthCap = 500

local Mouse = {
	Hit = CFrame.new(),
	HitNormal = Vector3.new(),
	Target = nil,
	TargetFilter = {}
}

local function FindFilteredPartOnRay(ray)
	local ignoredParts = {}
	local foundPart
	local foundPosition
	local hitNormal
	
	for index, filtered in pairs(Mouse.TargetFilter) do
		if filtered then
			table.insert(ignoredParts, filtered)
		else
			table.remove(Mouse.TargetFilter, index)
		end
	end
	
	
	while not foundPart do
		local currentPart, currentPosition, currentNormal = workspace:FindPartOnRayWithIgnoreList(ray, ignoredParts)
		
		if currentPart then
			if currentPart.Transparency ~= 1 then
				foundPart = currentPart
				foundPosition = currentPosition
				hitNormal = currentNormal
			else
				table.insert(ignoredParts, currentPart)
			end
		else
			break
		end
	end
	
	return foundPart, foundPosition, hitNormal
end

function Mouse:GetPosition()
	return UserInputService:GetMouseLocation()
end

function Mouse:BindToMouseMovement(func)
	UserInputService.InputChanged:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseMovement then
			func()
		end
	end)
end

RunService:BindToRenderStep("MouseUpdate", 1, function()
	local position = Mouse:GetPosition()
	local unitRay = camera:ViewportPointToRay(position.X, position.Y + 36)
	local ray = Ray.new(unitRay.Origin, unitRay.Direction * lengthCap) --TODO: Transfer to https://developer.roblox.com/en-us/api-reference/function/WorldRoot/Raycast
	local hitPart, hitPosition, hitNormal = FindFilteredPartOnRay(ray)
	
	if hitPart then
		Mouse.Target = hitPart
	end
	if hitPosition then
		Mouse.Hit = CFrame.new(hitPosition)
	end
	if hitNormal then
		Mouse.HitNormal = hitNormal
	end
end)

return Mouse


Notice how the primary part of the object is very close to the mouse when it is near the player/camera:

image



When the object is far from the player/camera, the center of the mouse is nowhere near the primary part of the object:

image

I find that the slabs shown in each picture do not seem the same to me.
It looks like the slab is partly transparent.
Is the Primary Part you are referring to inside the slab?
If it is could you make it bright yellow so we know what the target for the mouse is.
I cannot tell from the pictures much about distance as its not viewing at a good angle.
Can you show us a view from up high so we can see the distances involved.

1 Like

The entire positioning of the mouse hit seems to be off a decent amount. This distance becomes quite large at distance. Here are the images you asked for:

(The yellow cube is the primary part)

1 Like

Thank you very much for the images.
Have you tried printing out and comparing the camera.focus and mouse.hit?

1 Like

I’ve fixed this by removing the + 36 on this line:

local unitRay = camera:ViewportPointToRay(position.X, position.Y + 36)

I read on a DevForum post that you needed the + 36 but after removing it, it fixed the offset, which makes sense now. Not sure what the + 36 accomplished.

1 Like