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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Renderer.isVisible performance hit ?

Discussion in 'General Graphics' started by originalterrox, Jul 20, 2015.

  1. originalterrox

    originalterrox

    Joined:
    Feb 6, 2015
    Posts:
    40
    I use WorldToViewportPoint to disable some function calls for my AI when they are about 50% away from the screen, but it seems slower to check Renderer.isVisible for each object before WorldToViewportPoint than it does to just do WorldToViewportPoint on every object.

    I assumed that Renderer.isVisible would be a quicker way to check if something is offscreen than using WorldToViewportPoint and some math but my tests in Editor with about 30 objects doesn't match my assumptions.

    Looking up Renderer.isVisible seems to show that it uses CalculateFrustumPlanes and I guess that is slower than WorldToViewportPoint? So even if I had a million objects to check, isVisible wont be any help?

    Is there a faster way to detect on onscreen object?

    my game is 2D, Unity 4, multiple cameras sometimes.
     
  2. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,887
    Can you just use a collision trigger sphere around the player? Enemies that touch it can use their function calls.
     
  3. originalterrox

    originalterrox

    Joined:
    Feb 6, 2015
    Posts:
    40
    I guess I could use a huge one, but is that going to be faster than WorldToViewportPoint ?
     
  4. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,887
    Calling WorldToViewportPoint will be slower because it's checking every frame. With a trigger, the OnTriggerEnter/Exit event will only occur once when the enemy touches it.
     
  5. originalterrox

    originalterrox

    Joined:
    Feb 6, 2015
    Posts:
    40
    my WorldToViewportPoint is only 5 times a second, but would still be slower than trigger checks I guess because of checking every single object. I assume trigger checking is highly optimised so I will use that and redo some code ;)