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

How does ScreenPointToRay Work?

Asked by 8 years ago

I started using UserInputService and I found that I would have to cast my own mouse ray to get the typical Mouse.Target/Mouse.Hit properties I'm accustomed to.

I found ScreenPointToRay which looked like it would do what I needed, but I've encountered an issue

    --input is an InputObject, camera is CurrentCamera
    local ignore = {}
    local mouseRay = camera:ScreenPointToRay(input.Position.X, input.Position.Y, 999)
    local target, hit = workspace:FindPartOnRayWithIgnoreList(mouseRay, ignore)

I've found that FindPartOnRay is ignoring all parts leaving target nil and hit's position is at the end of the ray. Am I doing this wrong here or could there be a bug on Roblox's end? I'm open to alternatives as long as it doesn't use the mouse.

Edit: I've found the depth property of ScreenPointToRay to be an offset for the Ray.Origin and the Ray length is 1 stud...

1 answer

Log in to vote
1
Answered by 8 years ago

You're on the right track for solving your own problem. All you have to do is create a new ray with the proper length:

ray = Ray.new(ray.Origin, ray.Direction * 999))

ScreenPointToRay clearly returns a unit Ray, whereas FindPartOnRayWithIgnoreList expects the ray to have substantial length.

1
Thanks, that's what I went with last night. I wish the documentation was a little more clear, so I can avoid running around like this. MrgamesNwatch 65 — 8y
Ad

Answer this question