Setting Up An Easy Parallax Effect in Unity
Let’s talk about parallax for a few minutes. What is Parallax? It’s basically a technique where background images move slower than foreground images, which gives an illusion of depth in a 2D scene. Early parallax scrolling has been seen in video games since the early 80’s. The original technique goes back to1930’s animation techniques.
Basically what is happening in Parallax is that you have separate background layers moving at different speeds. The closer layers move more quickly than the farther layers.
The Unity Engine makes it very easy to set up a parallax effect with just a few changes to the main camera and the background elements. Consider the following scenes:
Above the screen scrolls with no parallax, giving a pretty flat feel. Now let’s take a look at parallax:
So what’s the difference in setup? Basically you need to do a couple of things to set this up. First of all, split your background up into different layers. Here, I have a foreground and 2 background tile maps. Give each layer a larger z value. Here I have the foreground at z = 0, the near background at z = 1, and the far background at z = 3:
The next thing you need to do is change the camera projection setting from orthographic to perspective:
What’s the difference between orthographic and perspective? Here I’ll give a shout out to my dearly departed father, an artist, who explained this to me over 30 years ago. When you draw in perspective you draw how you see things. Meaning things farther away will be smaller. Things closer will be larger. That’s perspective. Think of orthographic as purely 2D. There is no depth, the Z value is basically ignored.
That’s how a few basic changes in Unity settings can give a parallax feel to our scene.