WEBVTT

00:00.080 --> 00:01.560
Let's code our game.

00:01.560 --> 00:06.720
Basically, once this player game object collides with this heart, it need to be collected.

00:06.720 --> 00:10.600
Also, this heart need to play the heart collected animation clip.

00:10.760 --> 00:12.120
So we're going to do that.

00:12.120 --> 00:18.960
So before we do, uh, if we select our heart sprite and then move over to animator tab, we can see

00:18.960 --> 00:22.120
we have two animation heart idle and Heart Collected.

00:22.120 --> 00:24.680
And we need to trigger this collide parameter.

00:24.680 --> 00:29.360
And we do know once we trigger it's going to play the heart collected animation.

00:29.360 --> 00:32.720
And we're going to trigger this collide parameter through our code.

00:32.760 --> 00:36.720
Once our player GameObject collect this heart game object.

00:36.720 --> 00:38.040
So we're going to do that.

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

00:40.920 --> 00:43.840
And let's open up the player script by double clicking on it.

00:44.560 --> 00:49.000
And we're going to use Ontriggerenter 2D function basically this one.

00:49.000 --> 00:51.640
So this function get called when we trigger with something.

00:51.680 --> 00:58.640
And you can just ignore these things over here and make sure you just include this function in your

00:58.640 --> 01:00.370
class which is On trigger.

01:00.410 --> 01:04.610
Enter 2D function and this function get called when we trigger with something.

01:04.610 --> 01:10.850
So if I select my heart gameobject in the hierarchy, then move over to Inspector Polygon Collider 2d.

01:10.890 --> 01:13.770
We can see we just take this trigger box.

01:13.770 --> 01:17.930
So that's why we're going to use that on trigger enter 2D function.

01:17.930 --> 01:22.890
So that function get called when we collide with something on when we trigger with something.

01:22.890 --> 01:27.690
So let's suppose this player game object trigger with this heart game object.

01:28.250 --> 01:31.370
Then this function over here just gonna get trigger.

01:31.490 --> 01:35.850
And we can extract extract information about what we hit with.

01:36.010 --> 01:41.770
So by moving over to this collision parameter we can extract info something like a name of the game

01:41.810 --> 01:42.170
object.

01:42.170 --> 01:47.210
We trigger it with the tag of the game object or the component that we're looking for.

01:47.210 --> 01:50.130
So we can just extract info about that.

01:50.130 --> 01:55.610
So what we're going to do now, uh, basically we're gonna check if we trigger with this heart and if

01:55.610 --> 01:57.850
this game object has a tag called heart.

01:57.850 --> 02:00.910
And if we select our heart GameObject in the hierarchy.

02:00.950 --> 02:05.030
We can see in the Inspector tag, we assign it as a heart attack.

02:05.190 --> 02:07.870
So we're going to trigger with this trigger with this.

02:07.870 --> 02:14.230
So in that situation what we need to do we need to increase our health amount of this player game object

02:14.230 --> 02:15.470
by one unit.

02:15.470 --> 02:20.510
And after that we need to play the heart animation heart collected animation clip.

02:20.710 --> 02:26.110
And also we need to destroy that heart heart game object once we collect that.

02:26.110 --> 02:26.950
So let's do that.

02:26.950 --> 02:28.670
So we're going to move over to script.

02:28.670 --> 02:30.470
And over here we're going to check.

02:30.470 --> 02:33.470
So this function get called when we trigger with something.

02:33.670 --> 02:39.990
And what we're going to do over here we're going to check if we make some first bracket and some curly

02:39.990 --> 02:42.750
brackets and what we want to check over here.

02:42.750 --> 02:45.710
We just want to check the game object we triggered it with.

02:45.750 --> 02:49.270
We can we're going to extract the tag of the game object.

02:49.270 --> 02:52.710
So we're going to use this collision parameter in order to do that.

02:52.710 --> 02:54.870
So let's go to collision parameter.

02:54.870 --> 02:58.750
So I'm going to use this collision dot sorry.

02:59.360 --> 03:03.240
We're going to use this collision dot game object.

03:03.400 --> 03:05.640
Sorry I'm really apologize for it.

03:05.800 --> 03:07.480
Game object dot tag.

03:07.840 --> 03:11.000
And we're going to check if it's equals equals to heart.

03:11.040 --> 03:14.720
If that is the case means we're just colliding with the heart.

03:15.080 --> 03:18.000
Uh, means you need to put inside this quotation.

03:18.000 --> 03:19.840
So make sure you just do that.

03:19.840 --> 03:24.080
So collision dot game object basically means the game object we created with.

03:24.080 --> 03:29.360
So if this player game object trigger with this heart game object or collide with this heart game object.

03:29.360 --> 03:33.600
So the collision game object right there, this heart game object.

03:33.600 --> 03:35.160
And what are we doing after that.

03:35.160 --> 03:38.160
We just extracting the tag of the game object.

03:38.160 --> 03:43.280
So we basically moving over to this heart game object and then accessing the tag of it.

03:43.280 --> 03:47.680
So we can see we assign it with heart tag, as you can see in the inspector.

03:47.680 --> 03:51.360
And we're checking if the tag is equals equals to heart.

03:51.360 --> 03:54.920
And definitely we're going to trigger with that heart game object.

03:54.920 --> 03:57.600
And we assign it that heart as a heart tag.

03:57.640 --> 03:59.610
As you can see in the inspector.

03:59.610 --> 04:02.770
So we're definitely gonna trigger with this heart game object.

04:02.770 --> 04:08.730
So in this situation, what we need to do, we need to increase our player max health by one unit.

04:08.730 --> 04:12.930
And after that this heart game object need to play the heart animation clip.

04:12.970 --> 04:14.170
Sorry not heart.

04:14.210 --> 04:17.490
The collected animation clip of this heart game object.

04:17.490 --> 04:18.650
So let's do that.

04:18.650 --> 04:22.610
So we're gonna are just collecting over here as you can see.

04:22.730 --> 04:25.090
So we're checking the tag of the game object.

04:25.090 --> 04:29.930
If it's equals equals to heart means we basically trigger it with heart game object.

04:29.930 --> 04:32.330
So now we're going to increase our max health.

04:32.450 --> 04:34.650
So the max health we set by default.

04:35.570 --> 04:40.410
Uh you can see we have it over here as you can see.

04:40.410 --> 04:42.930
So we're gonna increase this max health by one unit.

04:42.930 --> 04:44.050
So let's do that.

04:44.530 --> 04:46.770
So we're going to do it over here.

04:47.410 --> 04:49.490
So the max cell so max cell.

04:49.490 --> 04:50.890
And top of that I'm going to add.

04:50.890 --> 04:53.970
So we're going to use this plus equal in order to add.

04:53.970 --> 04:56.570
And we're going to add one unit of health.

04:56.570 --> 04:57.870
So I'm going to put one.

04:58.230 --> 04:59.950
And after that, what we need to do.

04:59.990 --> 05:02.830
We also need to play the hurt animation clip.

05:02.990 --> 05:06.270
The collected animation clip of this hurt game object.

05:06.270 --> 05:13.070
So we we do know that if we select our hardest sprite in sprite, we can see a animator component is

05:13.070 --> 05:15.910
attached to this sprite game object.

05:15.910 --> 05:20.550
So basically what we have to do, we need to move over to this hurt game object first.

05:20.550 --> 05:25.230
And by this we're going to move over to this transform of this hard game object.

05:25.230 --> 05:31.030
And by this transform reference we're going to access the first child so hard sprite right over here.

05:31.030 --> 05:33.870
It's going to be the first child or the first.

05:34.070 --> 05:36.710
So the index is just gonna be zero.

05:37.230 --> 05:43.670
And uh, and we do know that, uh, animator component is attached to this sprite game object, not

05:43.670 --> 05:45.150
this hard game object.

05:45.150 --> 05:51.430
So that's why we need to move over to this sprite game object or the or the child of this hard game

05:51.470 --> 05:52.070
object.

05:52.070 --> 05:55.870
Then we're gonna move over to this animator component of this hard sprite.

05:55.950 --> 06:01.080
And by getting this animator component, we're going to trigger this collider parameter.

06:01.080 --> 06:06.440
And we do know once we collide it's going to play the heart collected animation clip right away.

06:06.520 --> 06:07.560
So let's do that.

06:07.640 --> 06:11.520
So over here what we're going to do we're going to use this collision.

06:11.920 --> 06:18.120
So we will go to collision dot GameObject means we basically move over to that basically this heart

06:18.160 --> 06:18.880
game object.

06:18.880 --> 06:23.640
And now what we're going to do we're going to access the child of this heart game object because the

06:23.680 --> 06:28.120
animator component is attached to this heart sprite, not this heart game object.

06:28.120 --> 06:31.040
So you can see in the inspector as well.

06:31.480 --> 06:33.760
So that's why we're going to access the first child.

06:33.960 --> 06:36.520
So we will move over to transform.

06:36.520 --> 06:39.560
And then we're going to use this dot get child function.

06:39.560 --> 06:42.200
And over here get child function in brackets.

06:42.200 --> 06:44.320
It's going to take the index of the child.

06:44.480 --> 06:50.680
So the first child so hard sprite we can see we have only one child of this heart game object.

06:50.920 --> 06:53.680
And the first child is just going to be index zero.

06:53.680 --> 06:58.050
So if you just pass right over here zero in this gate cell.

06:58.050 --> 07:00.130
So it's going to reference the first cell.

07:00.130 --> 07:06.610
And if you let's suppose pass one what basically means the second child or the second child index is

07:06.610 --> 07:07.610
going to be one.

07:07.610 --> 07:11.250
But we don't have any second child as you can see.

07:11.290 --> 07:15.970
And the animator component is attached to the first first child.

07:15.970 --> 07:21.410
So that's why in this brackets I'm going to pass zero zero basically referencing the first child.

07:21.450 --> 07:25.330
And after that what we're going to do, we're going to go to that sprite game object.

07:25.330 --> 07:27.170
So we're going to type this game object.

07:27.170 --> 07:31.570
And now we're gonna move over to transform sorry animator component.

07:31.570 --> 07:34.250
So we're going to use this dot get component function.

07:34.930 --> 07:37.810
Not sorry this is not the function.

07:38.210 --> 07:40.930
Or you can simply say uh sorry.

07:40.970 --> 07:43.490
It's uh basically use method group.

07:43.490 --> 07:46.810
So you can see uh what what basically is this.

07:47.090 --> 07:53.210
So over here what we need to do, we need to pass the component that we're looking for.

07:53.210 --> 07:56.590
So the component we're looking for is a animator component.

07:56.590 --> 07:57.990
So firstly, what are we doing.

07:57.990 --> 08:00.710
We just moving over to this hard GameObject first.

08:00.710 --> 08:04.230
So collision dot GameObject basically means the hard GameObject.

08:04.230 --> 08:06.910
And after that we just moving over to transform.

08:06.910 --> 08:12.310
So firstly we just moving over to this hard GameObject, then this transform component of this hard

08:12.310 --> 08:13.070
GameObject.

08:13.070 --> 08:19.510
And by this transform we just accessing the child of this hard GameObject, which is hard sprite as

08:19.510 --> 08:20.270
you can see.

08:20.310 --> 08:25.990
And after that what we're doing, we're just going over to this sprite and then we just moving uh,

08:26.030 --> 08:30.990
we basically just getting this animator component, as you can see in the inspector.

08:30.990 --> 08:34.790
So in this arrow we need to pass the component that we're looking for.

08:34.830 --> 08:38.350
So we're going to pass animator component animator component.

08:38.350 --> 08:40.030
And we're going to make first bracket.

08:40.030 --> 08:45.910
And now we're going to use dot set trigger function in order to trigger that collide parameter.

08:45.910 --> 08:48.310
So we're going to use this set trigger function.

08:48.310 --> 08:53.950
And this sorry and uh dot set trigger function.

08:53.950 --> 08:59.600
And this trigger function in the first bracket in basically the first bracket is in quotation.

08:59.600 --> 09:04.600
It's going to take the parameter name or the parameter that you want to track a trigger.

09:04.600 --> 09:07.080
So we using this trigger function basically.

09:07.120 --> 09:07.560
Why.

09:07.720 --> 09:13.920
Because this parameter in animator tab we can see this is a trigger parameter.

09:13.920 --> 09:16.760
So that's why we need to use this trigger function.

09:16.760 --> 09:17.760
And this bracket.

09:17.760 --> 09:19.960
It's going to take the parameter name.

09:19.960 --> 09:23.440
So make sure you type quotation and spell it correct.

09:23.440 --> 09:26.040
Basically you need to pass the parameter name.

09:26.040 --> 09:27.680
So in my case collide.

09:27.680 --> 09:30.000
And make sure you spelling it correct.

09:30.000 --> 09:32.760
And you can check that by moving over to unity.

09:32.920 --> 09:34.800
And in animator tab you can check.

09:34.800 --> 09:41.120
So I can see the C is capitalized and all other letter is small letters.

09:41.120 --> 09:44.000
So make sure you just type spell it correct.

09:44.000 --> 09:46.800
And after that we're going to close that up with semicolon.

09:46.800 --> 09:50.320
So firstly we're going to collision dot GameObject basically means heart.

09:50.320 --> 09:52.240
And then going over to transform.

09:52.240 --> 09:57.420
And by this transform which is going over to the first tile and then we're just getting the animator

09:57.420 --> 09:58.180
component.

09:58.180 --> 10:00.820
Then we're just triggering the collide parameter.

10:00.820 --> 10:06.340
So basically we're moving over to this hard GameObject first and this hard, uh, GameObject transform,

10:06.340 --> 10:11.820
which is accessing the first tile or which is moving over to this sprite game object, or the first

10:11.820 --> 10:13.580
child of this hard GameObject.

10:13.580 --> 10:19.980
And after that, we're just getting this animator component, as you can see in the code over here.

10:20.300 --> 10:23.340
Basically we're just going over to this animator component.

10:23.340 --> 10:27.380
And by this animator component we're just triggering this collide parameter.

10:27.420 --> 10:29.540
You can see in the animator tab.

10:29.540 --> 10:34.780
And we can also notice in our code over here which is triggering the collide parameter.

10:34.780 --> 10:37.860
So I'm going to press down Ctrl S to save my script.

10:37.900 --> 10:39.780
And let's move to unity by now.

10:39.780 --> 10:43.860
And let's have a look on this if it's perfectly working or not.

10:43.860 --> 10:45.780
So we're going to take a look on it.

10:53.020 --> 10:59.870
So just wait for to compile and once it's done, we are ready to play our game and we will see our max

10:59.870 --> 11:02.550
health just going to increase by one unit.

11:03.190 --> 11:08.830
And I'm going to drag away my enemy a little bit further away for testing purposes now.

11:08.990 --> 11:12.030
So I'm going to select my enemy two GameObject in the hierarchy.

11:12.030 --> 11:18.830
And I'm going to drag it a little bit far away from my player GameObject something uh, somewhere over

11:18.830 --> 11:19.270
here.

11:19.270 --> 11:20.870
And this seems pretty much fine.

11:21.350 --> 11:27.550
And if I select my player game object in the inspect hierarchy in the Inspector, we can see underneath

11:27.550 --> 11:29.750
our player skip max health is five.

11:29.750 --> 11:34.550
So once we collect this one or collide with this hurt, we're going to see this max health just going

11:34.550 --> 11:36.710
to increase by one unit.

11:36.710 --> 11:38.910
So let's click on this play button in the top.

11:39.230 --> 11:43.030
And we're using plus equal sign in order to uh add.

11:43.030 --> 11:49.230
So you you you just do that uh plus equal one which just gonna increase.

11:49.230 --> 11:57.240
So let's collect this heart game object And as soon as we collect it, as you can see, it just splits

11:57.240 --> 11:57.880
the heart.

11:57.920 --> 11:58.960
Animation clip.

11:59.120 --> 12:02.560
And after that we can see the max health of our player.

12:02.560 --> 12:06.720
So I'm going to click on this pause button in the top in order to pause my game.

12:06.880 --> 12:10.440
And we can see our player Max health is now six.

12:10.440 --> 12:12.720
So basically that is working fine.

12:12.720 --> 12:19.760
But the problem is that if we move over to hierarchy and if we just take a look, we can still see the

12:19.760 --> 12:25.280
hard game object is laying around in the scene view as well as in the hierarchy.

12:25.280 --> 12:31.240
So after collecting this, what we basically need to do, we need to destroy this hard game object right

12:31.240 --> 12:33.200
away because we collect it.

12:33.200 --> 12:39.000
But uh, instead of destroying this hard game object right away, what are we going to do?

12:39.040 --> 12:41.920
We're gonna destroy it after some period of time.

12:41.960 --> 12:47.320
Basically, uh, once we played the collected animation clip 100%.

12:47.360 --> 12:51.520
After that amount of time, we're gonna destroy our hard game object.

12:51.520 --> 12:53.780
We're not gonna destroy it right away.

12:53.940 --> 12:58.700
So firstly, we need to play the hard collected animation clip as basically it's just playing.

12:58.700 --> 13:01.940
It's going up and over time it's just fading out.

13:01.940 --> 13:04.060
So we're going to play that animation clip.

13:04.060 --> 13:09.140
And after completing that animation clip of that hard game object, we're going to destroy that.

13:09.140 --> 13:10.820
So let's get out of play mode.

13:10.980 --> 13:15.780
And if I select my hard GameObject in the hierarchy, then move over to animation tab.

13:16.340 --> 13:23.180
Uh, sorry, we need to select the child because the we created the animation clip a child of this hard

13:23.180 --> 13:23.660
game object.

13:23.660 --> 13:26.740
So we're going to select our hard sprite or the child game object.

13:26.900 --> 13:32.580
And if we move over to animation tab and click over here and I'm going to choose the hard collected.

13:32.580 --> 13:36.860
And we can see this animation we completing 60 frame.

13:36.860 --> 13:42.260
Or you can simply say one second which is completing the whole uh hard game object.

13:42.260 --> 13:48.140
So sorry, not whole uh, in one second we just completing the whole animation clip.

13:48.540 --> 13:50.100
Sorry, not game object.

13:50.300 --> 13:54.390
So we just completing the whole animation clip after one second.

13:54.550 --> 13:58.910
So after this amount of time, we're going to destroy our hard game object.

13:58.910 --> 14:03.470
So it's not going to lay around in the hierarchy as well as in the scene view.

14:03.470 --> 14:04.470
So let's do that.

14:04.470 --> 14:06.070
So we're going to move over to script.

14:06.070 --> 14:09.510
And after playing the animation clip we're going to destroy that.

14:09.510 --> 14:11.510
So we're going to use this destroy function.

14:11.510 --> 14:16.750
And we do know it's going to take as a first argument what game object you want to destroy.

14:16.790 --> 14:19.110
So I'm going to pass this dot game object.

14:19.150 --> 14:19.670
Sorry.

14:19.710 --> 14:21.670
Not going to pass this dot game object.

14:21.910 --> 14:22.910
This dot game object.

14:22.910 --> 14:25.350
Basically it's just referencing itself.

14:25.590 --> 14:27.070
So it's going to be player.

14:27.070 --> 14:29.470
But we don't want to destroy our player.

14:29.470 --> 14:32.070
Instead we're gonna destroy that hard game object.

14:32.070 --> 14:37.190
So we're going to pass this collision dot game object and collision dot game object basically means

14:37.190 --> 14:39.110
the game object we trigger it with.

14:39.110 --> 14:41.470
So we trigger it with this hard game object.

14:41.470 --> 14:45.430
So collision dot GameObject right there gonna be this hard game object.

14:45.750 --> 14:48.430
And you can just close that up with semicolon.

14:48.430 --> 14:50.830
But it will destroy right away.

14:50.830 --> 14:54.680
So that's why as a second argument we're going to specify our time.

14:54.800 --> 14:59.440
And if you just leave like this it's going to destroy the game object right away.

14:59.440 --> 15:01.320
And we don't want to do that.

15:01.320 --> 15:06.840
So that's why as a second argument we're going to pass the time or we're going to specify the time.

15:06.840 --> 15:14.000
So I'm going to put one if why I just put one because uh, in one second I'm just completing the hurt

15:14.000 --> 15:18.800
collected animation clip, which is going up and over time just fading out.

15:18.800 --> 15:22.000
So once again, it just completing the whole animation clip.

15:22.000 --> 15:27.200
So that's why as a second argument I just pass one f, which basically means one second.

15:27.240 --> 15:31.520
Now I'm going to press down Ctrl s to save my script and let's move to unity.

15:31.840 --> 15:36.720
So after one second you will see that hurt game object will also be destroyed.

15:36.760 --> 15:42.520
After collecting that firstly we collecting and we increasing our Maxwell.

15:42.520 --> 15:46.080
And after that once again we just destroying it right away.

15:46.080 --> 15:52.860
So let's click on this play button in the top in order to take a look on this and let's, uh, see what's

15:52.860 --> 15:53.820
really happening.

15:56.180 --> 15:57.860
So just wait for to compile.

15:59.540 --> 16:05.780
So if we get closer to this hard game object, we can see we increase our health.

16:06.020 --> 16:08.300
And the hard game object also get destroyed.

16:08.300 --> 16:12.060
So I'm going to click on this pause button in the top to pause my game.

16:12.060 --> 16:18.900
And if we move over to hierarchy, I'm not able to see any hard game object laying around in this hierarchy

16:18.940 --> 16:20.540
as well as in the scene view.

16:20.900 --> 16:22.140
Uh, as you can see.

16:22.140 --> 16:28.100
So basically means after one second we're just destroying, uh, right away after, uh, after one second,

16:28.100 --> 16:28.780
basically.

16:28.940 --> 16:30.500
And that is pretty cool.

16:30.500 --> 16:32.980
I hope you get the idea that how you can do it.

16:32.980 --> 16:37.260
So I'm going to click on this play button in the top in order to get out of play mode.

16:38.700 --> 16:40.060
Uh, get out of play mode.

16:40.060 --> 16:46.940
And I can just remove this hard game object from, uh, for now in the you can see in the hierarchy,

16:46.940 --> 16:48.420
but I'm going to leave that.

16:48.420 --> 16:49.420
So let's leave.

16:49.540 --> 16:50.580
Leave it like that.
