ViewportPointToRay
This function creates a unit DataType/Ray from a 2D position on the viewport (defined in pixels). This position does not account for the GUI inset. The DataType/Ray originates from the DataType/Vector3 equivalent of the 2D position in the world at the given depth (in studs) away from the Camera.
As this function does not account for the GUI inset, the viewport position given is not equivalent to the screen position used by GUI elements. If you are not using ScreenGui/IgnoreGuiInset and need an otherwise identical function that accounts for the GUI offset, use Camera/ScreenPointToRay.
This function can be used in conjunction with the Camera/ViewportSize property to create a ray from the centre of the screen, for example:
local camera = workspace.CurrentCamera local viewportPoint = camera.ViewportSize / 2 local unitRay = camera:ViewportPointToRay(viewportPoint.X, viewportPoint.Y, 0)
As the DataType/Ray created is a unit ray it is only one stud long. To create a longer ray, you can do the following:
local camera = workspace.CurrentCamera local length = 500 local unitRay = camera:ScreenPointToRay(100, 100) local ray = Ray.new(unitRay.Origin, unitRay.Direction * length)
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
|
|
The position on the X axis, in pixels, of the viewport point at which to originate the |
||
|
|
The position on the Y axis, in pixels, of the viewport point at which to originate the |
||
|
0
|
The depth from the |
Returns
| Return Type | Summary |
|---|---|
|
A unit |