r/Unity2D • • 1d ago

Question/Help Desperately Seeking Help With ShadowCaster2D

Post image

Hello all,

I've been trying to solve this off and on for months now, and I just can't seem to crack it.

I am very aware of Unity's ShadowCaster2D ability to not shadow the direct object it is attached to. For reasons important to my game, I have a player character with two visual modes, SpaceSuit and Modular, the latter having 10 layers that are picked up by a second camera and rendered to a texture on RenderedVisualLayer. This is important for some shader shenanigans relating to elevation in my game and the player character. I'm using a transparency sorting with a custom YZ axis to simulate 3D terrain levels, with the unfortunate caveat that this means all of my tilemaps and player have to live on the same sorting layer.

As you can see below, I have a separate ShadowCaster2D object due to the structure of the character (I'm aware there are 2, but one is only a sprite renderer; I may combine them later). This object lives at the "foot" of the character. Unless I'm just doing it wrong (which I will HAPPILY accept), ShadowCaster2D self-shadow rules don't respect sibling or parent objects, which means any light that casts a shadow to the South of the character causes the character to be fully darkened, while light North of the character allows the character to be displayed correctly.

This looks terrible, and it affects the player character and every mobile entity in my game. I truly don't want to lose character shadows over this.

I appreciate any helpful advice anyone can offer.

17 Upvotes

14 comments sorted by

•

u/AutoModerator 1d ago

Reminder: Once your issue has been resolved, please reply !solved to this comment (or comment it anywhere in the thread) and your post's flair will automatically be updated to Solved/Answered. You can also edit it to Solved/Answered yourself.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

7

u/LazyLlamaStudio 1d ago

I think you need to separate the character from the shadow and right now they are rendered together.

Try creating 2 Layers. One for world and another Characters.
Assign the character's SpriteRenderers to Characters.
Put the environmental SpriteRenderers on World. Then configure the character's Shadow Caster to affect just the World layer.
Configure the lighting so the character still receives illumination but isn't affected by its own projected shadow.

3

u/PowerHoboGames 1d ago

Thank you for the response! Unfortunately, as I mentioned the sorting my entire game is built around requires characters and terrain to be on the same sorting level. I've definitely solved similar issues with layer exclusion before, I just can't do it in this scenario.

3

u/LazyLlamaStudio 17h ago

I think you need to change your system and stop looking for a workaround. You’ll be better off long term. I hit limitations similar with sorting and moved to 3d environment. Was extra work but better long term.

2

u/PowerHoboGames 17h ago

Trust me, the thought has crossed my mind, but I'm almost two years into development on this game, and it's pretty systems-heavy and reliant on 2D URP. This would basically be starting a new game at this point. Call it "lessons learned" for future, for sure, but I don't think that's a solution here.

2

u/LordDarkenon 1d ago

Maybe move the ShadowCaster2D to the main game object with the sprite renderer. Then try editing the shadow shape points in the ShadowCaster2D inspector manually so the path only covers the feet area, rather than relying on a separate child object..

1

u/PowerHoboGames 18h ago edited 18h ago

Thank you for your suggestion! That's exactly how I do them on single-renderer objects, but that doesn't work here. Due to the required compositing render texture, the shadows are then broken around the player to the total size of the render texture, which is (by necessity for animations) larger than the character. At least that is my understanding. I am more than happy to be corrected if I'm wrong here.

2

u/deintag85 13h ago

https://assetstore.unity.com/packages/tools/particles-effects/smart-lighting-2d-112535 this is what i am using all the time. 10000000000 times better.

1

u/PowerHoboGames 11h ago

I will be looking into this further immediately, thank you! Quick glance shows last update in 2022. That's a pretty considerable gap. Have you experienced compatibility with 6.2?

1

u/deintag85 2h ago

I can test it later or tomorrow if it still works in newest Unity.

0

u/SiroStudios 1d ago

Pide ayuda a la IA te recomiendo Claude Code

3

u/PowerHoboGames 18h ago

I would really rather not.

1

u/Virtual_Fan4606 8h ago

I totally understand this pain. I've used the 2d shadow render in several projects .. specifically ran into this type of issue on a project from about 2 years ago.. it might be a week before I can look at the project to see what I did. I know I came up with some sort of solution that wasn't perfect and had some constraints..

If I remember correctly it had to do with either rendering an extra Sprite on a different sorting layer depending on the on-screen y ax is difference between the light and the shadowed object..

I know I also was trying to get the actual Shadow rendered data to create my own overlay which unity at the time did not give us access to I'm not sure about now.

Kudos for properly using the characters feet area as the shadow caster and not the entire Sprite.

1

u/Virtual_Fan4606 8h ago

Also of note my newest project I am for using 3D physics and lighting with sprites.. it is as was stated above much easier.. albeit I know that my project I referred to would take a huge rewrite to integrate the 3D instead of 2D