Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Is target in view frustum?

Discussion in 'Editor & General Support' started by Grady Lorenzo, Apr 18, 2011.

  1. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    This one's beyond me. I could do physical triggers, that is a feasible alternative, but it would interest me to know how to create a boolean that is determined by whether or not it's a given GO is in the camera's view frustum. Useful for a heat seeking missile, as heat seekers "give up" if it's target has been lost.
     
  2. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    In addition there is also the possibility to project their position with the cameras WorldToScreenPoint function and look if the point is inside the 0,0,Screen.width,Screen.height rect (for example if you need that for click interaction or an overlay info screen anyway)
     
    FlightOfOne, Sewmina7 and Westland like this.
  4. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    Gotta love quick replies :D

    Now isVisible is basically if the object is visible in any camera. Can I get it to decide if it is visible in just it's own camera?
     
    Last edited: Apr 18, 2011
  5. Sycle

    Sycle

    Joined:
    Nov 24, 2009
    Posts:
    446
    To check against a specific camera, I'd use dreamora's suggestion of WorldToScreenPoint.

    Check if the resulting X and Y coordinates are between 0 and screenPos.x and screenPos.y and if they are, the target is 'on screen' for that camera.
     
    samana1407 likes this.
  6. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    Would do, but I devised a much better system for making my missiles lock on.
     
  7. Mingo

    Mingo

    Joined:
    Nov 14, 2010
    Posts:
    39
    Just a quick note on this:

    1) The renderer flags things as "visible" if they're casting dynamic shadows regardless of if they're actually on-screen, which means you can't use this check. I assume this is because it has to render them if they're within dynamic shadow range in case they're casting shadows on something that is actually visible. This doesn't seem very efficient, as it means you could have a load of meshes being animated and rendered even if they're behind the camera or a wall and not casting shadows on anything that's visible.

    2) WorldToScreenPoint will return the same value if you're facing directly away from the object as it does if you're facing it, so it can't be used for a visibility check unless I'm doing something wrong.

    3) You really don't need this (or cameras) to check if a missile can "see" something. You could instead get the dot product of the missile's forward direction against the vector to it's target. That would tell you if the target was in front of or behind the missile.

    If you're using dynamic shadows and want to check if something is visible, you could use something like this:

    Code (csharp):
    1.  
    2. public bool IsVisibleFrom(Renderer renderer, Camera camera)
    3.     {
    4.         Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
    5.         return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
    6.     }
    7.  
    This will return true if the renderer is within the camera's view frustrum. You would then have to raycast or something to check for occlusion. This seems like a terrible hack, so please correct me if there's a better (tested) way of checking object visibility when that object is casting dynamic shadows.
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The Z element is positive if the object is in front of you, and negative if it's behind you. So the value isn't the same, and indeed it works well as a visibility check (I've used this).

    --Eric
     
    SergMac, Alverik, Graph and 2 others like this.
  9. Mingo

    Mingo

    Joined:
    Nov 14, 2010
    Posts:
    39
    Ha, thanks for that, I didn't even look at what it was returning in Z. That should work fine then, although checking the bounds of the renderer against the view frustrum might be more useful for larger objects, unless you just need to know if the object's center is within the screen.
     
  10. Sewmina7

    Sewmina7

    Joined:
    Apr 17, 2018
    Posts:
    6
    That's big brain move, gotta test this now! <3 <3