Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I find where the cursor's position is in the world?

Asked by 7 years ago

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.

2 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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
0
Ah, thank you! crabbyninja 58 — 7y
0
Mouse.Hit.p works too lel Goulstem 8144 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

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.

Answer this question