• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by lgmontoya · Mar 09, 2022 at 08:58 PM · rendererevent triggeringprimitivesmessages

OnBecameInvisible behaviour varies by GameObject Primitive Type

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($"{t$$anonymous$$s.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.

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Mar 10, 2022 at 09:35 AM

OnBecameInvisible and OnBecameVisible are affected by all cameras, including the sceneview camera. The object must not be visible in any camera.


ps: Also the visibility is checked against the AABB (axis aligned bounding box) of the object. So depending on the orientation the bounding box may be larger than the actual mesh, especially at the corners.

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lgmontoya · Mar 10, 2022 at 06:29 PM 0
Share

Thanks for that Bunny83. Following on your suggestion of AABB, it could be the discrepancy between the cylinder's transform.localScale and the cylinder's Renderer.bounds.size (respectively (40, 20, 40) vs (40, 40, 40)). In trying to Expand() the bounds of the Renderer to match the transform.localScale I ran into another post (where you also contributed) and it appears a collider bound cannot be set, but a mesh bound can be set: what about a renderer's bound?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing two different objects renderer colour 1 Answer

renderer.material.color.a 2 Answers

Trail renderer in a pixel art style context 0 Answers

Is mesh rendered if backface culling? 1 Answer

Multiple Materials on Particle System? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges