Search Unity

Field Of View 2D Based On Ray Tracing | Free Package

Discussion in 'Assets and Asset Store' started by Dkury, Feb 7, 2013.

  1. Dkury

    Dkury

    Joined:
    Jul 14, 2012
    Posts:
    46
    Field Of View 2D allows to create visually represented fields of view for AI units.



    An approach for this 2D field of view based on ray tracing and may not be efficient if there will be more than 10 instances on a screen at once. But it may be ideal for small games and prototypes.



    Look at:
    Sample
    FOV2D with “AI” and Mecanim

    Download Package

    PS
    If you have some questions or need a developer for hire write me on dima.kurilchenko@gmail.com or skype: dima.kurilchenko
     
    Last edited: Feb 13, 2013
    Tien-Tran and negreiros_jorge like this.
  2. MaDDoX

    MaDDoX

    Joined:
    Nov 10, 2009
    Posts:
    764
    Looks cool, generous offer! :)
     
  3. Toad

    Toad

    Joined:
    Aug 14, 2010
    Posts:
    298
    Looks great! Very generous too, as Maddox says. Thanks!
     
  4. Yusuf-AKDAG

    Yusuf-AKDAG

    Joined:
    May 13, 2009
    Posts:
    280
    This is very good lookin.
    Can I use it on commercial component?
     
  5. Dkury

    Dkury

    Joined:
    Jul 14, 2012
    Posts:
    46
    Thank you all! I hope this package would be helpful at some point in your projects.

    Yes, you are welcome to use it on a commercial component and feel free to modify it as you like.
     
  6. DanRaine

    DanRaine

    Joined:
    Jun 30, 2006
    Posts:
    7
    That's great,

    Much appreciated

    Dan
     
  7. Dkury

    Dkury

    Joined:
    Jul 14, 2012
    Posts:
    46
    You are welcome, Dan.

    Actually, if someone needs to implement this, you know, make it more efficient or some customization, you can always contact me:
    dima.kurilchenko@gmail.com
    skype: dima.kurilchenko
    I am working as a freelance developer and looking for interesting projects.
     
    Last edited: Aug 2, 2013
  8. Yusuf-AKDAG

    Yusuf-AKDAG

    Joined:
    May 13, 2009
    Posts:
    280
  9. Dkury

    Dkury

    Joined:
    Jul 14, 2012
    Posts:
    46
  10. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Thanks a lot fo this! I will certainly use it in my prototype
    Kudos!
     
  11. RCraig

    RCraig

    Joined:
    Jul 10, 2012
    Posts:
    9
    Excellent work! Exactly what I needed to test a prototype I'm working on.

    One (hopefully small) question: If I set the FOV Angle to 360, it leaves one segment 'open' as if it didn't join the first and last tri to create the final one. Is this an easy edit? Please forgive the newbie-ness.

    Thanks again!
     
  12. RCraig

    RCraig

    Joined:
    Jul 10, 2012
    Posts:
    9
    Sorry to bother with such a simple question. After a few minutes of fiddling I figured it out. Just needed to add an extra ray.

    For others, to get a 360 mesh with the last missing triangle, change the following in FOV2DEyes.cs in the CastRays function:

    to


    Thanks again!
     
  13. rattlesnake

    rattlesnake

    Joined:
    Jul 18, 2013
    Posts:
    138
    Thanks again for this awesome Script Dima !

    I would like to ask you a question.

    Currently the angle of view is like that (360 degres on a plane) :



    Is it possible to easily change the angle of the rays in order to project them like a cone ? like that :



    Thanks in advance for your answer :)
     
  14. Dkury

    Dkury

    Joined:
    Jul 14, 2012
    Posts:
    46
    In order to change the angle of the rays and project them as a cone you can do these:

    1) Find a CastRays method in a FOV2DEyes class.

    2) Replace following:
    Code (csharp):
    1.  
    2. direction = Quaternion.AngleAxis(currentAngle, transform.up) * transform.forward;
    3.  
    with
    Code (csharp):
    1.  
    2. Vector3 lift = Vector3.up * 0.75f;         
    3. direction = Quaternion.AngleAxis(currentAngle, transform.up) * transform.forward + lift;
    4.  
    At this point you will have a cone like this:
    $lSjxX2C.png
    One problem is that the mesh doesn't has polygons at the bottom in the current FOV2D thing.
    $WbWp8Rr.png

    3) In order to make a two sided mesh you should make changes in a UpdateMesh method in a FOV2DVisionCone class. Basically it means that there would be 2x more vertices (and faces) where a second half would represent a bottom. Remeber: a clockwise order goes for upper faces and a counter-clockwise goes for back faces.
     
    Last edited: Aug 13, 2013
  15. rattlesnake

    rattlesnake

    Joined:
    Jul 18, 2013
    Posts:
    138
    Thanks Dima !

    It works perfectly ! you're awesome :D
     
  16. Brum

    Brum

    Joined:
    Mar 20, 2013
    Posts:
    11
    Hey man! this is amazing! Thanks a lot! I've wanted this for my whole live ;P
     
  17. cyberlarry

    cyberlarry

    Joined:
    May 14, 2014
    Posts:
    12
    Are there any similar for unity 2D? Thanks
     
  18. negreiros_jorge

    negreiros_jorge

    Joined:
    Feb 7, 2013
    Posts:
    5



    HI Dima Kurilchenko ... this package is very usefull.
    I this reference to make a 3dFOV. Ref-3dEyes.png
     
    Dkury likes this.
  19. Dkury

    Dkury

    Joined:
    Jul 14, 2012
    Posts:
    46
    Great! How does it work?
     
  20. MarcGames

    MarcGames

    Joined:
    Dec 14, 2014
    Posts:
    1
    Woah, thanks you man, you're great!
     
  21. ToastedJustice

    ToastedJustice

    Joined:
    Dec 1, 2014
    Posts:
    1
    Is there any way to implement this package into Unity2D, i.e. use the xy plane instead of xz.

    Thanks!
     
  22. Cryothic

    Cryothic

    Joined:
    Dec 4, 2013
    Posts:
    6
    I'm trying to use this in a 2D projects (XY).

    So far, it seems to work if you change this in FOV2DEyes.cs (line 36):
    Code (csharp):
    1. direction = Quaternion.AngleAxis(currentAngle, transform.up) * transform.forward;
    to:
    Code (csharp):
    1. direction = Quaternion.AngleAxis(currentAngle, transform.forward) * transform.up;
    The view-cone turns upwards in this case.
    Only thing I haven't got working at this point, is "hitting" a BoxCollider2D.
    A regular (3D) BoxCollider seems to work though.
     
  23. Rtyper

    Rtyper

    Joined:
    Aug 7, 2010
    Posts:
    452
    This works really well, thanks!

    How would you go about making the field of view adjust to the slope of the current surface? I have enemies that walk up slopes, and ideally they'd be able to look up the ramp ahead of them xD
    I've tried casting a ray down before the for-loop in FOV2DEyes.cs to get the normal of the surface, but I'm not all too sure how to take that into account with the other raycasts.
     
  24. JacobDzwinel

    JacobDzwinel

    Joined:
    Dec 19, 2013
    Posts:
    26
    Awesome asset! I was looking for something like this.
    How hard would it be to make script works also with different height variations. Consider map from Commandos Game where enemies can also see what's under&above their view


    I was thinking about it for some days but I can't find any good solution how I can achieve this type of LOD drawing.

    More examples:
    Clipboard01.jpg
     
  25. NinjTsax

    NinjTsax

    Joined:
    Mar 10, 2015
    Posts:
    2
    Hello,
    can i use this with sprites? i tried to put the right collider but im using polygon and light goes trhow it... the object dodnt stop the light
     
  26. ashley

    ashley

    Joined:
    Nov 5, 2011
    Posts:
    84
    Did you ever get it to work with BoxCollider2D? Currently having the same problem.

    Trying to convert the script into a 2D Physics one and on the right path but still having problems. The cone of vision isn't working quite the same (ie the same angle, distance etc settings are producing different results), but I can get it to recognise 2D colliders! The difference is probably because I've only changed the Eyes script and not the vision cone one though!
     
    Last edited: Oct 11, 2015
  27. willparsons

    willparsons

    Joined:
    Aug 3, 2015
    Posts:
    1
    If you want it to work with 2D colliders (and this method ONLY allows 2D colliders) then you'll have to modify the scripts a bit.

    1)
    FOV2DEyes.cs (Line : 14)
    Change the hit list to use RaycastHit2D

    2)
    FOV2DEyes.cs (Line : 62ish)
    Remove the raycast if statement and replace with:
    RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, fovMaxDistance, cullingMask);

    Then write:
    if(hit == false)
    {
    hit.point = transform.position + (direction * foxMaxDistance);
    }

    3)
    FOV2DVisionCone.cs (Line : 32)
    Change the hit list to use RaycastHit2D

    ----------------------------------------------

    That should do the trick, however you will need to rotate the FOV2D prefab depending on your camera angle.

    I'll upload a reference just in case (it's a bit different in terms of other functionality but you'll be able to understand the idea of it).
     

    Attached Files:

    ashley likes this.
  28. ashley

    ashley

    Joined:
    Nov 5, 2011
    Posts:
    84
    Thanks very much for that, it's really useful!

    Now just trying to get my head around getting it to follow the parent's rotation. I get its because the direction is being set within the script, but trying to figure out how to make it inherit the parent's direction. Might be a bit too much for my brain on Sunday!

    Thanks again :)
     
  29. Zace

    Zace

    Joined:
    Jan 3, 2013
    Posts:
    2
    Hey!

    This code is great? Have you though about uploading this to the asset store?
     
  30. humpbackgames

    humpbackgames

    Joined:
    Dec 21, 2016
    Posts:
    2
    Has the package been deleted?