Mouse.Hit in ViewportFrame

Hey all!

I’m aware that this topic has been mentioned a few times on the forums. However, most of these topics pre-date the addition of WorldModels for viewport frames.

How could I implement the raycasting offered by WorldModel to achieve the functionality of Mouse.Hit in a ViewportFrame? Documentation and related issues are rather vague in their description of this topic.

Edit: I should probably add, I specifically need to know how to get the mouse’s world position inside the viewport, based on where the mouse is in the viewport.

2 Likes

You should be able to use the Camera:ViewportPointToRay() function in order to cast a ray from a point on the ViewportFrame. Assuming of course that you do have a camera inside the frame.

1 Like

Doh! Glossed over that in the documentation. Thanks!

Update:
After many days of trial and error, unable to come up with a solution that eloquently solves this issue using ViewportPointToRay.

Reiterating the question, how could I achieve effectively the same result as Mouse.Hit in the workspace, but in a ViewportFrame?

Call on CurrentCamera of Viewport method ScreenPointToRay, and use direction to fire Raycast from WorldModel of ViewportFrame. In theory this should work.

Could you provide an example? I’ve tinkered with this solution for days now and have been unable to create a result that functions as expected.

My viewport frame has a camera looking downwards on a static plane upon a “map”. Simply supplying the mouse position (with absolute position accounted for) does not result in the ray hitting the expected location below. The farther away from the origin of the camera, the more inaccurate it is, ranging anywhere from a few studs away from the mouse location to hundreds of studs away, depending on the camera’s height from the map.

Depends on what are you trying to do, but do you really need to use ViewportFrame? Its pretty much be easier just to put camera above actual map.

It’s a minimap system, so yes, it is necessary.

I also wasnt able to use the camera functions amd found this workaround instead.