Camera:ScreenPointToRay doesn't work correctly for ViewportFrames

Currently it is possible to raycast within ViewportFrames by using a WorldModel. I wanted to use this to be able to detect when parts in a ViewportFrame are clicked so I opted to use ScreenPointToRay on my ViewportFrame’s camera. The function doesn’t work as expected and produces a ray with the complete wrong direction.

System Information:
Intel (R) Core™ i7-6700K CPU @ 4.00GHz, 32 GB, NVIDIA GeForce RTX 2080 SUPER

Repro File:
ViewportFrame_ScreenPointToRay_Repro.rbxl (53.1 KB)

In the repro I’ve created a simple ViewportFrame with a camera pointing down at a baseplate. In play solo, clicking on the ViewportFrame will attempt to create a ball at the position where you click, and clicking outside of the ViewportFrame does the exact same thing but in the workspace. Observe how the ball appears in the workspace but never in the ViewportFrame, because ScreenPointToRay gives the wrong direction.

A user provided workaround to this problem here which I have tested and works but this is how the real functions should behave.

Note that this issue happens with both the ScreenPointToRay as well as the ViewportPointToRay functions

Expected behavior

If everything was working correctly, ScreenPointToRay and ViewportPointToRay would work in ViewportFrames exactly how they do on the workspace camera.

3 Likes

Thanks for the report! We’ll follow up when we have an update for you.

2 Likes

Hi, thanks again for your report, I wanted to follow up on this.

Getting a 3D ray from a click/tap in a ViewportFrame is definitely a valid usecase, but unfortunately we don’t currently provide a built-in API for this.

Camera:ViewportPointToRay/ScreenPointToRay currently only works for the main viewport/workspace camera. The reason is because currently a Camera.ViewportSize is only updated when it is set to Workspace.CurrentCamera. So if a Camera is set to ViewportFrame.CurrentCamera (like in your placefile), the Camera.ViewportSize isn’t updated to the ViewportFrame’s AbsoluteSize and remains at the default of (1,1) pixels. This results in Camera:ViewportPointToRay() calls on this camera being incorrect, because the camera thinks the viewport size is 1x1 pixel when it is usually larger.

We also can’t update the Camera’s ViewportSize when it is set by a ViewportFrame’s CurrentCamera because the same Camera could be the CurrentCamera for multiple ViewportFrames and/or the workspace. So, there isn’t always a unique ViewportSize.

I think the API you are looking could be something like ViewportFrame:ScreenPointToRay(x, y): Ray, is this correct? If so, would you be able to submit a feature request for this API (or what API you are looking for) in the #feature-requests section please?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.