Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Check if gameobject in visible on screen

Discussion in 'Scripting' started by inthemilkywat, Aug 7, 2016.

  1. inthemilkywat

    inthemilkywat

    Joined:
    Dec 29, 2013
    Posts:
    56
    I'm currently using this code to check if a gameobject is visible on the screen. But my problem is that it only returns true if the center of the object is on the screen. So if only a small portion of it is visible, it won't return true. Is there any way I can check for that?

    Code (CSharp):
    1. Vector3 screenPoint = Camera.main.WorldToViewportPoint(_pooledGameObjects[i].transform.position);
    2. bool onScreen = screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1;
     
  2. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
  3. dmoney

    dmoney

    Joined:
    Oct 6, 2014
    Posts:
    15
    if(GetComponent<Renderer>().isVisible){
     
    Ktuuurnz, Powzone, Sammonius and 4 others like this.
  4. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Keeping with your code you'd need to change your 0's and 1's to something covering a wider range based on the dimensions of the object.
     
  5. eliteforcevn

    eliteforcevn

    Joined:
    Oct 25, 2018
    Posts:
    47
    solution :3

    Code (CSharp):
    1.         Vector3 screenCenter = new Vector3(Screen.width / 2, Screen.height / 2, Camera.main.transform.position.z);
    2.         Debug.Log("screenCenter "+screenCenter);
    3.  
    4.         Vector3 screenHeight = new Vector3(Screen.width / 2, Screen.height, Camera.main.transform.position.z);
    5.         Debug.Log("screenHeight " + screenHeight);
    6.  
    7.         Vector3 screenWidth = new Vector3(Screen.width, Screen.height/2, Camera.main.transform.position.z);
    8.         Debug.Log("screenWidth " + screenWidth);
    9.  
    10.         Vector3 goscreen = Camera.main.WorldToScreenPoint(transform.position);
    11.         Debug.Log("GoPos " + goscreen);
    12.  
    13.         float distX = Vector3.Distance(new Vector3(Screen.width / 2, 0f, 0f), new Vector3(goscreen.x, 0f,0f));
    14.         Debug.Log("distX " + distX);
    15.  
    16.         float distY = Vector3.Distance(new Vector3(0f, Screen.height / 2, 0f), new Vector3(0f, goscreen.y, 0f));
    17.         Debug.Log("distY " + distY);
    18.  
    19.         if(distX > Screen.width / 2 || distY > Screen.height / 2)
    20.         {
    21.             Destroy(gameObject);
    22.         }
     
    perdy, olgaspirols, ZoraMikau and 2 others like this.
  6. Kaldrin

    Kaldrin

    Joined:
    Jul 10, 2018
    Posts:
    42
    Thank you very much this solved my issue!
     
  7. makled

    makled

    Joined:
    May 28, 2017
    Posts:
    14
    I have used a different approach that I would like to share. Here is what I wanted to do

    I want to know if the object is actually visible in the screen. Checking if it is within the camera frustum or in the center view of the camera won't work for me. I had to check based on the user's screen resolution.

    I found some problems with the earlier proposed solutions. For starters the OnBecomeVisible will return true if the camera is in 180 degrees of the object. Therefore, the condition "isObjectVisible" returned true when the camera was looking at the object and when the camera gave its back to the object. Not just that, it would sometimes return true even when the object hasn't rendered yet. So instead I did the following.

    1. Get the object position on the Screen
    2. check if that position is within the user's resolution

    Code (CSharp):
    1. screenPos = m_camera.WorldToScreenPoint(transform.position);
    2. onScreen = screenPos.x > 0f && screenPos.x < Screen.width && screenPos.y > 0f && screenPos.y < Screen.height;
    However I got the exact same problem. OnScreen was true when the camera was facing the object and when the camera was giving its back to the object. So I did a double check with the Renderer component.

    Code (CSharp):
    1.  
    2.         void Start()
    3.         {
    4.             m_renderer = GetComponent<Renderer>();
    5.             m_camera = FindObjectOfType<Camera>();
    6.         }
    7.         public void CheckVisibility()
    8.         {
    9.             //Check Visibility
    10.  
    11.             screenPos = m_camera.WorldToScreenPoint(transform.position);
    12.             onScreen = screenPos.x > 0f && screenPos.x < Screen.width && screenPos.y > 0f && screenPos.y < Screen.height;
    13.  
    14.             if (onScreen && m_renderer.isVisible)
    15.             {
    16.                 //Visible
    17.             }
    18.             else
    19.             {
    20.                 //NotVisible
    21.             }
    22.         }
    23.  
     
  8. Kaldrin

    Kaldrin

    Joined:
    Jul 10, 2018
    Posts:
    42
    This is clever, thanks for sharing, however, I wonder, if the object is large it may partially appear on screen but as its center is not in the screen it might return false?
     
    Oknaa likes this.
  9. SassyPantsy

    SassyPantsy

    Joined:
    May 17, 2020
    Posts:
    136
    Thanks! Had the same issue, where the check based on the screen res would return true when the object was behind the camera. Drove me insane. This seems to do the trick