2D point to 3D point?

So, I was wondering, how would I go about taking a point from the player’s camera and turning it into a ray?

I have tried searching this up, but I get solutions that are from a year ago, and I worry that the API might have changed since then.

If it helps, I want a ray from a 2D point and then it goes into a direction in a 3D space.

Thanks!

1 Like

A 2D point is akin to a Vector2. You would use the function ScreenPointToRay or ViewportPointToRay.

These both accept a X, Y, and depth arguments. The difference is that ScreenPointToRay will consider the top bar of the screen ingame while the other does not.

Example:

local camera = workspace.CurrentCamera
local function viewportRay(vector2)
    return camera:ViewportPointToRay(vector2.X, vector2.Y)
end

A 2D point’s X and Y coordinates can be used in a 3D point form but the Z coordinate must be filled out, a direction in a 3D space provides this Z coordinate. This Z coordinate would be determined by how far the ray can go, which you can call maxDistance.

4 Likes