- Home /
Show and hide object according to camera rotation.y position
I have a Main Camera rotating at 0,0,0 translate and I'm trying to make an object visible a few seconds after the camera gets it's visible field.
I have tried gameObject.active and renderer.enabled with no sucess.
function Update() {
if (Camera.main.transform.rotation.y <= 80 && Camera.main.transform.rotation.y >= 45) {
renderer.enabled = true;
}
else {
renderer.enabled = false;
}
}
function Update() {
if (Camera.main.transform.rotation.y <= 80 && Camera.main.transform.rotation.y >= 45) {
gameObject.active = true;
}
else {
gameObject.active = false;
}
}
None of them work. The object don't appears at camera.y 80 position. The code is attached only to the game object I want to show and hide. Thanks.
@Zefaistos Forgot to mention, you can use OnBecameVisible/OnBecameInvisible functions to deter$$anonymous$$e if the object is being viewed by any camera. You can then make a visible boolean true and false or just make the objects renderer true or false.
The OnBecameVisible function will always be true if the editors scene view camera is viewing the object. Just add these function to the script on the object/objects in question. Hope that helps.
The problem is I don't want the object being visible all the time by the camera when it's looking at that point.
I'll explain exactly what I want:
The camera is rotating only on y axis, and sometime the camera will show some stars representing a constelation (scorpion constelation). After a few seconds that the camera shows the stars of the constelation, I want to display the object behind the stars, with a scorpion draw, fitting the art with the stars. And I want to hide again the scorpion art object (a cube with transparent texture) before the camera goes out of the stars of the constelation.
Answer by Zefaistos · Mar 31, 2012 at 06:02 PM
I'm new to Unity and don't know how to use Frustum Planes. What I want to do is not so relevant, so I decided to use StartCoroutine, hiding and showing the object every few seconds.
The camera.rotation.y on panel is that value, but it's indeed wrong. I have tested with a object attached to the camera, and the camera.y changes from > 0 and < 0, but never > 1.
Thanks for the answer. I'll try Frustum Planes later anyway.
Your answer
![](https://koobas.hobune.stream/wayback/20220613065732im_/https://answers.unity.com/themes/thub/images/avi.jpg)