Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How does ScreenPointToRay Work?

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

✓ Accepted answer

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 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.