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. Dismiss Notice

2D Light as Field of View?

Discussion in '2D' started by fajerbujt, Sep 29, 2020.

  1. fajerbujt

    fajerbujt

    Joined:
    Feb 29, 2020
    Posts:
    14
    I'm trying to implement a standard FoV/LineOfSight feature in my 2D game using the URP 2D Renderer.

    I thought I could use the new 2D Lights to create a simple FoV so I added a Point light to the player and the effect is good enough for what I'm trying to do:

    However what's missing is that the light cone/rays should stop when it hits an obstacle (eg. the blue walls). Currently the light just lays on top of everything. Is it possible using current 2D lights to configure it so that the light wont overlay the obstacles?

    (link to gif if it doesnt show: https://i.imgur.com/G9hACRD.gif)
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    You have to add shadow casters to anything you want to block the light.
     
  3. fajerbujt

    fajerbujt

    Joined:
    Feb 29, 2020
    Posts:
    14
    I added a ShadowCaster2D to an object and it creates a shadow but it doesnt block the light:


    As you can see in the picture I still get vision behind the obstacle (the light passes through)

    The ShadowCasters bounds matches the obstacles
     
  4. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Last edited: Sep 30, 2020
  5. piotrzulawski

    piotrzulawski

    Joined:
    Aug 27, 2019
    Posts:
    3
    You need to do two things:
    1. In your PointLight2D Game Object, in the 2DLight component, increase the Shadow Intensity slider to 1 (away from 0)
    2. In your ShadowCaster2D component tick the SelfShadow box.

    Does that do the trick?