This post outlines how to get started with light and shadow for 2D projects in Unity. We’ll achieve this by using the 2D Universal Render Pipeline (2D URP).
Why use the 2D URP anyway?
- Optimized for 2D games and apps, resulting in better performance than using the standard 3D rendering pipeline for 2D projects
- Lightweight! Suits a range of devices, including mobile and lower end hardware
- Customizable, allowing you to alter settings, materials and shaders to achieve the visual style you want for your game
Before and After
As you can see, 2D lights and shadows make it super easy to add atmosphere to your project.
Steps
1) Configure your project
If you are starting from scratch, it’s simple to get all the configuration you need by creating a new Unity Project using the 2D (URP).
If you want to implement the 2D URP into an existing project, you need to add the package form Package Manager and do a little additional config as outlined in this documentation.
2) Prepare a 2D level / environment
Prep a level ready to play with. If you started from scratch, build out a small 2D level. It can be top-down or side-scrolling style – your preference!
I used Unity’s native tilemaps to quickly create an environment, but there are many ways you could go about doing it.
3) Get acquainted with the global light
Configure your Global Light (in the Hierarchy as part of a project by default) with the desired settings. A global light is a type of light that will provide illumination for the entire scene (think of it like a sun ☀️).
- You can set yours to a lower value, such as 0.5 if you want the scene to have a moody, dark feel.
4) Experiment with various types of 2D Lights
Tinker with all the lights under Light -> the 2D options near the bottom of the list.
- Spotlight: A light that only illuminates its immediate surroundings.
- Sprite Light: Fit light to the shape of a sprite. Good for overlaying the light on an existing sprite e.g. a neon sign.
- Freeform Light: Very versatile! Can completely customize the shape of the light.
Now that you are familiar with all the lights, place them in your scene as you wish.
IMPORTANT! If you at any point want your lights to cast shadows, make sure to “Tick” the Shadows box for each light. You can set a value for the strength of the shadows to indicate how much light you want to pass.
5) Now for a bit of darkness 🖤
Add Shadow Casters to objects in your scene that you want to cast shadows.
If you are using a newer version of Unity (works in 2023.1.12f1), the bounds of the shadow caster will automatically fit around the bounds of your sprites! However, you can manually adjust the bounds as well by clicking Edit Shape.
Using tilemaps? If you want to try to apply a shadow caster to a tilemap layer, first make sure it has a Tilemap Collider on the object as well. This result doesn’t always work perfectly and you have less control around how tightly the casters fit around the tiles.
What next?
Adding lights already makes a huge difference to your project, but you’ll really see how powerful it can be once light sources or shadow casters are moving about in your scene e.g. a character with a spotlight moving past furniture that cast shadows.
Other ways to make your scene even more beautimous:
- Animate your lights! Very easy to do.
- Post-processing. Also very easy.
- Particles! Easier than you might think.
- If you are very pro, do cool things with shaders.