Welcome back, My favorite game developers.
As you can see now we have three enemies, x2, very scary.
Let me maximize on play.
And we have a wrong with not only have doors,
we also have enemies,
which are three skeletons.
These skeletons are the guys we need to kill in order to open the doors.
But for now what we do is we just catch them in the act using a list.
So I've maximized on play,
I should not have done that.
I'm now going to run again.
If I look in the rooms and today now,
I can see that I have the enemies right here and I have the two doors,
but the magic is that we caught them without adding them manually.
You can see that it happens automatically using contact filters,
using lists, using a lot of cool stuff.
So let's not waste any more time and let's get started.
Oh, case.
So now we have the doors closing.
It's time to start opening them up.
How do we open up the doors?
The way we will allow them to open is when we get
rid of all the enemies that we have in the current map.
So what we're going to do is we're going to
create a variable which will first of all make sure that the,
all the doors open when all the enemies are killed.
Maybe just maybe the level that you created doesn't have to kill all the enemies.
Maybe it's a level for the shop,
for example, just like we had in Hades.
So that's why we'll add this variable.
So let's get started.
So in here we have the enemy controller.
As you can see, I've organized everything.
Anyways, let's not waste time because in the wrong manager,
we are going to add the volume right here.
And that is to open or open doors.
When and and me is dy.
There you go. We save that.
We'll go back in here and we'll make sure to set this,
this particular room to true open when enemies die.
Okay, great.
Next thing we need to do is we need to get a reference of all the enemies.
Well, how do we do that?
We will have obviously multiple enemies.
That means we will have to most likely get some sort of array.
But why we don't want to use an array?
Because when we have an array,
how am I going to demonstrate this?
So for example, you can see right here,
if I click on one of those arrays right here,
and if I use the minus,
this will simply remove the door from our game.
It does not actually delete the entire element.
So we still have two doors right here,
but one of them is missing.
Now when it comes to the door,
this is not a huge problem because we already know that we will have only two doors.
We are not going to count them.
We are not going to get rid of them.
They are there, they are static,
they are not moving.
But when it comes to the enemies,
what we want is we want to get a reference to all the enemies in the map.
And then as we slowly kill them one by one,
we are going to count them.
And if we find that all of them have disappeared,
then we will open the door.
And this will cause a problem when we are using a rate.
That's why we are going to use something a bit better,
which is the list.
So a list is just like an array.
It represents a strongly typed list of objects.
And by the way, this is the documentations of Microsoft for the C-sharp,
not the Unity documentation,
but it's pretty much the same.
You can find examples,
you can find what ever you want and lists
the great thing about them as they are much better at sorting,
they are much better at sorting through and manipulating.
So what we can do is as each enemy dies,
we can remove him from the list without worrying about any kinds of error,
the list will adjust itself.
So that explained, I will let you find the list,
the class, find the documentation of this.
And I'm going to issue you the challenge again just like the last challenge I know.
So annoying when you have to do something by yourself without seeing what I did first.
But it's time to grow up,
time to become real game developers,
look for problems and do things that we have never encountered before.
This is the beauty of it.
Look through the documentation and find how we can instantiate a new.
Let's create a serialized field list.
We want to see the list in the inspector.
Later on, we obviously need to make it private,
but we want to check if everything is working.
And finally, the type of the list will be a collider to D. So find the documentation,
go through it, create a new list,
which is the list of enemies of type collider 2D.
I'll see you in just a bit.
Okay, welcome back.
So how did you get on that?
I hope you're not too angry at me.
Anyways, let's go ahead and create a serialized field.
And this is going to be a list.
And as you can see, well,
you saw in the example,
let's just double-check and see how it's created.
So you can see it needs the systems dot collections,
which is right here.
Oops, sorry, which is right here that allows us to create lists.
Let's go see what we have in here.
Let's say I'm scrolling down,
how do you create lists, please?
How do I create a list?
Where is it? Where is it?
Okay, So there you go.
This is how you create a list.
So we need to type in the type of this that we have inside these two unnatural,
what they're called and say that it's equal to a new list.
So back in here,
the type collider 2D,
I've already told you that.
And you'll see why it's a collider 2D in just a bit.
So enemies, and it's equal to
a new list of Collider to the enemies and we close it off with these two parentheses.
Now, obviously it took you just a little bit more time to find everything,
maybe look through the documentation, but that's good.
Now you can say that proudly that shoe look through
the documentation found out by yourself how lists are created,
and this will give you much more confidence
as you move through your game development journey.
I'm not always going to be here.
Sometimes I won't be sometimes I'm don't have the time to answer you.
So this is a great exercise.
I love these exercises.
I hope you're not too angry.
Let's continue on. What do we need to do right now?
We need to go into the rooms right here
and we're going to create a new empty game object,
which will be the enemies.
And this will hold all the enemies that we have.
The skeleton enemy right here that C does he need anything?
Also the shooting range.
Let's make it a bit smaller so we don't want the enemies to shoot us.
We know everything works.
That's good and we are going to make their health.
But smaller, maybe three.
So he takes three hits, apply the changes.
And I'm going to move him to the enemies.
I'm going to also go into the Prefabs and add a couple of enemies.
And here, so the enemy,
the skeleton will adding another one.
Here. Another one, so great.
Now we have three skeletons roaming around that will chase and shoot the player.
We will save that.
Okay, great.
Now the next thing that we want to do is we want to get the,
a reference to all those enemies and be able to count them.
Now what we can do, obviously,
as we can go ahead,
select these three and drag them in here and set them manually.
That would be good,
that would be working.
But we want to have a more dynamic way of accessing these enemies.
One of those dynamic ways is using find objects of type,
where we go through all the objects and we find all of them.
This is also a good idea,
but it is very taxing on the system.
You have to go through all the elements,
starting from the camera and down until you
find all of these enemies of type or object of type.
We're not going to do that.
Instead, we are going to do something much more complicated at first,
but much more effective and less taxing on the system.
And it's a great way to learn something very new.
And we already have the,
let's say the infrastructure for that established by having
the colliders of the enemies and the collider of the room.
So what am I talking about?
You just saw it.
It's the collider, 2D overlap collider.
What does this do?
It takes in a contact filter,
which we will look at in just a bit.
And it gives us the results of all the collider 2D objects.
So a contact filter used to filter the results differently.
What do we mean by that?
We mean that we have the collider of the room right here.
So we have the collider,
we have all the objects inside.
We don't want to catch the player,
for example, we are only interested in the skeletons.
So we need to add this contact filter that will understand how it's used.
But let's continue on.
So contact filter and the result,
that result, It's the array.
To receive the results.
We can use an array,
we can use also a list.
Don't worry about that.
Let's continue one.
What do we need?
So it returns the number of results if we want a return,
and then we are going to check what is a contact filter.
So I'm giving you all the ways of how we create.
So honestly, I had no idea what a contact filter was.
This is the first time I encountered when I was creating this game,
even though I've had a lot of experience in game development,
this is really the first time that I actually used it into the,
so I went and looked for it.
Okay, So look, we have a lot of properties.
And if you go back,
we can see right here results differently.
And what else do we have?
We have something called bi layer mask.
I know what layer mask is,
so I have to look and I find.
Set Layer Mask, excellent.
This sets the layer mask based on some kind of provided a parameter or something.
Great. I click on it,
I see contact filter, layer mask.
I give it a layer mask.
I know what the layer mask is and I'll show you how we are going to use it.
But anyways, these are the steps that even I sometimes take.
So don't think that NEW,
don't remember everything and you should know,
no, you should do your own research.
Always look through the documentation and now let's go ahead and get started.
So I'm going to go back in here and I'm going to create, first of all,
a private collider 2D,
and this will be the room collider.
Save that.
And then a, another private contact
filtered to D and it's going to be the contact filter to D.
I'll just keep the name as it is.
So now and start,
what are we going to do?
Well, first of all, we need a reference to the room collider.
So room collider is equal to get components.
And it's going to be the collider 2D.
Great, So now we have a reference to the collider on the room.
Next thing we want to do is we want to get the contact filter 2D.
And we're going to set the layer mask just like we saw.
And what did we say we need inside of the set Layer Mask,
we need the Layer Mask.
How do we get the layer mask?
Well, we know what layer is,
but what is the mask?
The mask is a way to let us say to convert,
to understand what the layers are.
So what can we do?
We can use something called the layer mask.
And we can use a method in here that says to get the mask.
And how do we get the mask?
We can get it actually buy the layers name.
Great, so now we have access to the layer's name.
And what does the layer, it's the enemy.
And because this is a string reference,
makes sure that shoe have potted correctly,
go even copy it if you have to.
But what's happening in here,
we got the layer name.
We use the get mask,
which is a method available in the layer mask library.
We use this whole layer mask and set the layer mask on the contact filter.
Complicated, I know.
And now we use the room collider, thought overlap collider.
And in here, what did we need?
We need the compact filter to the,
what are the results?
Where do we want to throw our results?
And you can see right here the arrow,
if you click it, there you go.
You can add it to a list or we can add it to a collider 2D based on what we give it.
So if we give it an array in here,
then this result will be an array.
But because we're giving it currently a list,
this will be as a list, save that record.
So again, let's go through this one more time.
We created a contact filter.
We understood what contact filters are through the unit documentations.
We've added a collider 2D.
That's very simple.
We got a reference using the get component.
We already know how we do that.
We access the contact filter.
We use a method called the Set Layer Mask.
This set Layer Mask requires a,
something called a layer mask,
not just a layer, a layer mask.
So what we do,
we looked for get Layer Mask.
And this will actually be a sight challenge to you.
Go look for Layer Mask,
get Layer Mask and stand how we found this method.
So this is a whole different process.
We use the get layer mask,
which is part of the layer mask and Unity engine layer mask,
and we use the name of the enemy layer.
So now what happens as soon as this room manager starts?
First of all, on Trigger Enter it finds the enemy.
Second of all, it looks through all the enemies that are already here.
And as I've said, there are many ways of catching the enemies.
There is find objects of type.
We can have used the entrepreneur enter,
but I thought this is a great opportunity to learn about lists.
Learn about contact filter in never know when.
Sometimes you might find yourself in a situation and
programming where you just can't do it without a contact filter.
So save all of that.
We go back into Unity and let's look at our enemies now as soon as we run,
we should see the three enemies here.
Hopefully run the game.
There we go. Excellent.
And you can see that we've got all the colliders of skeleton one,
skeleton to and skeleton three,
or however you want to call it.
Excellent, We have a reference to them.
So now the next step is to kill all of them
and open the doors with blue that in the next video,
make sure to complete your changes.
Make sure to leave a review and I'll see you in the next video.