Welcome back, my fellow game developers.
In this video, when we run the game,
you'll see we have lots of errors.
We don't fix all of them,
but we fixed most of them.
So I'm going to go ahead and pick up the shotgun,
pick up some coins,
get hit by the spikes,
go through the door,
go through this portal.
And there you go.
You can see that I'm on the other side was my shotgun,
was my pistol with my health,
but it's not visible
because we haven't tweak those things.
But if we go to doing player scroll down,
you'll notice we had 500.
We cut it by the spikes and it's 490,
even though we didn't do anything.
So let's not waste any more time and let's get started.
The things we learn in this video are very important,
a bit cumbersome, but very important.
So let's get stomped.
O k.
So currently we can go from level to level.
We have options where we
choose one of the random levels that we have.
Obviously, there are a couple
of things that we will need to
tweak with the appearance of
the door and disappearance and so on.
I feel there is still a bit of tweaking left,
but there is a even greater problem.
And that is once we pick up the coins,
the weapon, and get hit by these bikes or something.
Let me show you.
When we pick up the shotgun,
pick up some money,
get hit by the spikes.
There we go. So we can see our life is for 9500,
we go to a number level.
Maybe this will be, and you can see right now,
whereas the player, if I
look at D1 player, he is detached.
He's not there. So you can see
that his life is back to 50.
If I tried to choose the shotgun,
I cannot. And there you go.
You can see that the coins are
0 and that is something that we will want to
change to make sure that our player actually
keeps everything with him as he goes through the level.
So let's go ahead and just do that.
So I'm going to open up
my scripts in here and I'm going to
go to the player controller, scroll up.
And what we want is obviously we want to keep
our weapons from level to
level and the health of the player.
So to keep that,
we need to make sure that we do not destroy the player.
We can save it on some other object
or somewhere in the project.
But obviously there is,
the problem was loading
a new scene which resets everything.
So what I'm going to do is in here I'm going to create
a public static player controller.
And it's going to be the instance.
And in the instance I'm going to
create a private or know what,
let's just write awake.
And in here I'm going to say that
the instance is equal to this.
And then in here I'm going to don't destroy on load.
And it will be this.
So if I save this and hover over onto don't destroy load,
you can see that to not destroy
the target object when loading a new scene.
And that's exactly what we want.
So what will happen now?
The object or the player and our case,
we'll keep everything that he
had with him from scene to scene.
It will not be destroyed,
it will not be reinstantiated.
But there is the problem that
the start will not be called the Gap.
So we will not be setting the main camera.
We will keep the player animator,
but the main camera will change.
And you'll see that this will produce a lot of
errors that we will fix as we move on.
But for now, understand the don't
destroy load and what it does.
Now, I'm going to go back in here.
And if you look at the,
where is it, the room,
the portal, I've changed the portal one,
which is this one, to go to level four.
So we can go to the cowardly.
And let me just go to the scenes because I was
trying something out and I turned
off the dome player in here. So there we go.
That be turned him on.
Let's save that.
Let's go back to wait for this to load any day now,
save that back to where we're serving.
The skull is where we have Kelly's.
Yep, We were a skeleton.
So save that.
And you'll notice in the hierarchy right now,
and unity, the new update, sorry, it's terrible.
So in the hierarchy you'll notice that we have the don't
destroy it up and it has the doom play or under it.
So I'm going to pick up the shotgun,
become the Bitcoins get hit by the spikes,
open up the door, moved to level 4.
And there we go.
And you can see that now I am here,
but the problem is,
you can see a player down and I layer up here.
So now we have two players.
One of them has a shotgun, the other doesn't.
And you can see we have a ton of errors and the cons.
So I'm going to open up the console.
I'm going to pause the game.
And first of all, we have the doom player.
And here this is a debug I was showing.
Let me delete this.
You'll see what I mean in just a bit.
I'm going to delete this.
Next.
We will have the,
this arrow right here,
which is okay, so this is the problems at all.
We'll fix this in the later video.
Then finally we have
the object of type camera has been destroyed.
You are still trying to access it.
So I'm going to double-click in
here and it's the main camera,
and it's exactly what I told you about.
So now that we know that our player is transitioning,
and we can see right here in the scene,
if I click on the doom player,
which is the don't destroy,
we have to do player,
so we'll have to fix that.
But one of them still has the shotgun.
So technically, he will be
able to move with that shotgun.
And I look on the tomb player,
scroll down to his health and you can see that the,
his current health has also transition.
Unfortunately, there is still
the problem of the bitcoins,
but we will fix that step-by-step.
Let's first of all start off with
managing the double layer standards.
So this is something that we will need to address.
And how are we going to adjust this?
Well, there is a very nifty trick that we can use,
which is called a singleton pattern.
Let me show you using
code what that is and then we will explain.
So first of all, in here on awake,
instead of immediately setting the instance,
I'm going to say that if we don't have an instance,
so the instance is
amines are if we already have an instance,
so it's different than null,
and the instance is different than this.
Then what I'm going to do is I'm going to
destroy this game object.
So what am I doing exactly in here?
I'm going to check as soon as we await.
Is the instance different than null.
So that means if the instance is not null,
I'm going to destroy the game object.
But there is another thing
if the instance is different than this.
So what I'm doing in here,
as I'm starting the game off,
I'm checking if we either have an instance already
or the instance is equal to this game object,
I'm going to destroy this game object.
And this will ensure that we,
first of all don't create
a second instance of this game object.
And second of all,
we don't duplicate the object that is already in here.
So if we go to a new scene
and we find that we have in a game object,
it will be destroyed immediately.
But else, in case we don't have an instance,
so we're starting the game all over again,
or when we start a new scene and there
is no instance that is equal to this,
then I'm going to say that instance as equal to this.
This will just create a new instance.
And this will make sure that we do not have
two instances or that there is already one available.
So I'm going to save that and I'm
going to demonstrate how this will
work and buckle up because we will have many errors.
And I was testing this out to make
sure I can properly manage everything.
So I pick up the coins,
I get hit for 90,
open up the door, and I'm going to hit the portal,
move away from it. And there we go.
So you can see right now,
I don't have a team player.
The second player has been destroyed.
That's because it checked and it's awake.
Is there an instance?
Is instance different than null?
Yes, it was.
And is instance equal to this?
Yes, it was. So we are going to go ahead and destroy.
So back in here you'll see that we have
only 12 employer which has a shotgun.
He can shoot.
He cannot point and he cannot change his weapon,
but he does have the shotgun.
He is animating,
everything is working and you'll notice that
he is in the same position that
he was in the previous scene.
And that's because he will actually
keep that with him as he moves.
So you'll notice that when I entered the portal,
I then moved away because I didn't
want to stay in that portal.
So with that information,
after all the way, Let's see what errors we have in here.
So I'm going to pause because there is a lot.
So first of all, you'll see the problem with the camera,
which we will fix in just a bit.
You'll see that we have a problem
with the cowardly enemies.
They are trying to find the object which is the player,
but they simply cannot.
Not only that, it's telling us that
the object transform has been destroyed,
but you are still trying to access it.
What do they mean by that?
Well, as soon as they came into this world,
they saw the little doom player that was here initially.
And then when the scene started,
he got destroyed and they were still trying to access.
And if we double-click in here,
you'll notice it's right here on the player chase.
So the whole problem is with the player Chase.
Obviously we will be fixing that also,
the camera will be fixed.
And finally, this one problem is also here and
the player game object active and hierarchy.
So all of this will be fixed and just a bit.
So I'm going to unpause this and let's
handle the problems one at a time.
So stop this, open this up, the game start.
So open the scripts up.
And what shall we do?
First of all, first I think let's
handle the problem of the main camera.
So if we go to the player controller where we use the
Main camera. Let's double-click
on Main Camera and scroll down,
see where we use it.
We use it.
There we go, in the pointing the gun.
So in here I'm simply moment to check if
the main camera is equal to null.
That means we don't have a camera.
I'm going to say that main camera is
equal to camera dot main.
And this will only happen once we
check is the camera null.
That means we don't have a reference.
If it is, then we will set the main camera.
Again. If not, we will just continue on regular,
regularly with the pointing a gun at most.
So back in our game will run.
The game will take up this one, opening. Anything else?
We will go through this.
And on the other side,
we shall see in the scene.
There we go. You can see that I'm able to point this out,
but obviously there is,
the camera in the game world
isn't focusing on the player.
And this is something that we will
obviously need to flex.
So let's get to fixing.
I'm going to go into my scripts.
And in here while we're shall we call this,
Where should we call this?
Let's see, where did we put the shape camera?
Can we know?
We don't know what?
Let's just go into
the scripts and put it inside of the managers.
Managers, managers, managers.
There is no managers.
Okay.
So managers for some reason, our cameras.
There we go. So and the cameras,
I'm going to create a camera control
or camera camp controller.
Hit Enter, wait for it to compile.
Double-click in here, and
I'm going to do a couple of things.
So what? I'm always doing a couple of things.
So what are the things that we are going to do?
First of all, what we want to do in here is
always find the player target automatically.
So previously what we used to do is if we went into
the camera right here and to the virtual camp,
we said the follow player.
And the problem is when we go to a new world,
let me show you in detail,
should have shown you before.
So if I, for example,
here I have the player,
I click on him, he's in the hierarchy.
I opened up the door,
I call to another level.
You'll notice right now if I open up the cameras again,
which are exactly the same virtual camera,
you can see that the follow is missing,
so there is no player.
Why? Because we have set the player before,
but now he was destroyed.
He's missing, so we need to find them all over again.
So in here I'm going to open this up and I'm going
to create a variable which is the player to chase,
which is obviously going to be,
let's see, the transform or no atlas just make him the
layer controller layer to look at.
And he's going to be equal to find an object of type.
The player, oh, no,
no, no, not here.
Costs down and start.
We are going to say that the layer to look
at is equal to find objects of type,
and it's going to be the player controller.
Now we have a reference to
the player controller that's
also create a virtual camera.
So, so cinema machine,
virtual camera also inherit we need to
double and C using cin, a machine.
There we go, Save that.
So cinema machine,
cinema machine virtual camera, thank you.
Is going to be the verb.
Virtual camera.
And we're going to set it in here as soon as we start.
So it's going to be equal to get component.
And where are we engineer?
Where shall we attach the camera controller?
We might want to attach it in the lecture.
We will attach to the virtual cameras
so we can immediately
access the same machine virtual camera in here.
So get component.
And it's going to be the cinema machine virtual camera.
There we go. And what are we going to do in here?
Well, we are going to set the virtual camera up
top follow to be equal to
the layer to look at dot transform.
So this will be at the start,
but an update, we will need to create a while loop.
So we've covered already while loops.
And it's going to be checking for the player
to look at while he is equal to null.
That means we haven't found a player yet.
We are going to keep searching for the player to look at.
Keep finding the player controller
until we finally find them.
Once we find them,
if we do have a virtual camera.
So this is just an extra step to avoid any errors.
The virtual camera not follow.
Will be equal to the player.
We will save that. And let's look again once what we did.
So first of all, we created
the player controller, which will,
which is a variable that we'll
look at players to look at,
which is a variable of
the player controller, a virtual camera.
These are all private.
We can name the private.
I just didn't have the time.
You know what, let's just make sure
that we don't have any confusions in here.
So private, private because we
won't need them in the inspector.
We can set M if we want.
We don't have to. Then start.
We are going to find the player controller.
This is just in case.
We start off and we have the player in the scene,
then we find the camera,
then we set the follow cam to be the player to look at.
Have only one player in any scene,
because we've already made sure of this right here.
And the Singleton pattern.
There we go.
This is called the singleton pattern, keeping in mind,
whenever you tell it to somebody,
they will think that you're very smart.
Trust me, even though it's
singleton means a single object.
So just in case they ask you what is a singleton pattern?
Anyways, the virtual camera dot follow,
we follow the only player that we find and
then an update just in case this is for,
let's say the player which doesn't get
destroyed usually loads before all the other objects.
So just in case we start off the game,
we don't find the player immediately he is missing.
We cannot solve him as follows.
Follow then an update we will keep looping through
until we find the player would check
if we have a virtual camera.
This is just an extra step.
I don't think we will need this even,
not even once because we will be
attaching the script to the virtual camera,
but just in case there isn't a virtual camera,
we will check the condition and then we
will set the virtual camera dot follow,
save that back in here.
Let's go ahead.
And so the cameras,
Let's see, shake and
shake the camera controller through the virtual camera.
And for some reason I can see that this is not a prefab.
Is that on prefab this.
So when the prefabs University,
whereas the weapons you are a poor to level objects here.
Nope.
Okay. So I can see I did not prefer
up the cameras for some reason I thought I did.
But anyways, that's just prefer the camps and here.
And you can see that
the virtual camera will have the camp controller.
So save that.
Let me go to the other level.
I'm not going to do this to all the levels.
I'm just going to do it with the ones that we're testing,
fixed everything and it will probably be the same.
So cameras and here I'm going to delete it.
And I'm going to go back to the prefabs and make
sure I add the cameras in here in the hierarchy.
Save all of that.
Open this up, okay, seems,
everything seems to be good. Back in the scenes.
Back in the skull is save that, run the game.
Go to the door, opened it up,
move to the next level.
And we should see, there we go.
So we again found the player and now
the cameras are working perfectly fine.
Great. So now with that out of the way,
if we look back in the console,
we still have the problem of
the player getting chased, not being fun.
And because I see that
this video has been already going long enough,
I'm going to posit right here and then we're
going to continue on in the next video.
So I hope you enjoyed.
This is a bit of a boring subject, I know,
but at least you learned what a singleton pattern is.
You understood why the camera was not going.
I am trying to explain as much as
possible even though it's very boring.
But I want you to understand how everything is feeling.
At first, I was also confused,
but as I went through everything,
I understood the things and I
wanted to pass them on to you.
So with that out of the way,
thank you for watching and I'll see you in the next one.