Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Script for generating ShadowCaster2Ds for Tilemaps

Discussion in '2D' started by AlexVillalba, Jun 6, 2020.

  1. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    349
    Thank you for your kind words!
     
  2. Its_Gerryz

    Its_Gerryz

    Joined:
    Oct 17, 2020
    Posts:
    4
    Miracles happen i guess!
     
    AlexVillalba likes this.
  3. Ziberian

    Ziberian

    Joined:
    Jan 9, 2018
    Posts:
    66
    Hello ThundThund,

    Your shadow casters have been in my project for a long time, and now it is time for me to build my project and sadly it doesn't work. I saw above that you replied to someone saying that you have added the compiler definitions but it still doesn't work for me.

    At its current stage, I have this line while generating my world:
    Code (CSharp):
    1. ShadowCaster2DGenerator.GenerateShadowCasters();
    and it works perfectly. But when I try to build it doesn't work. I get this error when I press build:
    Code (CSharp):
    1.  'ShadowCaster2DGenerator' does not contain a definition for 'GenerateShadowCasters'
    I see you have attached a separate script, saying it is to generate it on Awake(), but that one doesn't work for me when I attach that to my "Wall" game object, which holds the tilemap for the walls. What are my options here?
     
  4. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    349
    Hi Ziberian, what problem are you finding when attaching the second script? What happens if you call ShadowCaster2DGenerator.GenerateTilemapShadowCasters() with your arguments for each tilemap?
     
  5. Ziberian

    Ziberian

    Joined:
    Jan 9, 2018
    Posts:
    66
    This seems to have done the trick, thank you very much <3:

    upload_2021-11-7_19-41-22.png
     
    AlexVillalba likes this.
  6. AlefN7

    AlefN7

    Joined:
    Feb 9, 2022
    Posts:
    6
    Hello ThundThund, how are you?

    I'm with a project here trying to accomplish things with shadows. When I attach your script to an Tilemap in my project, it says: "Can't add script behaviour ShadowCaster2DExtensions. The script class can't be abstract!" You know how to deal with this?
     
  7. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    349
    Hi AlefN7, that's a static class, it just contains extension methods. You have to add the other class, TilemapShadowCaster2D.
     
    AlefN7 likes this.
  8. maxilar20

    maxilar20

    Joined:
    Jan 16, 2022
    Posts:
    4
    I'm using two tilemaps, one for the walls which have a composite collider and a shadowcaster2D for generating shadows and a floor tilemap.
     
  9. jingnate

    jingnate

    Joined:
    Jun 1, 2022
    Posts:
    1
    It worked perfectly without much trouble! You are a lifesaver
     
    AlexVillalba likes this.
  10. Zecrah

    Zecrah

    Joined:
    Nov 1, 2016
    Posts:
    7
    Still to this day this is very helpful! Using it in my 2022.1 project successfully.

    I did have one issue where the generated Shadow Caster 2Ds weren't matching the tilemap. Turns out that it doesn't seem to accommodate a custom scale on my tilemaps. I added a quick line to 146 of ShadowCaster2DGenerator.cs to resolve this:

    Code (CSharp):
    1. newShadowCaster.transform.localScale = new Vector3(1 / collider.transform.localScale.x, 1 / collider.transform.localScale.y, 1 / collider.transform.localScale.z);
     
    Larsi_Official and AlexVillalba like this.
  11. Krythic

    Krythic

    Joined:
    May 29, 2017
    Posts:
    1
    Seems to work well. Thank you for doing this; it's pretty crazy to think that the Unity devs haven't added shadowcaster functionality to tilemaps by now. It's just such a basic thing imo. Basic as in it should have been at the top of their priority list.
     
    AlexVillalba likes this.
  12. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    349
    I can't understand that either.
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,761
    It's far from basic and everybody says everything is a "top priority" (;)) but improved shadow support was released to 2023.1 alpha and the URP 15 package that support all sorts of "providers" including support for any 2D Collider and 2D renderer as well as any custom geometry.

    There are no demos or docs available yet.

    Using trunk 2023.1 alpha and the ShadowCaster2D component, I selected the Casting Source which allows a custom shape editor, any 2D collider or any 2D renderer:

    ShadowCaster2D.png


    It's what I used when I did the follow dev videos albeit in a very early version that didn't correctly support curved colliders such as CircleCollider2D or the CapsuleCollider2D which it now does:



     
    DevPanela, nonebits and Grimeyes like this.
  14. Larsi_Official

    Larsi_Official

    Joined:
    Dec 14, 2022
    Posts:
    2
    I would like to thank you very much for this script! Without it, I would not have been able to realize my goal.

    Because I needed a little to get everything running, I have written a step by step guide:

    1. choose your tilemap from the hierarchy.
    2. if you already have "ShadowCaster2D" here, remove it. They will be created automatically later.
    3. add Composite Collider2D, but keep the Tilemap Collider 2D.
    4. a Rigidbody 2D is created, set Body Type to "Static".
    5. check "Used by Composite" in Tilemap Collider 2D.
    6. create a new C# script, paste the first code from post 1. Save it. Confirm the query.
    7. in the Unity top bar now you will find a "Tools" tab, select "Generates ShadowCasters 2D" from there.

    If your tilemap collider does not perfectly match the shape of the tiles, follow the instructions on this page: https://docs.unity3d.com/Manual/CustomPhysicsShape.html

     
    mrmagic2024 and AlexVillalba like this.
  15. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    349
    Nice, I've added your post to the OP. I don't know how to quote posts so I just put the link there.
     
    Larsi_Official likes this.
  16. drizztdourden_

    drizztdourden_

    Joined:
    May 4, 2016
    Posts:
    7
    @MelvMay Is that also part of the new 2022 LTS version by any chance?
     
  17. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,158
    Unfortunately, it isn't since URP 15 is available only in 2023.1.
     
  18. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,761
    As I said, 2023.1 alpha onwards. :)

    New features are never backported in Unity.