Like say my cursor is pointing at some point far off in the distance, when I click it would for example print the position of where it's pointing at. I haven't really gotten a clear answer by googling it either.
✓ ANSWER ACCEPTED
How do I find where the cursor's position is in the world?
2 answers
✓ Accepted answer
You need raycasting. Here's the function I use:
function Convert2DTo3D(camera, x, y, maxDist, ignoreList)
--returns the object hit followed by the coordinate hit
--maxDist and ignoreList are optional. ignoreList can be a table or an instance.
local ray = camera:ScreenPointToRay(x, y)
ray = Ray.new(ray.Origin, ray.Direction * (maxDist or 999.5))
local basePart, hit
if type(ignoreList) == "table" then
basePart, hit = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
else
basePart, hit = workspace:FindPartOnRay(ray, ignoreList)
end
return basePart, hit
end
--Sample usage:
print(Convert2DTo3D(workspace.CurrentCamera, 50, 100))
In the example case I used coordinates 50, 100. You'll want to use the mouse's actual coordinates; possibly on MouseMove (see UserInputService)
The general idea is really just these two steps:
- Convert where the mouse cursor is into a ray using
ScreenPointToRay - Raycast this ray to find what it hits using one of the workspace's functions
2 comments
0
Ah, thank you!
0
Mouse.Hit.p works too lel
mouse.hit
Not entirely sure how you'd find the position, most likely using raycast which I haven't done in a few months.
Do some digging on the wiki with mouse.hit and I'm sure you'll find what you're looking for.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
http://wiki.roblox.com/index.php?title=API:Class/Mouse/Hit