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.
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.
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.