Search Unity

Question Shadow Caster 2D shadows passing through objects

Discussion in '2D' started by Neil-Corre, Apr 26, 2023.

  1. Neil-Corre

    Neil-Corre

    Joined:
    Nov 29, 2015
    Posts:
    44
    Hi there, after a day of searching around, I'd like to ask how to block/cull the shadows from a Shadow Caster 2D component? In the attached example, the shadow should stop when it hits the object and not pass through it.

    ShadowCaster2D.gif

    Note:
    - I tried using different 2D colliders with and without Rigidbody 2D
    - Lights classes has been edited to change the shadow's length using the suggestions in this forum thread (https://forum.unity.com/threads/2d-lighting-shadows.826584/)

    Versions:
    - Unity 2022.2.15
    - URP 14.0.7 (edited as mentioned above)

    Thank you and I apologize if I missed something basic regarding the 2D lighting system.
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    There is no light in that area so it will be shaded. It is like this in real life. The only way to "stop" shadows is with light. Another light source would achieve your intended goal.

    Hope this helps :)
     
    MelvMay likes this.
  3. Neil-Corre

    Neil-Corre

    Joined:
    Nov 29, 2015
    Posts:
    44
    Hi there! Thank you for the reply. Not sure if I'm misunderstanding the 2D lighting system. But, I'll try to explain what I'm trying to achieve with a 3D scene. Here's the set up.

    - The cube is the subject that casts shadow
    - The plane receives shadow
    - The long ledge receives shadows but doesn't cast shadows

    upload_2023-5-3_17-35-12.png

    What I wanted to achieve in the 2D sample above is the letter A shadow in the image. That is, the shadow is clipped and shouldn't be casted on the ground anymore, because an object is "catching" the shadow instead of the ground. Another way to phrase this is that the length of the shadow should wrap up to the top of the blocker and not pass through it.

    But, what's happening is the letter B shadow. That is, the shadow is still passing through the object and being casted on the ground.

    I could use layers and culling masks, such that the ground don't receive shadows or the ground is lit up by light that doesn't cast shadows, but that introduces another problem - the ground where the subject is doesn't have shadows (see letter C in the image below).

    upload_2023-5-3_17-42-2.png

    I've been confused the past few days. Please let me know if this shouldn't be that trivial and I'm just complicating this problem. :confused:

    Another way to phrase what I'm trying to achieve is: currently if I have a room in top down 2D, and I place an object that casts a shadow OUTSIDE of the room, the shadow of that object is being casted INSIDE the room and not stopping on the walls of the room.
     

    Attached Files:

  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    The only thing that can "block" shadows from going through an object is to make that object a shadow caster from my testing.
     
  5. Neil-Corre

    Neil-Corre

    Joined:
    Nov 29, 2015
    Posts:
    44
    I see. I may have to change how I approach the problem. Or, use a different "smoke and mirrors" effect for our game.

    Thank you, Chris!