A function to get a cursor's position in 3d space

Since Mouse is depreciated, I thought I’d post the function I use to get mouse positions in 3d space for my guns and other silly systems.


local RaycastParameters = RaycastParams.new()
RaycastParemeters.FilterType = Enum.RaycastFilterType.Exclude
RaycastParemeters.FilterDescendantsInstances = {} -- add anything you want ignored here.

local Distance = 1000 -- can be set to any number.

function CursorToPosition()
     local MousePosition= UserInputService:GetMouseLocation()
     local UnitRay= Camera:ViewportPointToRay(MousePosition.x, MousePosition.y)

     return workspace:Raycast(UnitRay.Origin, UnitRay.Direction * Distance, RaycastParameters)
end
4 Likes