One of my friends told me that she couldn’t get raycasts to work when using Camera:ScreenPointToRay
.
I looked into it, and found that it sends out a unit ray with a length of 1. The expected behavior is that this raycast can detect any part in front of it, regardless of distance. The actual behavior is that it only detects parts that are at most 1 stud in front of the ray.
I’ve known that rays behave this way for awhile, but I gotta ask: why? The API seems to imply that the directional vector is supposed to be a unit vector with no meaning to its length, and yet it acts like a line where the direction is the offset to from the origin, and the length is defined by the offset’s magnitude. This contradicts the definition of a ray.
To demonstrate the oddity of all this, I made a LocalScript that visualizes the result of ScreenPointToRay raycasts:
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local debugPart = Instance.new("Part")
debugPart.Shape = "Ball"
debugPart.Material = "Neon"
debugPart.Anchored = true
debugPart.CanCollide = false
debugPart.Size = Vector3.new(.2,.2,.2)
local function step()
local c = workspace.CurrentCamera
if c then
local mPos = UserInputService:GetMouseLocation()
local ray = c:ScreenPointToRay(mPos.X,mPos.Y,0)
local hit,pos = workspace:FindPartOnRay(ray,debugPart)
debugPart.CFrame = CFrame.new(pos)
debugPart.Color = Color3.new(hit and 0 or 1, hit and 1 or 0,0)
debugPart.Parent = c
end
end
RunService:BindToRenderStep("RayMouseTest",201,step)
The result is that the red sphere moves 1 stud away from the near clipping plane and then stops.
I can only make it work if the part I’m hitting is really close to the camera.
This issue can be “fixed” if you put this line under the local ray
declaration:
ray = Ray.new(ray.Origin, ray.Direction * 5000)
I’m surprised no one has brought this issue up before. It’s weird that you have to reconstruct a ray in order to extend its “range”. Shoudn’t raycasts just assume that unit rays want the maximum length when performed?
This seems like a bug to me.