# How to do draw a “visibility polygon” in 2D top-down Unity? [duplicate]

I've made a very simple 2D top down prototype with Unity. Now I want to expand the thing and add a feature where objects change when the player looks at them.

So I have to add a visibility polygon feature. I want to be able to get a list of objects (2d Colliders) which are in the Field of view of the player.

How I can do that?

I looked around and only found a tutorial which works with Physics2D.Raycast, but this seems to be only one ray as I need a field of view. Similar to the light of a flashlight.

I've drawn a sketch to illustrate my question:

In this case the list should contain the collider V1, V2 and V3 because the player can see them.

Edit:

• I experimented with the sending a "fan" of Raycasts every 0,1 ° but this solution does have some problems. The accuracy depends on how far the collider is away. Also this is not the best performing solution. And there are problems with small objects.
• As solutions like this: https://www.redblobgames.com/articles/visibility/ seems to be a lot of work just to reinvent something which is already out there I wonder if there is a built-in way in unity to help me to compute this area.
• What's wrong with doing a raycast for every object within range? – Philipp May 25 '20 at 11:59
• The objects might not be fully visible. So one raycast might be hitting the (small) collider which is before the big collider and mark the big object in the back as invisible. But in reality 99% of the collider in the back is visible to the player. – Gener4tor May 25 '20 at 12:03
• "Field of View" usually refers to the angular range of a game camera. What you're describing here is drawing a "visibility polygon". Red Blob Games has an excellent tutorial about how to compute thjs polygon, and in Unity you can build a mesh with your computed shape using the Mesh class. Can you show us how you've attempted to implement this based on your research of relevant tutorials so far, and where you're stuck and need help? – DMGregory May 25 '20 at 12:39
• The example of Red Blob Games is a good starting point how to do this. I just dont know how to work with circle colliders as the example has only rectangles in it. And I hope there is some built-in way in unity to help me computing this visibility area as Im not want to reinvent the wheel. – Gener4tor May 25 '20 at 13:09
• "Seems to be a lot of work" - welcome to game development. 😉 You might be able to find pre-made assets that do this for you, but it's not a core engine feature that you'll find "built-in". If you need help adapting the Red Blob Games tutorial to work with circle colliders, then please edit your question to ask that. (Hint: the algorithm only cares about the left and right extents of an edge from the perspective of the viewpoint. It doesn't care about the shape of the edge.) – DMGregory May 25 '20 at 13:29