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.


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.position.

Let me know if you have any follow up questions!


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


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