2D Shadows in Unity

Home Code 2D Shadows in Unity

One cool feature of 2D projects in Unity is that you can work with light sources and shadows. Here I'll explain the required steps to start rendering shadows based on sprites.

Adding the 2D Render pipeline

In order to render shadows in 2D, we must first add a renderer capable of doing so. For this purpose we'll use the experimental 2D renderer. First, we must create a Renderer asset in our project. You might not see this option if you don't have the proper packages installed. For this example you'll need to add the Universal RP package from the Package Manager.

Create > Rendering > Universal Render Pipeline > 2D Renderer

After creating the 2D Renderer asset, we'll need to add it to our Universal Render Pipeline Asset and set it as the default renderer. Assuming you already have a working Universal Render Pipleline Asset, open it in the Inspector. At the top you'll find the list where the 2D renderer must be set. Set it as the default renderer.

Adding Shadow Caster 2D's to sprites

In order to make a Sprite cast shadows, it must have a component named Shadow Caster 2D. This will make an individual sprite cast a shadow. If you want overlapping shadows to render as one, group all the sprites that must cast a shadow under a single parent object, and add the Composite Shadow Caster 2D to it. Shadows will then blend into one another, creating more realistic shadow behavior.

Tech Stack

PHP

HTML

Javascript

C#

CSS