WEBVTT

00:00.040 --> 00:02.160
We also need to destroy our arrow.

00:02.200 --> 00:05.000
Once it hits our player game object.

00:05.000 --> 00:07.160
So let's open up our arrow script.

00:07.160 --> 00:12.720
So for to do that I will head over to my project tab assets and then prefabs folder.

00:12.720 --> 00:14.000
And let's select this arrow.

00:14.000 --> 00:15.520
Let's go over to Inspector.

00:15.520 --> 00:19.800
And let's double click on this arrow script to open it up in Visual Studio.

00:19.960 --> 00:23.160
For that we're gonna use Ontriggerenter 2D function.

00:23.160 --> 00:29.680
Because if we move over to unity and select this arrow, we can see and then move over to this box Collider

00:29.680 --> 00:30.120
2D.

00:30.160 --> 00:32.480
We can see that uh, is triggered.

00:32.480 --> 00:33.920
We just set it to checked.

00:33.920 --> 00:40.040
So that's why we're going to use Ontriggerenter 2D function in order to check if that arrow just triggered

00:40.040 --> 00:42.200
with this player game object or not.

00:42.400 --> 00:46.080
And also make sure you assign your player game object as a player tag.

00:46.080 --> 00:49.600
If you haven't, then select your player game object in the hierarchy.

00:49.640 --> 00:52.280
Then go over to Inspector any tag.

00:52.320 --> 00:56.920
As you can see it says untagged, so just click there and then click on this player.

00:56.960 --> 00:59.920
Now we assign it this player as a player tag.

00:59.960 --> 01:02.360
As you can notice in the inspector.

01:02.560 --> 01:06.530
And make sure you also move over to overrides and then apply all.

01:06.970 --> 01:09.690
And now we're going to open up our arrow script.

01:09.690 --> 01:14.170
So let's head over to Arrow Script and let's use this on trigger.

01:14.650 --> 01:19.410
On trigger enter enter 2D function.

01:20.290 --> 01:29.130
Sorry we need to use this on trigger on trigger trigger enter 2D function.

01:29.130 --> 01:32.130
So this function get called when we trigger with something.

01:32.130 --> 01:33.930
And we can extract information.

01:33.930 --> 01:35.330
What we trigger it with.

01:35.370 --> 01:41.210
By going over to this collision a parameter we can extract information like name of the game object.

01:41.210 --> 01:45.210
We trigger it with the tag of the game object, anything we like.

01:45.210 --> 01:47.690
So over here what we're gonna do we're going to check.

01:47.690 --> 01:51.770
So let's make if condition some first bracket and some curly brackets.

01:51.770 --> 01:53.250
And what we want to check.

01:53.290 --> 01:57.530
We basically want to check if we trigger with that player game object or not.

01:57.530 --> 02:03.730
So we're going to use this collision dot game object and collision dot object over here basically means

02:03.730 --> 02:06.830
the game object we trigger it Wait, so the arrow.

02:06.870 --> 02:07.590
Definitely.

02:07.630 --> 02:10.230
Definitely gonna trigger with that player game object.

02:10.230 --> 02:14.430
So collision dot GameObject over here basically means the player game object.

02:14.430 --> 02:17.350
And now what we're gonna do, we're gonna access the tag of it.

02:17.350 --> 02:20.910
And we will check if the tag is equals equals to player.

02:21.430 --> 02:22.590
If that is the case.

02:22.590 --> 02:28.390
And we do know that we assign it this player as a player tag as you can notice in the inspector.

02:28.390 --> 02:30.750
So we definitely gonna trigger with that.

02:30.750 --> 02:36.910
So what we need to do now we need to destroy our arrow as soon as it's trigger with this player game

02:36.950 --> 02:37.670
object.

02:37.670 --> 02:40.030
But before we do also we need to destroy.

02:40.470 --> 02:43.230
We basically need to damage our player.

02:43.230 --> 02:47.070
For that, we're gonna print out a message something like debug dot log.

02:47.430 --> 02:53.990
And we say, we're gonna say in quotation something like damage, uh, damage and the name of the player.

02:53.990 --> 02:55.830
So I'm going to use this plus icon.

02:55.830 --> 03:01.390
And top of that I'm going to use this collision dot GameObject dot name.

03:01.670 --> 03:07.910
So top of our this message damage, we just adding the name of the game object we trigger it with.

03:07.910 --> 03:08.120
Wait.

03:08.240 --> 03:09.800
So the message should be pop up.

03:09.800 --> 03:11.120
So this will be damaged.

03:11.160 --> 03:15.320
Collision GameObject basically means player and we accessing the name.

03:15.320 --> 03:16.680
So the name is player.

03:16.880 --> 03:18.960
So this over here becomes player.

03:18.960 --> 03:21.720
So the message should pop up player damage.

03:21.760 --> 03:22.800
Player takes damage.

03:22.800 --> 03:32.440
So let's simply say something like player uh sorry uh player uh let's say takes damage.

03:33.080 --> 03:41.720
Uh, or we can simply say, let's put this damage, uh, put this damage over here.

03:41.920 --> 03:45.280
So let's use this plus icon in order to add the message.

03:45.280 --> 03:49.360
And we're going to say takes damage, takes damage.

03:49.360 --> 03:51.560
And let's close that up with semicolon.

03:51.560 --> 03:52.840
So this will be player.

03:52.840 --> 03:59.160
So the message should be pop up in our console tab should be something like this player takes damage.

03:59.160 --> 04:02.040
And after that we want to destroy our arrow.

04:02.040 --> 04:07.240
So destroy we're going to use this destroy function and destroy function as a first argument.

04:07.240 --> 04:09.760
It's going to take the game object we want to destroy.

04:10.000 --> 04:13.850
So this arrow script is attached to our arrow game object.

04:13.850 --> 04:20.450
So we want to destroy that arrow game object basically means the game objects are a game object itself.

04:20.450 --> 04:23.490
So that's why we're going to pass this this dot game object.

04:23.490 --> 04:25.930
And then we're going to close that up with semicolon.

04:25.930 --> 04:31.370
So the game object will be destroyed right away because we didn't given any second argument.

04:31.370 --> 04:34.570
As you can see we didn't as specified.

04:34.850 --> 04:38.530
So we just destroying our arrow game object right away.

04:38.570 --> 04:44.090
Or you can simply say immediately after printing out this message says player takes damage.

04:44.090 --> 04:48.450
So now press down Ctrl S to save our script and let's head over to unity.

04:48.450 --> 04:53.250
So as soon as our arrow is just going to trigger with this player game object, the message just going

04:53.290 --> 04:59.610
to pop up in our console tab, say something like player takes damage and after that that arrow game

04:59.650 --> 05:05.170
object just going to destroy itself right after triggering with this player game object.

05:05.170 --> 05:08.050
So let's have a look on this and you will see that.

05:08.050 --> 05:12.650
So just wait a little moment and once it's done we can see that.

05:15.580 --> 05:17.540
So just wait a little moment.

05:28.540 --> 05:31.740
It's gonna take a few seconds to compile our script.

05:32.020 --> 05:37.540
Once it's complete, we can just go ahead and play our game in order to check that out.

05:37.860 --> 05:39.820
So just wait a little moment.

05:40.820 --> 05:41.740
It's, uh.

05:42.260 --> 05:47.020
So basically, I have low system, so that's why it's taking a little bit longer time.

05:47.260 --> 05:50.060
So now, as you can see, the compilation is done.

05:50.100 --> 05:51.820
I'm going to select my console tab.

05:51.820 --> 05:55.740
And let's click on this play button in the top in order to play our game.

05:55.820 --> 06:01.340
So as soon as we're gonna get attack range of this enemy game object, you will see this enemy just

06:01.340 --> 06:06.100
gonna shoot out ray array on top of this player game object.

06:06.100 --> 06:07.140
So let's have a look.

06:07.260 --> 06:13.740
So if I get closer to this, uh, as you can see, as soon as our arrow just hitting our player game

06:13.780 --> 06:18.800
object, it just destroying itself, as well as the message in our console tab.

06:18.800 --> 06:24.400
Just getting pop up says player takes damage, since the name of the game object is player.

06:24.400 --> 06:28.360
As you can notice in the hierarchy as well as in the inspector.

06:28.360 --> 06:31.800
So I'm going to get out of play mode, so I hope you get the idea.

06:31.800 --> 06:36.600
So we're going to leave the message for now, because we're going to use that later in order to damage

06:36.600 --> 06:37.440
our player.

06:37.600 --> 06:40.680
So we also need to give some health amount to this.

06:41.120 --> 06:45.760
You can simply say this enemy game object in order to take damage.

06:46.080 --> 06:47.840
So I hope you get the idea.

06:47.880 --> 06:52.800
Now I'm going to press Ctrl s on my keyboard in order to save my project.

06:52.920 --> 06:58.240
And if you change anything inside this player, game object, or enemy one, make sure you just go over

06:58.240 --> 07:01.040
to Inspector Overrides and then apply all.

07:01.320 --> 07:04.360
And also you make sure inside this player as well.

07:04.920 --> 07:09.680
And after that you can simply press down Ctrl S in order to save your project.

07:09.680 --> 07:15.400
Or you can simply move over to top left file and then click on the save button in order to save your

07:15.400 --> 07:16.200
project.

07:16.200 --> 07:19.040
So I hope you get the idea that how you can do it.
