Help with making item invisible if not on screen

Ok so you read the title, this is my code so far:

function OnBecameInvisible() {
    renderer.enabled = false;
}

So I tried OnBecameVisible but that doesn’t work if the object is invisible or renderer is disabled. I thought a Raycast might work but was wondering if there was another better way to do it. Thanks!

1 Answer

1

Read the section about clip planes. Sounds like you’re trying to do something called Frustum culling, which in most cases happens automatically.

would it be beneficial then?

You can technically calculate the angle between the vector to the object and the camera and if it's greater then the view angle, disable it's renderer. However, as MrLeap already said, this is something that Unity does automatically for you. You can test against renderer.isVisible to enable/disable scripts etc. for performance though, which can certainly be useful for optimization purposes.