Is there a similar method to Mouse.Target that uses UserInputService instead?

For anyone curious:

Use a event and detect if

UserInputService.InputChanged:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.MouseMovement and not gameProcessed then
        local ray = Camera:ViewportPointToRay(inputObject.Position.X, inputObject.Position.Y)
    end
end)

After getting the ray, it’s just a simple matter of ray manipulation, getting Ray.hit, etc.

https://developer.roblox.com/en-us/api-reference/function/UserInputService/GetMouseLocation
Is this what you were looking for?

Ah, not quite, but I’ve got it figured out now, thank you!

1 Like

What’d you do? It’ll help other people that stumble upon this thread in the future with the same question :smile:

1 Like

I’ve edited my solution into the main post!

1 Like

Wanted to leave this here. It’s not much different, except it doesn’t use an event.

local UserInputService = game:GetService("UserInputService")
local Camera = game.Workspace.CurrentCamera

local function GetMouseLocation()
	local Position = UserInputService:GetMouseLocation()
	return Camera:ViewportPointToRay(Position.X, Position.Y)
end

It doesn’t keep executing in the background, only needs to execute when ever called.

1 Like

You should perform the raycasting logic every RenderStepped instead, because the 3D position of the mouse can change if the camera moves (even if the mouse itself doesn’t move). An example would be when you walk forwards and your camera follows (without you moving the mouse at all.

4 Likes