- Home /
Check if a camera is looking at an invisible object
I know how to check if the renderer is visible, but I want to know if a camera is looking at an object WITHOUT a renderer enabled. I don't want an object to enable rendering when a camera is looking in its direction. But when I use
if (boolName && !renderer.isVisible) { renderer.enabled = true; }
it renders anyway because renderer.enabled
is false when that code is executed. This can be achieved by complex raycasting, but I don't know how to do that and I'd rather not add 50 extra lines of code. Thanks.
"This can be achieved by comlex raycasting", isVisible
doesn't do raycasting, it simply checks whether the object is in the viewport volume. What do you want to achieve with this? There might be a better solution to your problem (such as raycasting)
isVisible
doesn't work when the renderer's disabled, right? STEP 1: The monster object takes a moment to disappear when the player looks at him (timer -> renderer.enabled = false;
). STEP 2: The monster waits a few moments before reappearing (timer2 -> renderer.enabled = true;
).
I don't want STEP 2 to happen if the monster would suddenly appear in the viewport. I would like to know how to achieve this.
I would use WorldToScreenPoint
and check if it's in the bounds of the camera...
Answer by Benproductions1 · Jan 06, 2014 at 11:57 PM
Hello,
To check whether a point can be seen by a camera you can check whether that point, transformed using WorldToScreenPoint
has a x
value between the range of 0
and the screen width (`Screen.width`), a y
value in the range 0
to the screen height and a positive z
value.
Hope this helps,
Benproductions1
PS: If you don't know how to code that, I suggest you learn coding before you try something like this ;)
Thanks. I do know how to code that. I learn best by doing.
Your answer
Follow this Question
Related Questions
Camera raycast and rotated collider 0 Answers
Make the terrain ignore Raycast if in between Camera and Player? 1 Answer
Raycast Object Selection 3 Answers
Help with Raycast on Render Texture 0 Answers