Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by HBishop · Feb 14, 2020 at 04:50 PM · cameraviewportvisibility

GameObject and Camera visibility

Hi,

I'm trying to write a script that allows you to interact with a gameobject, but only when it's in the main camera view.

I've tried Render.IsVisible and I'm trying void OnBecomeVisible but it keeps behaving as if they are all in the camera view when they aren't

Here is the code attached to the GameObject I want to move (to the left) on a key press but only when it's in the camera view.

          public float leftSwipe;
 public static bool isSeen;
 

 // Use this for initialization
 void Start () {

    
     isSeen = false;
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown(KeyCode.Space) && isSeen==true)
     {
         
         transform.position = new Vector3(-leftSwipe, transform.position.y, transform.position.z);
     }
     
     
 }


 private void OnBecameVisible()
 {
     isSeen = true;
 }

 private void OnBecameInvisible()
 {
     isSeen = false;
 }


I'm not sure if I've completely misunderstood how the above functions work. Thanks for any help

Comment
Add comment · Show 1
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 tormentoarmagedoom · Feb 14, 2020 at 05:50 PM 0
Share

Render.IsVisible didnt work? it should work. THe object have a mesh renderer i supose

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by phosphoer · Feb 14, 2020 at 06:05 PM

OnBecameVisible() and IsVisible should work, however it may not be the exact behavior you're looking for depending on your setup, as it will include times when the object is rendered due to just its shadow being in-frame. It also will be affected by all cameras in the scene, including the scene-view camera when running in editor. I'd guess that if you aren't receiving these messages at all, your script may not be on the same object as the Renderer (The message does not propagate up the hierarchy).


Another approach you could use would be to just calculate whether the object's position is within the frustum of the camera. It's quite simple to test a single point for this, though if your object is large or oblong you may run into issues where a part of the object is visible on camera but the point you are testing is not. But for simple interaction tests this may be good enough! You could just pass Camera.main to this function with your objects position.

 public static bool IsWorldPositionInViewport(Vector3 worldPos, Camera cam)
 {
     Vector3 viewPosition = cam.WorldToViewportPoint(worldPos);
     return viewPosition.z > 0 && viewPosition.x > 0 && viewPosition.y > 0 && viewPosition.x < 1 && viewPosition.y < 1;
 }
Comment
Add comment · Show 2 · 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 HBishop · Feb 14, 2020 at 08:48 PM 0
Share

Thanks for the fast reply. It was the scene view camera, I had no idea it worked like that so thank you! Is there any way to make it so the object can only be interacted with once it's fully on the screen. Like you said, the shadow seems to be causing it to be classed as in view.

avatar image phosphoer HBishop · Feb 14, 2020 at 09:20 PM 0
Share

Unfortunately I don't know of a built-in method that specifically tests whether a renderer's geometry is within the viewport. As far as I know Unity only concerns itself with whether the renderer needs to be drawn, which will include cases that you probably want to exclude.
Another thing to consider, do you care about occlusion? e.g., if your interactable is behind a door, but the camera is looking towards it, do you need to handle that?
Either way, I think my recommendation would still be to use the code I shared and test whether the object is in the viewport manually. You could expand this to test multiple points if your object is oddly shaped and you want to support it being partially off-screen.
If occlusion matters to you, then you could additionally perform a raycast from the camera to the point that was in the viewport and see if it hit anything in-between.

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.

Follow this Question

Answers Answers and Comments

186 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 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

Unable to see proper view in viewPort 0 Answers

How to know when a object is in the viewport? 1 Answer

How to force the game to be stretched to fit the screen? 1 Answer

Activate object only if camera views it 3 Answers

Camera position in Viewport coordinates 2 Answers


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