Clicking An Object Through a Viewport

Currently for my game, I have it so you can access one of several skill trees depending on your class and stuff. They’ll appear through a viewport frame in a GUI, and all of them work. Previously, it would edit the camera to pan to their positions when clicked, which is what I intend to be able to do through the viewport frame, changing the camera of the viewport when clicked on through the viewport.

The issue is that Click Detectors can’t be clicked on in viewport frames, but after scrounging around the DevForum for a bit I saw posts on both sides. One claiming it would be possible via the use of raycasting, and the other saying it would be impossible as viewports are simply to look through, nothing more. I’ve looked around for hours and have found absolutely nothing to help, and those posts are from years ago. Nothing on raycasting, skill trees through GUIs, absolutely nothing. I am completely stumped. Is it even possible?

This may be possible with the use of a WorldModel and raycasting. I imagine the math would be pretty complex and far beyond my skills (unless there’s API methods exclusively made for this), but what you would have to do is:

  1. Find the 3D position of the mouse in the ViewportFrame, this would be relative to the camera (how I do not know, but it is 100% possible)
  2. Perform a raycast forwards based on the cameras look direction and set the origin to the value obtained above

The result from this will be the mouse target in the viewport

1 Like

So it is possible. Thank you, I really needed to hear that all of my work wasn’t for nothing :slight_smile:

So I did some messing around and tested to see if the GUI really moved when clicked on. Despite having the script react when the skill tree is clicked on inside the WorldModel (which is deep in the GUI itself), I copied and pasted the folder containing them into the workspace. Clicking on one of the nodes in the workspace would send the message to the script to change the part the camera is focused on, despite not at all being pointed to in the code itself. Is this normal? I haven’t seen this kind of behavior before.

The folder containing all the skill trees lies at the same place the models in the workspace are. I even changed the location of the skill trees to be underneath where the regular ones are, separated. Despite that, I clicked on the one below the platform and the GUI changed. This means that the ones in the workspace are completely connected to the ones in the WorldModel inside the GUI, despite the code not at all recognizing them.

It’s going to be really hard to understand your problem without visuals, can you take a screenshot of the hierarchy/video recording of the issue?

robloxapp-20220728-2048349_Trim.wmv (1.6 MB)

The footage shows me clicking on the Skill Tree in the Workspace and the Viewport Frame follows, yet the code itself:

Has the code connected to the one in the WorldModel:


The same thing would happen when the workspace models were taken underneath the platform. Clicking where the WorldModel ones are don’t do anything, but clicking the Workspace models beneath the platform do change the camera.

On top of that, after doing some testing and changing the “Unlock Skill” system from the original version, the one on the Workspace will have the parts glow white while the one in the Viewport Frame will not.

robloxapp-20220728-2108462_Trim.wmv (447.5 KB)

Here’s footage of the Workspace model on the other side yet still moving the one in the GUI:

robloxapp-20220728-2115024_Trim.wmv (1.1 MB)