Best way to detect if the camera is inside a part?

I am trying to make lava, but you can see everything clear under it.
I can fix this by making a gui popup when the camera is inside of the lava, but for that i need to detect whenever the camera its position is under the lava.

Is there a way to detect if the camera its position is inside of a part?

1 Like

If your lava is a part, then you can cast a ray from the camera to the center of the lava, with only the lava part in its whitelist. If there’s a hit, then the camera must be outside. Otherwise, it must be inside.

You don’t want to cast all the time, though. Very long raycasts can be expensive. Check if there’s any lava nearby first. You can do this with a very tiny Region3 check.

4 Likes

Wouldn’t this lag a lot? I am trying to keep it as efficient as possible.

No, it wouldn’t.


How large would the region have to be?

Very tiny. A stud would be enough. If there’s no lava in the region, then there’s no need to raycast.

But the lava could be very large and also there could be a lot of parts aswell.

Yeah, I know. A tiny region is enough.

Why would you need a larger region? You’re only checking if a point (the camera’s position) is inside something.

Oh sorry, i got confused and thought of something else that i have already made…