Welcome back my fellow game developers to a brand new video.
And in this video,
when we run our game,
when we click on the enemy,
you'll notice that he has an extra variable in here,
which is the enemy health and phi shoot him with a couple of bullets.
Notice how his health deteriorates from 100 to 40.
And if I keep shooting him from 40 to 0 and he disappears because he is dead.
So we are going to learn a lot of things in this video.
Put your thinking caps on and let's get started.
Oh, okay, So now time to start damaging our enemies using method.
So first thing we're going to do is we're going to create a way
to add health or to remove health from our enemy.
So I'm going to go into the enemy controller.
And here, Let's open up Visual Studio.
And I'm going to create an integer variable that will represent the health of the enemy.
Why is it an integer?
Well, because we want to keep things simple.
We don't want it to be a float.
So I'm going to add it right here under the speed.
So we're going to create a serialized field which will be of type integer.
And it's going to be called the Health.
And I'm going to set it to be 100 at the beginning,
so we never have the Health at 0.
I'm going to look back into inspector and see how that looks.
And they now, okay, great.
So now you can see that I have the option in here to change the health of our enemy.
Now, how are we going to make sure that every time the enemy gets hit,
we remove a bill of health.
We are going to create a method to do that. What are methods?
Well, you've already seen me create methods.
The update is a method.
We start as a method and also the gizmo select as a method.
But let's delve a bit deeper into the actual structure of a method and see how we use it.
So let me put my laser on,
and this is the method structure.
So it's similar to the way we create variables in that it has an accessibility.
So an access type, what is it?
Is it a private, a public?
We can't make it a serialized field,
but we can create either a public,
which means that we can access this method from anywhere in our scripts,
in any other scripts.
If it's private, we can only access it inside of the class where we are using.
Then we have the return type.
For example, void returns, nothing.
What is a return type?
As we move through the course,
you'll understand all the different return types.
But what this means is that we can actually return a value from this method.
If it doesn't return anything,
that means it's void.
So for example, let me show you in here.
This is a void because we don't return any value.
We don't expect any value to be given back
from this method later on when we do have a method that returns something,
may, I will make sure to point that out for you.
Next, we have the method name.
So usually whenever you want to name a method,
you have to name it by what this method actually does.
And you can notice how we create the name of a method.
It, unlike the variables were the first word has to always be in the lower class,
or the first letter of the first word has to be lower class.
In a method name,
all the words inside of the method name have to be capitalized.
So you can see that adding velocity,
so this method is a private,
so we only use it in a class void.
It doesn't return anything and its name is adding velocity.
That means it adds velocity to some kinds of objects.
And what are the parentheses on the end?
While these are called the parameter,
parameter means that we can send a certain value to that method.
And it can use it in order to do calculations or anything that it wants.
We will demonstrate how this all works in the method which will damage our enemies.
So enough talking, let's get started.
I'm going to open up Visual Studio and here,
and I'm going to create a method inside of the enemy controller.
I will scroll down on the Earth, the update.
And in here I will create a method to damage the enemy.
First thing first, what do I need it to be?
Does it have to be a private?
So I can only use it inside of here and the enemy controller,
or does it have to be a public?
That means I can access it from anywhere.
Well, most probably I will need it to be public because
I want the bullets damage the enemy whenever they hit the enemy.
What's going to be a public void,
and I'm going to call it the damage enemy.
And it's a void because we don't need to return anything from the damage enemy.
All we need to do is remove a bit of health that we already have.
And here, so the enemy health.
And let's change this or no,
I'm going to show you a very cool trick and just stay bit.
Anyways, let's continue on with our main focus.
I also want to send them a parameter of type integer,
which is going to be the damage.
They can.
So now what I can do is I can access the health and I can remove from it.
Sorry, the damage taken.
So now what happens?
You already know what the minus sign means.
That means that we remove from the health the damage taken.
And finally, we need to make sure that if the health is less than or equal to 0.
That means you just add a bit of spaces in here.
That means that our enemy has died and I want to destroy the game object.
Okay, So let's look at another time at this.
And before we do that, I think that the name health of a variable isn't that specific.
I think we need to change it to enemies health.
So what can I do in here?
I can change the name in here to enemy health.
Okay, and then scroll down here and change these two right here.
But this is a bit cumbersome,
especially if we have used health maybe 10 times in our code.
So how can we avoid this?
Simply double-click on health,
click Control R, R.
And now you'll see that you can add this.
So I'm going to call this the enemy health.
I'm going to hit Enter.
And if I scroll down,
you'll see that enemy health has changed.
So as a reminder,
control or our click it two times,
hold down Control R.
And you'll be able to change the variable name and change it everywhere and the game.
Now, the next thing we're going to do is we're going to actually call this method.
And where do we call it from?
We need to go to the bullet controller.
And let me just find the bullet controller in here somewhere.
Where is it in the scripts?
In the bullets, in the law terrible at controller.
And in here when we trigger something so we hit an object,
what we're going to do,
we're going to let me just remove this line from here.
So we instantiate the impact and we're going to go ahead and access the collision.
So what is the collision right here?
As you can see, this is a parameter that is being sent to the method on Trigger Enter.
What is the collision? It's the object that we've just collided with.
So for example, if we hit a wall,
the collision would be the wall that we've hit.
And in our case,
the collision will be the enemy that we've hit.
And I'm going to get the component, excuse me.
So I'm going to get the component which is on
this collision and it's going to be the enemy controller.
And in the enemy controller,
because I've made this method in here public,
I can access it right here,
which is the damage and amine.
And what should I send to this damage enemy?
You can see right now, here,
there is no arguments,
so we need to give the damage taken.
You'll notice all of this right here.
So I'm going to send to it the damage amount.
But we haven't created a damage amount.
So let's go ahead and create it in here.
So the bullet will also have a variable which will be an integer.
And that will just copy this,
which is going to be the damage amount and I'm going to set it to around 10.
So what's happening in here?
Every time the bullet collides with something,
we get the information of that collision.
We get the collision,
we get the component of the enemy controller,
and we access the damage enemy method there.
We sent to it the damage amount.
We go to the enemy controller.
It's public, everything we get the enemy health,
we remove the damaged taken.
The damage taken as obviously the damage amount right here.
And then we check if the health is less than or equal to 0.
If it is, we destroy the game object because the enemy has done.
So. Let's save all of that.
And let's go ahead and test it out in Unity.
So I'm going to click on the enemy,
make sure that we see the enemy health.
I'm going to run the code.
I'm going to try to shoot our enemy.
So I showed it until the bullets get third.
Let's see enemy health.
It was done.
There you go. It became 90.
Let me try to shoot him two times now his health should become 70 right here.
Keep that 80, 70, there you go.
The enemy is being damaged.
And if I keep shooting him with all my bullets,
you'll notice that 70 becomes 0 and the enemy disappears.
But notice what happens right now.
First of all, our bullets kept on flying everywhere.
Why did that happen?
Let me zoom out.
You can see the bullets did not interact with the wall.
They did not get destroyed.
And we got an error right here.
And let me open the console window so we can see what the problem is. There you go.
You can see that we have for errors for
the four bullets right here that interacted with the wall.
And let's double-click on this arrow and see what happened.
Oh, so you can see it takes us to the line that we have
an error and let me show you this one more time. Double-click.
We are on this line.
You can see right here.
And because this line didn't execute properly,
the destroy didn't execute properly solvable, It's kept flying.
This is just a side note.
Anyways, what's the problem?
The problem is that we are accessing the collision, right?
The collision should be the enemy because we are trying to access the NMI controller.
But the problem is that right now we've just hit a wall,
and this wall does not have the enemy script on it. Interesting.
So how are we going to save this?
Well, thankfully we have a very, very powerful tool.
Let me talk this right here.
Let me clear all these nasty errors.
We have an extremely powerful tool that we haven't yet talked about.
If I go into the edit layers, as we saw,
we have the layers of the physics layers,
we have the sorting layers.
We also have this tag right here.
And what we can do,
we can tag the enemy,
was an enemy tag.
And then whenever we are trying to check for the enemy controller, before we do that,
we can create a condition that checks if the tag on the collision is an enemy,
then we can go ahead and damage it.
If it's not an enemy or the object that we fit isn't tagged as an enemy,
then we cannot access the enemy controller.
So let me go back in here.
Let me show you where we can create dagger.
We can simply click on this plus button,
name the tag, whatever we want,
save that, go back to the enemy.
And you can see right here that we can tag him with a tag that we've just created.
Let me go back.
Delete the stack right here.
Yep. Okay.
Also because the enemy was tagged that can untagged.
I'm not sure why I did it.
So there you go. Will be removed from this list the next time the project is loaded.
Okay, So this was a big mistake,
but no worries about it because now it's time for
a challenge and your challenge is to tag our enemies.
So first of all,
create an enemy tag just like I showed you.
Make sure to tag the enemy within the enemy tag.
Before getting the enemy controller,
check for a condition if the collision object,
so you already know what the collision object is,
has the enemy tag on him.
And I recommend that you go through Google,
you find a way of how we can check for the tag and compare the tag.
If you want to go on by yourself, go ahead and do it.
If not, I will give you a small hint.
You can use something called the.com DAG,
go check it out in Unity dogs,
use your information, do your own research.
There is another way of checking for the tag.
I do recommend that you find it.
Pause the video right now and go do the shopping.
Hey, welcome back.
How did you get on with that?
So this is the unit of documentations, component.com, DAG declaration.
It returns a bool,
whether it's true or not or no public bool.
Also the compare tag is a bool and we're going
to use it in our if statement because as we know,
a condition in the if statement is a true or false.
So we can also see how it's used and it's used on the Trigger Enter,
how convenient for us.
So we use this other,
they called it other,
we have it as collision.
It's the same, no worries about that.
So let's see how we can do with in here.
So before we check for a collision,
I've added a tab right here.
I'm going to see if the collision that we've just hit.com tag is an enemy.
And before we do that,
we must create this type which is the enemy. Hit Enter.
So I'm going to make sure that we write it correctly.
So enemy, and I'm going to tag our enemy as the enemy.
So now in here,
if it is an enemy,
then we are going to go ahead and do the collision and another nifty tricks,
This video is full of nifty tricks.
If you only have one line under the if condition,
you don't need to add these brackets right here.
So if you only have one line,
you do not need to add the brackets if I add another line.
So for example, the destroyer is not part of the if condition.
Even if I take it and put it right here,
it will still be not part of the condition.
And if you're a bit confused by that, no worries.
You can add your own brackets.
This is just a small test.
You can even add a comment in here to say that this is what happens.
So I'm going to save that now.
And only if the collision an enemy tag on it.
Well, we call the enemy controller and give them to him.
Let's test this out.
I'm going to run the game and I'm going to start shooting the walls.
And there you go, you can see that we have no errors.
If I shoot the enemy,
then I will look at the damage the enemy has taken.
There you go. You can see that it's at 40.
I will shoot him three more times,
so he becomes 30, 20, 10.
And finally, I will shoot him for the last time and he gets destroyed without any errors.
So I hope you enjoy,
I hope you are committing all of the changes that we are making.
It's very important.
Sometimes I will be doing this outside of the realm of the video.
And with that said, I hope you enjoy it.
If you are enjoying this course,
please leave me a review.
It helps me out a lot.
It lets everyone know that this is a good course.
So make sure to leave me a review.
Thank you so much for watching.
I'll see you in the next video where we will be making
the depth of our enemies a bit more brutal.
So I'll see you then.