- Home /
GEOMETRYUTILITY.TESTPLANESAABB not working
Hi,
I'm trying to make make a FPS where there is a tag on the top of a enemy indicating where it is (similar to the objective/follow tag in battlefield/COD). It's is a 3D position (top of the enemy) that's been projected into 2D space using camera.WorldToScreenPoint
, which is then rendered in 2D UI camera.
There is one problem however, is that when my camera is completely back-facing the tag it still gets rendered, after some debugging, my assumption is that the camera.WorldToScreenPoint
will project a normal and penetrate the camera so no matter the angle of the camera it will get calculated in the screen even it's at the back of the camera.
I then came up with an idea that, I just simple test if the tag is inside the camera frustum, if not, the UI will simply not render, that should solve the problem.
During my research I found that the method GEOMETRYUTILITY.TESTPLANESAABB
could test if an object or bounding box is inside (or intersects with) the frustum planes or not. I have then took the code sample from THIS LINK and interfaced with my scripts.
However, when it actually runs it gives me completely wrong result. Saying object detected no matter what, even that object is completely outside the frustum. Here is the screenshot.
I can't figure out what's going on. Did I miss something? Please Help.
Answer by Bunny83 · May 17, 2015 at 11:33 AM
camera.WorldToScreenPoint just projects the 3d position onto a 2d plane. The projection of course happens both ways, no matter if it's before the camera or behind. You might want to check the returned z value of the projected point as it contains the signed distance from the camera. That means if the z value is positive the object is before the screen plane and if it's negative it's behind.
If you use the example code as it is, it's abvious why it's not working ^^. The example code calculates the cameras frustum planes only once in start. So moving the camera afterward doesn't change the planes according to the new camera position / orientation.
Your answer
Follow this Question
Related Questions
Camera Zoom When Camera Touches Geometry 2 Answers
3rd Person Cam Collides when it shouldn't 1 Answer
Camera looking through objects when touching 1 Answer
Camera Question 0 Answers
Know if gameObject is being seen 2 Answers