WEBVTT

00:00.120 --> 00:05.960
Once our enemy game object fire on this player game object, then this player need to take damage.

00:05.960 --> 00:10.000
So we're going to do the same thing as we have done in this anime game object.

00:10.000 --> 00:16.320
Firstly, we need to give some health amount to this player game object and then we can just take damage.

00:16.320 --> 00:19.000
So let's select our player GameObject in the hierarchy.

00:19.000 --> 00:21.280
And let's open up the player script quickly.

00:21.840 --> 00:23.680
And let's go to the top of our class.

00:23.680 --> 00:29.160
And we're going to declare a variable, or we're going to give some help to our player game object.

00:29.160 --> 00:33.280
So let's make a public and that will be int whole numbers.

00:33.280 --> 00:36.280
And we can call this one to something like Maxhealth.

00:36.480 --> 00:40.560
And by default I'm going to give around five unit of health.

00:40.560 --> 00:42.800
And let's close that up with semicolon.

00:43.000 --> 00:46.800
And let's make a takedamage function in order to take damage.

00:46.800 --> 00:48.880
So I'm going to make it somewhere in the bottom.

00:48.880 --> 00:52.880
Somewhere over here you can make it anywhere you want.

00:53.120 --> 00:56.960
So it basically doesn't need to be in order.

00:57.160 --> 01:02.780
And we need to make this function public because we're going to call this function another class or

01:02.780 --> 01:08.860
another C script where enemy is attacking on our player game object or shooting.

01:09.060 --> 01:11.500
So let's make a public for function.

01:11.500 --> 01:13.860
We're going to type void and we have to name the function.

01:13.860 --> 01:19.740
So I'm going to call it something like take damage and take damage and make first bracket and some curly

01:19.740 --> 01:20.580
brackets.

01:20.740 --> 01:23.980
And this takedamage function just going to take an argument.

01:23.980 --> 01:26.580
So we're going to pass int damage.

01:26.700 --> 01:30.820
And later on we can just replace this damage in damage variable.

01:31.140 --> 01:33.460
The number we would like to go with.

01:33.460 --> 01:37.100
So we can go with any number that we would like to go with.

01:37.140 --> 01:39.220
For now let's go to Maxhealth.

01:39.260 --> 01:40.580
So we need to subtract.

01:40.580 --> 01:42.180
So we will type Maxhealth.

01:42.340 --> 01:47.140
And we're going to subtract from this Maxhealth minus equal this damage amount.

01:47.340 --> 01:49.500
And let's close that up with semicolon.

01:49.500 --> 01:53.100
So let's suppose we call this takedamage function for the first time.

01:53.100 --> 01:54.540
So the maxhealth by default.

01:54.540 --> 01:56.620
As you can see we have given five.

01:56.620 --> 01:59.260
So this is going to be over here five.

01:59.380 --> 02:05.760
And let's suppose as a damage parameter you just or you just replace this damage a parameter with something

02:05.760 --> 02:06.400
like one?

02:06.840 --> 02:11.000
So the maxhealth is five, so five minus one is going to be four.

02:11.040 --> 02:14.040
And if we call this takedamage function for the second time.

02:14.040 --> 02:15.640
So the max is now four.

02:15.640 --> 02:17.760
So four minus one is just going to be three.

02:17.760 --> 02:20.880
And if we call this takedamage function for the third time.

02:20.880 --> 02:22.480
So the maxhealth is now three.

02:22.480 --> 02:24.440
So three minus one is going to be two.

02:24.640 --> 02:28.520
And if we call this takedamage function for the fourth time.

02:28.520 --> 02:30.080
So the maxhealth is now two.

02:30.120 --> 02:32.160
So two minus one just going to be one.

02:32.400 --> 02:37.040
And if we call this a takedamage function for the last time or the fifth time.

02:37.040 --> 02:39.000
So the maxhealth is now one.

02:39.000 --> 02:41.120
So one minus one is going to be zero.

02:41.240 --> 02:46.840
So the max health of our player game object will at some point it's gonna hit zero.

02:47.040 --> 02:49.880
And let's suppose the max health is already zero.

02:49.880 --> 02:56.040
And if we call this takedamage function so the max is zero, so zero minus one is just going to be minus

02:56.040 --> 02:56.480
one.

02:56.480 --> 02:58.760
So it will move over to negative value.

02:58.760 --> 03:04.640
So before we go to negative value we need to check if the max health of our player game object is already

03:04.640 --> 03:05.160
zero.

03:05.240 --> 03:07.620
Then we need to stop taking damage.

03:07.620 --> 03:10.020
Or we need to stop taking damage.

03:10.020 --> 03:14.420
Or we basically don't need to take damage anymore.

03:14.540 --> 03:20.660
So before we subtract or, uh, basically take damage, we just want to check if max health.

03:20.660 --> 03:24.620
So we're going to check if this max health is lesser than or equal to zero.

03:24.740 --> 03:26.020
So in that situation.

03:26.020 --> 03:27.860
So let's make some curly brackets.

03:28.300 --> 03:32.420
Uh so in that situation what we're going to do, we're going to type this return.

03:32.420 --> 03:38.540
And we do know that after return, what ever line of code we're going to write it will not be execute

03:38.580 --> 03:43.140
means uh, this our max health of player game object will start at zero.

03:43.540 --> 03:46.580
And once it's zero then we just typing return.

03:46.580 --> 03:53.300
And we do know after return, whatever we're going to do will not be execute means our player Max health

03:53.300 --> 03:54.700
will start at zero.

03:54.820 --> 03:59.620
And let's suppose the max health of player game object is already zero by now.

03:59.620 --> 04:03.340
So in that situation we need to destroy our player game object.

04:03.340 --> 04:06.260
Basically player don't have any health so that's why.

04:06.460 --> 04:08.960
So let's make a Di function in order to do it.

04:08.960 --> 04:10.920
And I'm going to make in the bottom.

04:11.160 --> 04:13.920
And this function doesn't need to be public.

04:13.920 --> 04:16.240
So we can simply type void for function.

04:16.240 --> 04:18.000
And we have to name the function.

04:18.000 --> 04:22.320
So I'm going to call it something like type make first bracket and some curly brackets.

04:22.320 --> 04:28.520
So what we want to do inside this function we just want to destroy our player game object right away.

04:28.520 --> 04:33.240
But before we destroy, let's have a look like printing out a message in the console.

04:33.240 --> 04:35.640
Say something like player type, something like that.

04:35.640 --> 04:37.080
We're gonna print out.

04:37.080 --> 04:39.840
So for printing out, we're gonna type debug dot log.

04:40.000 --> 04:45.400
And we have to pass the string that we want to see underneath our console tab.

04:45.400 --> 04:50.000
So I'm going to pass this dot object dot name.

04:50.240 --> 04:53.760
And top of that I'm going to add a message something like diet.

04:53.760 --> 04:59.680
So we're gonna type this plus icon in order to add a custom message and make this quotation.

04:59.680 --> 05:03.840
And I'm going to say something like diet and then close that up with semicolon.

05:03.840 --> 05:07.320
So this dot game object basically is just referencing itself.

05:07.320 --> 05:09.420
And then we just accessing the name.

05:09.420 --> 05:11.060
So the name just gonna be clear.

05:11.060 --> 05:15.380
So this dot game object, basically this player game object just referencing itself.

05:15.500 --> 05:21.180
And by that we just accessing this name and we can see the name in the inspector is player.

05:21.180 --> 05:25.060
So this over here, this dot game object dot name just gonna be player.

05:25.060 --> 05:30.260
And top of that we just added a custom message style as you can see.

05:30.420 --> 05:35.300
So player diet message will be printed out whenever we're gonna call this die function.

05:35.740 --> 05:40.940
So we're going to call the die function in this update function in top of our update function.

05:41.380 --> 05:44.860
Because we do know that update function runs every single frame.

05:44.860 --> 05:49.900
So every single frame we're going to keep track of how much health the player game object currently

05:49.900 --> 05:50.260
have.

05:50.460 --> 05:54.460
And if the max cell is zero, then we're going to call this die function.

05:54.460 --> 05:58.500
And this die function is just going to print out this message in the console.

05:58.500 --> 05:59.660
So let's do that.

05:59.660 --> 06:02.300
So we're gonna move over to this update function.

06:02.300 --> 06:10.440
And top of this update function I'm going to check if if max health is max health is less than or equal

06:10.440 --> 06:11.080
to zero.

06:11.120 --> 06:14.520
If that is the case, then we're going to make some curly brackets.

06:14.520 --> 06:18.200
Means our player game object just loses all of his health.

06:18.240 --> 06:20.280
Then now we're going to call the type function.

06:20.280 --> 06:23.200
So let's call the die function here and make first bracket.

06:23.240 --> 06:25.160
Then close that up with semicolon.

06:25.320 --> 06:27.120
And what this function is doing.

06:27.160 --> 06:33.080
It's really printing out the message in the console player type, as you can see over here.

06:33.080 --> 06:36.440
And we're going to press down Ctrl S in order to save our script.

06:36.440 --> 06:37.960
And let's move to unity.

06:37.960 --> 06:42.440
So we also need to call the Takedamage function in order to take damage.

06:42.440 --> 06:46.880
Or else this player GameObject not gonna lose any of his health.

06:47.640 --> 06:51.040
So the die function not gonna call at all.

06:51.040 --> 06:53.880
So we also need to call the Takedamage function.

06:53.880 --> 07:00.520
But when we need to call that Takedamage function, once our enemy GameObject shoot on this player game

07:00.560 --> 07:06.360
object, then at that moment we're going to do and we do know we're doing inside our enemy script we're

07:06.360 --> 07:07.480
just printing out.

07:07.880 --> 07:09.120
Let me show you first.

07:09.120 --> 07:12.860
So if I select my console tab and then click on this play button in the top.

07:12.860 --> 07:14.660
In order to play our game.

07:14.660 --> 07:21.300
You will see as soon as the game is gonna start and this anime game objects start shooting on our player

07:21.340 --> 07:22.260
game object.

07:22.260 --> 07:28.020
So let's have a look on this and let's get closer to this anime game object.

07:28.700 --> 07:35.780
And we can see as soon as we get, uh, a message in the console says player takes damage just getting

07:35.780 --> 07:38.220
pop up instead of printing out message.

07:38.220 --> 07:43.620
What this player game object need to do this player game object need to take damage.

07:43.620 --> 07:44.460
So let's do that.

07:44.460 --> 07:49.340
So we're gonna get out of play mode, and let's double click on this message in order to move that line

07:49.380 --> 07:49.900
of code.

07:49.900 --> 07:52.540
And we do know we're doing inside this anime script.

07:52.540 --> 07:58.180
As you can see in the left bar, you can see and we're shooting out our way over here.

07:58.180 --> 08:01.020
So the if hit info is basically true.

08:01.020 --> 08:02.900
If that is the case, then what are we doing?

08:02.900 --> 08:04.780
We just printing out a message.

08:04.820 --> 08:11.740
If info basically true means the ray, uh, the enemy that shoot out the ray and the ray basically colliding

08:11.740 --> 08:12.660
with this player.

08:12.660 --> 08:15.240
So that's why that message is getting pop up.

08:15.240 --> 08:20.000
So instead of printing out message, this player game object need to take damage.

08:20.000 --> 08:22.960
So we need to call the Takedamage function right there.

08:22.960 --> 08:24.120
So let's do that.

08:24.120 --> 08:26.960
So instead of printing out message we're going to remove that.

08:26.960 --> 08:33.200
Firstly we're going to check if firstly we will check if hit info dot game object.

08:33.200 --> 08:35.600
Sorry we're going to use this transform.

08:35.760 --> 08:39.520
Then dot get component get component.

08:39.520 --> 08:42.400
And the component we're looking for is a player ship.

08:42.400 --> 08:46.280
So we're going to type this player and then make first brackets.

08:46.600 --> 08:53.600
And then and we will check if it's not equal to null and make some curly brackets.

08:53.880 --> 08:59.680
So what we basically have done, we do know if hit info is true basically means the ray really collided

08:59.680 --> 09:02.040
with this player game object.

09:02.040 --> 09:04.800
It means this player game object needs to take damage.

09:04.800 --> 09:09.080
But before we take damage, what we're doing, we're checking in this if condition.

09:09.080 --> 09:12.720
We're just going over to hit info dot transform and hit it.

09:13.000 --> 09:18.750
Hit info dot transform basically means the, uh, the, uh, the ray collided with the transform.

09:18.790 --> 09:24.830
Well, let's suppose the ray that this anime game object stood out and that ray just collided with this,

09:24.870 --> 09:26.150
uh, player game object.

09:26.150 --> 09:31.230
So hit info dot transform basically means the ray collided with the game object transform.

09:31.310 --> 09:35.030
So let, uh, the ray just gonna collide with this player game object.

09:35.030 --> 09:37.070
So what we're doing, we're just going over to.

09:37.110 --> 09:42.950
We're just taking this hit info parameter in order to extract the game object, the ray hit width.

09:42.950 --> 09:46.030
So the ray definitely gonna hit with this player game object.

09:46.030 --> 09:51.070
So what we're doing we're just going over to this player game object transform component, as you can

09:51.070 --> 09:52.350
see in the inspector.

09:52.350 --> 09:57.310
And by this transform component we just getting this player skip as you can see.

09:57.310 --> 09:59.430
So we just taking the player skip.

09:59.430 --> 10:03.030
And we're checking if the player is skip is not equal to null.

10:03.070 --> 10:07.670
Basically means the player is skip is really attached to this player game object.

10:07.670 --> 10:13.790
And we definitely know that this player skip we can see in the inspector is attached to this player

10:13.830 --> 10:18.090
game object means the player game object is attached to our player game object.

10:18.130 --> 10:21.370
Firstly, we're checking if the ray really collides with the GameObject.

10:21.370 --> 10:24.330
That GameObject has a script called player.

10:24.330 --> 10:27.410
So the ray definitely gonna collide with this player game object.

10:27.410 --> 10:32.690
So basically we're just going over to this GameObject or this player game object Transform.

10:32.690 --> 10:35.930
And by this transform component we just getting the player script.

10:35.930 --> 10:42.090
And we're checking that if this player script is not equal to null, basically means this player script

10:42.130 --> 10:43.930
attached to this player game object.

10:43.930 --> 10:49.810
As you can see, indeed this player game a player script is attached to our player game object.

10:49.810 --> 10:54.650
So now what we have to do, we need to call the Takedamage function from this player script.

10:54.650 --> 10:58.890
And we do know we made the Takedamage function inside this player script.

10:58.890 --> 11:05.050
So we're going to call the Takedamage function right over here because the script attached to our player

11:05.090 --> 11:12.010
GameObject and the really colliding the with this player GameObject and hit info dot transform basically

11:12.010 --> 11:13.490
means the ray.

11:13.810 --> 11:14.090
The.

11:14.730 --> 11:19.070
Basically it's basically means the transform From the ray collided with.

11:19.110 --> 11:22.630
So the ray is going to collide with our player game object.

11:22.630 --> 11:26.070
So hit info dot transform basically means player transform.

11:26.070 --> 11:29.670
So basically we just moving over to this player game object Transform.

11:29.670 --> 11:32.910
And by this transform we just are checking this player skip.

11:32.910 --> 11:36.510
And we're checking if the player is skip is not equal to null.

11:36.550 --> 11:40.670
Basically means this player script is attached to this player game object.

11:40.670 --> 11:45.270
And we can indeed see the player skip is attached to this player game object.

11:45.270 --> 11:50.790
So in this situation what it's need to do, our player game object need to take that damage.

11:50.790 --> 11:55.790
So what we're going to do we're going to use this hit info parameter one more time to extract info.

11:55.990 --> 12:00.150
And we will go to hit Info Transform and Transform.

12:00.390 --> 12:03.390
And then we're going to get the component that we're looking for.

12:03.390 --> 12:04.430
So get component.

12:04.430 --> 12:07.230
And the component we're looking for is player script.

12:07.230 --> 12:08.630
So we're going to pass player.

12:08.630 --> 12:10.590
And you need to make this first packet.

12:10.590 --> 12:15.630
And now we're going to call the function over here that we made inside our player script.

12:15.630 --> 12:18.310
So the function we made is take damage function.

12:18.310 --> 12:19.970
So let's call the function here.

12:19.970 --> 12:23.210
So we're going to type dot take damage and make first bracket.

12:23.210 --> 12:25.330
And then close that up with semicolon.

12:25.570 --> 12:32.250
So uh so what we need to do now we need to pass the argument right over here.

12:32.250 --> 12:32.610
We do.

12:32.650 --> 12:35.810
No we just pass damage parameter in damage.

12:35.810 --> 12:39.490
And now we can choose the number that we would like to go with.

12:39.490 --> 12:45.090
So let's suppose you just pass right over here instead of one you just pass five.

12:45.210 --> 12:48.810
So the player game object just gonna lose all of his health right away.

12:48.810 --> 12:52.730
Because we have given to this player game object five unit of health.

12:52.730 --> 12:56.410
As you can see in the inspector underneath our player Skip.

12:56.810 --> 13:00.370
But I just want to, uh, subtract uh, every single time.

13:00.370 --> 13:01.250
One unit.

13:01.250 --> 13:05.650
So that's why as a damage parameter uh, over here I'm going to pass one.

13:05.650 --> 13:13.130
So, uh, enemy need to, uh, shoot on our player game object five times in order to destroy our player

13:13.170 --> 13:17.410
game object, or we just want to destroy our player game object.

13:17.410 --> 13:18.450
So it's enemy.

13:18.450 --> 13:19.130
Need to take.

13:19.290 --> 13:26.150
Enemy need to fire on our player GameObject five times, and if you just pass over here five instead

13:26.150 --> 13:34.110
of one, then enemy just gonna enemy GameObject just gonna destroy our player GameObject at one shooting,

13:34.110 --> 13:35.230
but we don't want that.

13:35.230 --> 13:38.790
So that's why as in damage parameter, I just pass one.

13:38.790 --> 13:41.470
So let me explain what we basically have done.

13:41.710 --> 13:45.870
We just going over to a player GameObject transform as you can see.

13:45.870 --> 13:51.990
So hit info dot transform basically means the player transform or the collided with the transform.

13:51.990 --> 13:55.150
So the ray definitely gonna collide our player GameObject.

13:55.150 --> 13:59.510
So hit info transform basically means right over here player transform.

13:59.510 --> 14:02.710
And then we just getting the player script as you can see.

14:02.710 --> 14:06.870
So basically we're just going over to this player GameObject transform.

14:06.870 --> 14:09.430
And then we just moving over to this player scape.

14:09.430 --> 14:13.630
And we do know inside this player scape we just made the Takedamage function.

14:13.630 --> 14:16.750
So we're calling the function over here as you can see.

14:16.750 --> 14:19.430
So firstly we're just moving over to this player script.

14:19.430 --> 14:24.970
And then we just calling this Takedamage damage function and we do not take damage function is really

14:24.970 --> 14:27.570
doing is just subtracting from our max health.

14:27.570 --> 14:31.170
With this amount of damage as a damage parameter, we just pass.

14:31.170 --> 14:38.290
One means our enemy game object need to shoot on our player game object five times in order to destroy

14:38.330 --> 14:43.690
our player game object, or in order to lose all of our health player game object.

14:44.490 --> 14:45.930
Basically, that is the thing.

14:46.090 --> 14:49.850
And now we're going to press down control S in order to save our script.

14:49.850 --> 14:51.770
And now let's move over to unity.

14:52.370 --> 14:58.570
So we will see this enemy game object need to shoot on this player game object five times in order to

14:58.570 --> 15:01.010
destroy our player game object.

15:01.010 --> 15:03.090
So let's have a look on this by now.

15:03.730 --> 15:05.330
So just wait for to compile.

15:05.490 --> 15:11.490
And if we just click on this play button in the top and uh, take a look on this, you will see.

15:11.650 --> 15:17.890
So this enemy game object need to shoot on our player game object five times in order to destroy our

15:17.890 --> 15:19.250
player game object.

15:20.010 --> 15:22.290
So let's have a look on this now.

15:23.010 --> 15:25.670
So enemy just gonna shoot for first time.

15:25.670 --> 15:32.310
As you can see, as soon as shooting and the max health, we can see it just go gradually going down

15:32.310 --> 15:34.230
and the max hell is now zero.

15:34.230 --> 15:40.750
And the health now just get stuck at zero, as you can see, because we are checking if the max health

15:40.750 --> 15:46.510
is already zero, then we're not doing at all, uh, at all, anything like that.

15:46.510 --> 15:47.750
So that is the reason.

15:47.750 --> 15:49.270
I hope you get the idea.

15:49.270 --> 15:51.270
And we need to get out of play mode.

15:51.270 --> 15:56.030
So now what we have to do, we need to destroy this player game object right away.

15:56.030 --> 16:02.070
But before we do, we need to make a script in order to check if the game is active or not.

16:02.110 --> 16:04.350
But we're gonna do in the next lecture.

16:04.350 --> 16:10.830
I hope you get the idea that how you can do it, uh, we basically have done the same thing as we have

16:10.830 --> 16:13.710
done in the inside this anime game object.

16:13.710 --> 16:19.870
We just made the Takedamage function and also the die function in order to damage our player as well

16:19.870 --> 16:25.910
as we have the, uh, we have done the same thing in this player game object as well.
