I have discovered that OnBecameInvisible is triggered immediately for cubes, but not for cylinders.
I have a scene with just two game objects (as per below) and OnBecameInvisible is triggered as soon as the cube is no longer visible (as expected). However, when the cylinder is no longer visible, OnBecameInvisible is not triggered right away. Instead, in order to get OnBecameInvisble to be triggered by the cylinder’s invisibility, I have to keep moving further away (in the direction that made the cylinder no longer visible)
public class CreatePrimitives : MonoBehaviour
{
void Start() {
// Create cube of certain dimesion at certain location
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = new Vector3(-40, 0, 0);
cube.transform.localScale = new Vector3(40, 40, 40);
cube.GetComponent<Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
cube.GetComponent<Renderer>().receiveShadows = false;;
cube.AddComponent<OffLimits>();
// Create cylinder of certain dimesion at certain location
GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
cylinder.transform.position = new Vector3(40, 0, 0);
cylinder.transform.localScale = new Vector3(40, 20, 40);
cylinder.GetComponent<Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
cylinder.GetComponent<Renderer>().receiveShadows = false;
cylinder.AddComponent<OffLimits>();
}
}
public class OffLimits: MonoBehaviour
{
private void OnBecameInvisible() {
Debug.Log($"{this.name} now offscreen.");
}
}
What I’d like to know is if that is expected (documented) behaviour or a glitch? My scene is as simple as possible with shadow effects turned off. I have also tinkered with all the different settings in my DirectionalLight trying to get the cylinder to be more responsive to OnBecameInvisible, but to no avail. Even tried OnBecameInvisible as a coroutine.
The same OnBecameInvisbile “delay” is also present for cylinders made with ProBuilder.
Many thanks.