x


Find out if GameObject is in Field of View

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;
more ▼

asked Jun 29 '10 at 10:35 AM

Case23 gravatar image

Case23
441 17 21 30

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You can just use this:

bool visible = renderer.isVisible
more ▼

answered Jun 29 '10 at 11:05 AM

Mike 3 gravatar image

Mike 3
30.5k 10 65 252

Perfect! Thank you!

Jun 29 '10 at 11:39 AM Case23

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

Jun 29 '10 at 12:08 PM Case23

Why do you need to disable the renderer if it's out of view again? it's not being rendered

Jun 29 '10 at 12:10 PM Mike 3

Ahhh! 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!

Jun 29 '10 at 12:15 PM Case23
(comments are locked)
10|3000 characters needed characters left

There's an option to stop animations if the object is not in view

http://unity3d.com/support/documentation/ScriptReference/Animation-animateOnlyIfVisible.html

more ▼

answered Jun 29 '10 at 10:41 AM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

Sorry I sayd the wrong thing. I dont use the Animation Class from Unity, instead i using the iTween library. Also i want to disable the MeshRenderer on the GameObjects not in view.

Jun 29 '10 at 10:50 AM Case23
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2981
x59
x21

asked: Jun 29 '10 at 10:35 AM

Seen: 4589 times

Last Updated: Jun 29 '10 at 10:51 AM