ScreenPointToRay help

Hi, can someone help me understand a bit of cam:ScreenPointToRay?
it returns like mouse.Hit.P?
I don’t really know what it does

I mean, what’s a Unit ray?

1 Like

A unit ray is a ray with a direction vector that has a magnitude of 1. Basically, this function creates a ray that starts at the camera, and points out of the camera directly towards the 3d location of the pixel at that point. The ray that it gives you is 1 stud long, so if you want to use it for ray casting you will need to extend it.

4 Likes
local ray = camera:ScreenPointToRay(x, y, depth) --returns ray

ray.Unit --The Ray with a normalized direction
ray.Origin --The position of the origin
ray.Direction --The direction vector of the ray

So, what’s the difference between that and Camera.CFrame.LookVector ?

Oh, so it’s literally Mouse.Hit.Position ?
But as a ray?

(I mean depends on the Y and X value that you give?)

But doing like
Ray.new(Camera.CFrame.Position,Mouse.Hit.Position)
It’s the same?

Camera.CFrame.LookVector is similar, but not the same. LookVector will give you a Vector3, not a Ray, and it will only point straight forward from the camera.
ScreenPointToRay will give you a Ray and yes, it is more or less like Mouse.Hit.Position but as a ray, and depending on the values you give it instead of the mouse’s position. You could recreate Mouse.Hit.Position by using this function with the mouse’s position and raycasting to find what gets hit.

1 Like

Okay, thanks, but what’s this useful for?
Did you ever used it?

I did use it, if you look at the path tracing engine I showcased on my portfolio. I used it to get the starting position of every ray.

1 Like