0:00
[MUSIC]
In this lecture, we'll finish setting up our Parallax Scrolling.
Our objectives will be to modify the main camera to only
render the foreground layers.
Create a background camera that renders the background layer.
And adjust the orthographic size on the background camera
to achieve the parallax scrolling effect.
So now that we have our background created in place on the proper sorting layer and
the proper layer, let's go ahead and set up our camera.
So if we go ahead and
look at the main camera, notice that the clear flags is currently set to sky box.
What that means is it's gonna render everything that we see.
And then it's gonna render the skybox.
There isn't actually a skybox to find, so
it defaults to that background color of blue.
So I actually wanna change this from skybox and choose depth only.
Which means it's gonna render just the items that are defined down below
under culling mask and it won't render anything in the background.
So, you can see down in my Game view, we see just this sort of
dark gray background now, which basically represents transparency.
Nothing is back there.
So, the next thing that we wanna do is look at the culling mask.
So, once again this is showing all the layers that it's gonna render.
Currently, it's rendering everything.
1:16
So by putting those background elements on the background layer,
we can actually turn this off.
So let me just select Background.
That'll uncheck the Background.
And notice that from the Game View, we no longer see those background elements.
Of course they still exist, they're up here in the Scene View.
And we can see them there and edit them there but
the camera, once again notice the camera preview here
does not actually show that background layer since we just turned it off.
So let's go ahead and create our second camera.
Our second camera's what's gonna actually render the background.
2:02
So we need to go in and change some of the properties of the camera.
First and foremost, this is set to a perspective camera.
Let's go ahead and change this to orthographic like the main camera.
We also need to adjust the location of the camera, so
right now it's its own game object.
I wanna make this a child of the main camera because I want this camera to
basically just follow everything the main camera does.
Of course we have that camera 2D Follow Script on the main camera,
which tracks the player.
And we want the background camera to just track and
do everything the main camera does.
So let me go ahead and make the background camera.
A child with the main camera.
And you're gonna wanna make sure that this is positioned at the exact same location
as the main camera.
So, since this is not a child of the main camera, we can just set its
position to 000 which means it'll be right at the same location as its parent object.
Notice in the Game view, we can essentially see the whole game now.
Because we haven't set up the rest of our properties on the camera, so
essentially this is just rendering everything.
And right now the background camera is actually rendering in front of
the main camera.
That property is defined here where it says depth.
So the depth is 0 of the background camera,
and the main camera the depth is -1.
So 0 is in front of -1, so if we go to the background camera once again,
let's set the depth to Negative two.
So this is going to render behind the main camera, for clear flags,
we'll leave skybox because in this case, we actually do want to render this blue
background unlike the main camera, because this will be our farthest back camera.
And then for the culling mask,
we want to change from rendering everything to rendering nothing.
So, it unchecked everything and
you can see now we're just seeing the main camera down here in the Game view.
Let me click on culling mask again and choose just background.
So, now it'll render what we see on the main camera, and then behind it,
because we changed the depth, we're rendering just the background layer.
So the other thing that we haven't changed yet
is the size of the orthographic projection.
So if you look at the main camera, the default size is five.
So both of them now have the same orthographic projection.
If I actually change this to something like eight, notice what happens.
Everything in the background looks a little more zoomed out.
The larger the size the larger the parallax effect that your gonna get.
That is the more distant the Background layer
will appear in relationship to the Foreground layer.
So let me actually set this just this one up.
So we'll go from five to six.
4:31
So we've got our camera properties set up.
Now one other thing that we want to do is we actually want to turn off
the audio listener.
So by default, cameras have an audio listener component.
Really only one of these can be active in the scene at a time.
So if I actually play right now, notice that down here below the Game view,
I am getting a warning.
If I actually open the Console, notice it's continually
outputting warnings that says there's two audio listeners in the scene.
Please ensure that there's always exactly one audio listener in the scene.
So we wanna resolve that warning issue.
It's not a game breaker, doesn't crash our game.
But it's inefficient, and Unity's confused what's going on.
So let's go ahead and close the console and stop the game.
So, in the case of the background camera,
we could just deactivate the audio listener, so it's not on.
It's not there, or, of course, we could always click the Gear and
actually remove the component, which I can do now.
So now, when I play, notice that I no longer get that error message.
And if I move around, notice that I have this sort of parallax effect happening.
So the mountains and clouds are moving at a different speed,
a slower speed than the foreground elements.
So I've basically got my parallax effect working.
Now I could go ahead and add additional cameras,
so maybe, have an additional layer for background one, background two.
Maybe I put the clouds on background one, and the mountains on background two.
And adjust the orthographic size for each camera.
Have each camera render differently.
Have the middle one be depth only, clear flags.
And then have the farthest back one be skybox which would render the background.