Hello, guys.
So in this video, we're going to make background move together with a player and it's going to feel
like endless background.
Let me explain when character is moving.
You can see that.
You can see that the camera moves with him, the camera, its borders.
And as well, we can see it has the position on the X.
It's moving over here.
So we're going to take our background and I'll just take one layer to explain.
So we have a background and character runs out of a background.
And at this point, you see.
When background is behind the camera, we're going to take it and place it.
Like this.
But because our background going to have duplicates on the left and on the right side, let me demonstrate
that you just watch for now and then we're going to make it together.
So this one will be minus one.
This one one.
We're going to have long background, right?
And we have our main background in the center like this.
Then when character exits the main part of the background till here, we're going to take it and.
Place it.
Like this.
But because it is the same.
Let me see.
71 position 71 and this is 41.
Right.
So what's going to happen real quick?
Just within one frame?
In a millisecond, it's going to be like this was 41 and it will become 71.
You see, you cannot notice the difference.
Well, you can, but basically, we will not be able to notice the difference.
And we just need to do this in the code.
So player runs out of the background like this.
Then we take this background and just change position to what is going to be and it will be unnoticeable
completely.
But we moved background so it's going to feel like an endless I guess it'll be easier to understand
once we make it work.
For now.
Let's just.
Creed C sharp.
Parallax background and go inside.
So as you know, we need a position of the camera.
Let's do this.
We're going to type sorry, private camera, and we're going to name it as Cam.
Then we need serialize field private.
Float.
PA relax multiplayer or parallax effect, let's name it as effect.
Then we need private float lengths of the background and we're going to just name it as lengths.
And we need private float, we need X position of the background.
So we're going to just type it as X position.
All right.
Now let's go to start function over here and make Oh, sorry guys.
This should be game object, not camera.
We're going to use camera, but it's going to be game object.
Yeah.
So let's do this.
Cam equals to game object dot find.
Main camera.
The name is very important.
We can it in the inspector, but I want to show you how to use game object dot find.
Right.
Although please never use a game object.
Don't find in the update.
Never.
It's going to overload your system.
Now links will be equals to get component of sprite renderer.
And then dot bounce, dot size, dot x, this is the length.
So what we do in here, we take in component.
E we take in component of the sprite renderer over here, the one we created first, if you remember,
which I can, how long it is, and then we take value of that and now we need X position, and x position
will be equals to transform that position that x.
Now let's go to update and in the update it might be a bit confusing, but just stay with me in the
update.
First, we need to define distance that we're going to use to move background distance that we're going
to use to move game object.
And it's going to be different for every background.
That's why we have parallax effect.
It's going to work like a multiplier.
So let's do this.
Float distance equals to can de transform dot position dot x multiplied by parallax effect.
That's cool.
Now let's click enter one time and type transform position.
And here we're going to change position of the background with the distance that we just created so
equals to new vector three On an x, we need x position plus distance we calculated and you know, to
make it easier to understand, let's rename it with the control R It's going to be distance to move.
All right then for the Y, we don't need to change it so it can be just transformed position that Y
and we actually don't have to type Z if we're not using it with this code.
We make our background layers move forward.
All right.
Let's go and see that in the unity.
We're going to select all of them.
Drag the script like that and just.
For the first test.
Let's put one for all of them.
And you can see they move in together with the camera.
Now we need to make them move according to how far it should feel.
Let's select the first one and make it move slow.
Let's put point three that select second one, 2.5.
And I'm just throwing values because this is not final.
And I want to show you just what I mean.
Now, look, if I uncheck Pose, you can see objects moving with a different speed and it feels like
something in real life.
Now, let's go and make it jump in front of the character when it goes far.
You remember how I showed you in the beginning of the video?
The background should jump in the center of the screen once it went too far.
So to do that, first, we need to know how far we moved this object.
So let's do this.
Float distance moved equals to cam transform position dot x multiplied by one minus parallax effect.
Now we have this value.
We're going to check if distance moved bigger than x position.
Plus this plus lengths.
Then we're going to do x position equals two x position plus lengths.
So if we moved out of the length of that background, we need to put it back.
Let's go back to Unity and we're going to change the speed again.
So the first one, let's do point three.
Second one, let's do point five.
We might change it later.
Might not.
We need to see it first point seven for the third one and 0.9 for the fourth one.
Like that.
Now let's see if his jump in where we needed to be.
I'm going to run.
And over here at the top and the scene window, we can see it's jumping, right?
All of it.
Yes.
Okay.
Now, what we need to do is to make it feel like an endless.
So what I want to ask you is to select all of these.
Duplicate them twice.
Now, let's select all of the duplicates and remove the script.
Okay.
Now I'm going to select background one.
One.
Make it a child of the first one.
And I'll just move it here so I can see it.
The first one will be minus one on the X is to put additional part from the left and additional part
from the right side.
Look, if I'm selecting the second duplicate and make position one, it's going to be here.
All right, Now let's do for the second one.
Where is one to move it here.
Well, let's arrange all of them.
And then three, three.
And then four and four.
And five and five.
And let's do the five.
Minus one.
One.
Next one.
Where is it?
Minus one.
One.
Third one.
Minus one and one.
And second one.
Minus one.
One.
Now, look, we have a feeling of a long background, right?
Like it's long.
What I want to ask you now is to go to play mode and don't look at the scene window.
Look at the game window and see if you notice anything.
All right.
Okay, I see the triangle, but it's endless.
You see it, right?
We cannot see the jumps of the background.
It feels flawless.
Now let's fix that triangle.
I don't know what's wrong with it.
What is the layer of the triangle I need to know?
This is the one.
Hey, what's up with you, dude?
Why you behave like that?
Oh, because we didn't change background.
And what it should be.
Minus one.
OC.
Not.
Oh, they all the default.
Certainly.
Oh, my God.
I'm so bad.
Okay.
What we can do is to.
I'll try to do that.
Select all of them.
Background.
Now, let's.
Well, this is my bed.
I'm so sorry.
We need to select all of them and make it background.
Then let's go to second one.
I don't know.
How did I miss this?
Open and select all of the pictures.
And set it to background.
Third one.
Background.
Background and last one.
Background.
Okay.
Now, you see, you might need to adjust it a bit according to how yours looks right now, because,
for example, I don't really like mine.
First, what we need to do, I guess, is to let me see if I run.
Yeah.
There is something below.
I can see something below.
So I need to take this first layer.
Um.
Over here.
Find.
The bottom square number 17 and make it all the way down like maybe that.
17 on the way down.
17.
All the way down.
Yeah.
And maybe wider.
Now if I make my plan from Slim.
It's okay.
Okay, That's cool, right?
That's ACTU.
We have something else important.
If you have your background acts like weird or your player, you see it's jittering right now, my player
or now I have my background jittering like that.
So it's about the center machine brain.
You need to select your main camera, find any machine brain over here and change it.
You need to play with it what works for you?
But it's supposed to be smart.
Update and fixed update.
No, wait.
Late update.
Yeah.
So smart update and late update should fix it for me.
It was smart update and a late update over here.
You can see and make sure your player set to interpolate in the region body.
And if you still have some issues like that, no worries.
It's going to be fixed later in the course.
Just leave it for now.
All right.
Let's move on.