How to know mesh is behind another mesh from camera?

Is there any good way to know if one mesh behind another one so we can just make the one closer to camera show?

This is a 3D math question!

You can calculate dot of the position of the object and the forward vector of the camera. Whichever is smaller is closer. (assuming it is greater than 0) Let me see if I can get a working demo just to double-check my answer.

2 Likes

Yep just as I thought!

Take a look at this example I made. You can change the positions of the spheres to see. Also for reference, the way we get the “forward vector” of the camera is taking the camera.target - camera.position.

Let me know if you have any follow up questions!

3 Likes

Thank you! Do you know why we need to take the camera.target-camera.position, what’s the different by just use camera.getDirection(Vector3.Forward())?

2 Likes

That works too! More was explaining the math behind it I guess. :smiley:

2 Likes