Light, easy and fast Field of View 2D

So I was working on a personal project that requires some AI to have a field of view to check if the player has been seen or not by the AI.

I searched on the asset store and to my surprise there is nothing like this implemented nor in the internet, so I decided to quickly implement a fast, light and small version of a small field of view for 2D.

My implementation doesn’t use physics at all, without further ado, here’s a quick explanation of the component; to use it just drop it on a GameObject and it will run automagically.

Inspector vars
Radius: Radius of the field of view, how far it can “see”.
FOV (Field of view): Angle in degrees how wide the field of view will be, by default 90 degrees
Direction: Defines the direction where the field of view will be pointing at, this vector internally gets normalized.
Test Point: This is a transform reference to test the field of view when hitting play.

And this is how the field of view will be seen in the scene view; the green line shows the direction and the yellow lines determine the FOV edges.

FOV Scene view


Finally the source code can be found here, feel free to use it in your commercial projects and also include it in your Asset store packages if needed, still you don’t have permission to sell this script alone in the Asset store.


6 Responses to “Light, easy and fast Field of View 2D”

  1. Juan Sebastian says:

    Hello MaximilianPs,

    for 3D should work the same but now you will need a volume. I cant seem to make your code work straight away. if you have a small test case I can look at it just by copy pasting a gist I might be able to help out 😉

  2. MaximilianPs says:

    Hello there,
    I’m trying to convert your code in 3D to use it with my mobs, but I’m stuck on a point…. when the testPoint will not be null, and How ? 😀

    also, I’ve some problem with the gizmo.
    The arc indeed change when the gameObject rotate on Y axis.
    Can you help me?

    here is the code.

  3. kamal says:

    I tried it in 3d fov top-down view where i want both enemy and player to use same type of fov. Can you give me link for script or script in C#?

  4. Juan Sebastian says:

    How did you tried to move it, its been long since I posted about this hehe, but if I remember well, it should move by moving the TestPoint.

    try it out, if it doesnt work mail me I can send you the version I’m using 😉

  5. Ashley says:

    This is the kind of thing I’m looking for, but I’d like to attach it to moving enemies. Tried to play around with my limited knowledge but not having much luck.