Mouse.Hit Inside ViewPort Frames?

I want to get a 3d world position of the mouse, Inside of a viewport frame.


print(mouse.Target.Name)
(Printed SpawnLocation which is the object through the backpack.)

Converting from Vector 2 ignores Depth.

2 Likes

Not going to work neither will raycasts.

Physics are not simulated in viewport frames at all, meaning rays can not be casted, which also means mouse.hit can not be used considering it uses rays.


Options?

The are a couple options at the moment, while we wait for this (hopefully) upcoming functionally with world models which will (supposedly) allow us to raycast in viewport frames.

Option1:

Is to use actual ray casting but not actually inside the viewport frame but instead project it inside the workspace where it can be simulated, similar to what Onogork has done with his viewport module:

Option 2:
Is to use gui buttons and kind of and try to map where the user can click and stuff

Option3: (My favorite)

is to theoretically create custom raycasts (sort of)!, ideally you would first convert the mouse position to world position (Not Screen/Viewport to world point that uses unit raycasts which if i recall correctly they also don’t work for vpf) Then you would need to actually do some "raycasting"which would need to start from the converted mouse’s position and provided with a direction, perhaps for this you could use @IdiomicLanguage’s custom GJK and Distance module.


if i have some time i would gladly try and compile a mocked-base script for this.

4 Likes

Thank you! Option 1 seems like the best solution for my scenario because I plan on having gravity affect the objects.

Even though i know you went for option1 but I thought i’d go ahead and try to do option 3 anyways while we wait for…world models (honestly just for my self enjoyment) or if someone wanted to know if it “could be done”. The way i choose to it was essentially the exact same thing i said above, Convert to world coordinates and then utilize @IdiomicLanguage’s Module to “raycast” in the direction in relation to the viewing frustum. It works pretty well and i believe i did all the math correctly (yay!..hopefully), However i think it could be optimized especially, well specifically, my wedge support function and possibly my ScreenToWorld Conversion function. The only true limitation with this is that if you don’t have a part that is something like a Sphere or Wedge or a normal part, i.e something that isnt easy to dynamically calculate it’s Support your going to have to add it in manually by providing it’s vertices (which isn’t too hard mind you, but it is a downside).

(Result:, sorry for bad quality, oh and btw the rotate model thing is from this post: How to make camera rotate around model in ViewPort Frame while mantaining set distance and angles - #8 by EgoMoose )

Also the reset button is just to reset the colors…

For anyone wondering I’ve tried this in a while loop and a renderstepped and it seems to run at a stable 60-59fps for me.

finally just in case anyone wants the place file:
ViewportFrameRayCasting.rbxl (38.7 KB)

6 Likes

Awesome! I actually implemented this and resolved a bit of an issue. I delete parts from my viewport frame, but the module script saves the parts that I’d already deleted. I fixed this by just removing parts from the AllParts table when I removed parts from the viewport frame.

1 Like