I try to calculate if a GameObject is in Field of View of my Camera.
Edit: Becouse if not i want to disable the MeshRenderer of the GameObject.
Is there a buld in function i didnt find till now? Right now im using this code snippet which looks to work right. But im not sure.
Vector3 cameraRelative = _camTransform.InverseTransformPoint(gO.transform.position);
Vector3 screenPos = _cam.WorldToScreenPoint(gO.transform.position);
if (screenPos.x > 0 && screenPos.x < Screen.width && screenPos.y > 0 && screenPos.y < Screen.height && cameraRelative.z > 0)
// The object is in front of the camera
_inView = true;
else
// The object is behind the camera
_inView = false;
Perfect! Thank you!
– anon51874571I cheered to fast. I have a little problem. After i disable the rendere. I, not able to Enable it again, because after that renderer.isVisible is always false.
– anon51874571Why do you need to disable the renderer if it's out of view again? it's not being rendered
– anon85704231Ahhh! If the renderer is not vissible it will not be rendered at all? So i can use isVisible only for my calculations? I testet it and it look that it works in this way!
– anon51874571Nice if(AI_Man_Mesh.renderer.isVisible){ Debug.Log("I can see a you mr AI man!!"); } Handy!
– superme2012