Is there a better way to get the mouse’s hit position, or is this the only method? Because everything is starting to seem really outdated since there’s a lot of Roblox updates, so I’m curious.
4 Likes
Well I personally like to use a function that fires a ray in the direction of the mouse and then have the function return the position of where and what the ray hit.
local function getMouseHit()
local mouseLocation = UserInputService:GetMouseLocation() -- :GetMouseLocation() returns a vector2 which isn't exactly what we need which is why we use it to fire a ray
local viewportPointRay = workspace.CurrentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
local extendedRay = Ray.new(viewportPointRay.Origin, viewportPointRay.Direction * 1000) -- you can change the 1000 to whatever distance you want your ray to reach, great for limiting distances
return WorkspaceService:FindPartOnRay(extendedRay)
end
local hitPart, mousePosition = getMouseHit() -- the variables are self explanatory, you dont have to name them that but that's what I find easiest
Hope this helps you and let me know if you have any questions
6 Likes