- Home /
Render.IsVisible always show visible even when off camera.
Hi!
This is my code, it is not working yet. I'm trying to tell if the children of the component of the children is visible or not but the Renderer always tells me that it is visible in the Debug.Log.
I also check if it was looking at the right Renderer which he seems to be doing.
In the end I want to run code when the children of the gameobject became invisible. If you have a better idea how to let me know !
Thanks and have a nice day! :)
public class teleport2 : MonoBehaviour {
public Renderer mesh;
// Use this for initialization
void Start () {
mesh = GetComponentInChildren<Renderer>();
}
// Update is called once per frame
void Update () {
Debug.Log(mesh.isVisible);
Debug.Log(mesh.name);
}
private void OnBecameInvisible()
{
var cam = Camera.main;
var viewportPosition = cam.WorldToViewportPoint(transform.position);
var newPosition = transform.position;
if (viewportPosition.x >1 || viewportPosition.x < 0)
{
newPosition.x = -newPosition.x;
}
if (viewportPosition.y > 1 || viewportPosition.y < 0)
{
newPosition.y = -newPosition.y;
}
transform.position = newPosition;
}
}
Answer by HenryStrattonFW · May 30, 2017 at 06:10 PM
One thing to keep in mind here is that the scene view is still rendering an object through a camera, which will cause the isVisible property to return true if it is considered visible in any game or scene view.
Try closing your scene view or full-screen your game view to test, if this fixes the problem then you just have to make sure that when you test, either deactivate your scene view or move the scene view camera.
Wow. Good to know. I've never realized this before. Thanks @HenryStrattonFW...over 2 years later!
Objects are also 'visible' by shadow casting lights, right?