WEBVTT

00:00.200 --> 00:00.620
Hello, guys.

00:00.620 --> 00:01.790
Welcome to the video.

00:02.060 --> 00:05.930
Uh, before we proceed, I want to fix a bug that we have with the rhino.

00:05.930 --> 00:08.690
And also we have this bug with, uh, other enemies.

00:08.690 --> 00:13.550
But I plan to pay full attention to this on the cleanup video in the end of this section.

00:13.550 --> 00:17.960
As of now, I just want to show you a quick fix to a very small bug that we have.

00:17.960 --> 00:22.760
So as of now, if I jump on the head of the rhino, it may happen that I'll get damaged instead of killing

00:22.760 --> 00:25.070
him, just like you've seen here.

00:25.070 --> 00:31.340
So what happens is damage trigger may be bigger than a body collider you have on the enemy.

00:31.340 --> 00:37.790
And when we try to kill the enemy, we looking for this collider for the body Collider, the main collider

00:37.790 --> 00:38.900
that the enemy uses.

00:38.900 --> 00:41.600
And when we damage in the player, we use damage trigger.

00:41.600 --> 00:47.750
And this trigger should be smaller on the Y than the body collider we have on the enemy.

00:47.750 --> 00:50.270
And now you'll not have any bug like that.

00:50.510 --> 00:50.990
Okay.

00:50.990 --> 00:51.920
Very cool.

00:52.190 --> 00:55.370
Uh, also, I want to do a bit of cleanup before we start.

00:55.370 --> 00:56.870
This is a real fast one.

00:56.870 --> 01:02.660
So one day I woke up and I realized that damage Trigger is also a collider.

01:02.660 --> 01:05.810
And if that's the case, then we can just.

01:06.460 --> 01:12.700
Get the collider and disable it together with the main collider instead of make an entire variable on

01:12.700 --> 01:13.630
the script.

01:13.630 --> 01:15.490
So let's go to enemy rhino.

01:16.060 --> 01:18.100
Then I'm going to need enemy script.

01:18.940 --> 01:22.750
And over here we can turn this collider into an array.

01:23.320 --> 01:27.130
And then when we get in component of the collider, we're going to do.

01:28.710 --> 01:29.370
Collider.

01:29.370 --> 01:32.160
Get components in children.

01:32.520 --> 01:38.430
This will collect colliders in children, but the main transform will be taken into consideration as

01:38.430 --> 01:43.620
well because when it starts to count the children and getting components in children, it also checks

01:43.620 --> 01:45.150
the main object itself.

01:45.150 --> 01:50.070
So it kind of does get component and then get components in children all together.

01:50.340 --> 01:52.440
Uh, if we make this variable public.

01:53.820 --> 01:54.720
Or visible?

01:54.720 --> 01:55.920
Just visible.

02:03.670 --> 02:04.480
What's that?

02:06.060 --> 02:06.420
Uh.

02:07.290 --> 02:08.940
I'm going to comment this out for now.

02:13.620 --> 02:17.850
So look we have colliders array and we can go to play mode.

02:19.950 --> 02:25.920
And now we have main Collider and Damage Trigger Collider included into this array.

02:27.470 --> 02:33.290
Thanks to that, we can now just disable both colliders on death and not to worry about assigning another

02:33.290 --> 02:35.450
variable into enemy script.

02:36.460 --> 02:38.020
So on the enemy.

02:39.130 --> 02:42.010
I'm going to remove this variable damage trigger.

02:42.010 --> 02:43.030
I don't need it.

02:43.030 --> 02:49.720
Also, we know how assigning of a player works, so I'm going to just hide it from the inspector.

02:49.720 --> 02:51.070
I'm going to hide this as well.

02:51.070 --> 02:52.300
I don't need it anymore.

02:55.640 --> 02:56.720
Maybe like this.

02:56.720 --> 03:02.900
And then we're going to go down to the death method, remove this line of code.

03:02.900 --> 03:04.370
And then I'm going to do.

03:05.050 --> 03:07.360
For each var.

03:08.520 --> 03:11.100
Collider in call array.

03:11.340 --> 03:14.010
Let's rename it to colliders.

03:14.930 --> 03:16.940
So for each of that we're going to do.

03:18.340 --> 03:20.860
Let me remove this and do collider.

03:21.370 --> 03:23.350
Enable false.

03:23.890 --> 03:24.850
Just like that.

03:27.980 --> 03:28.880
Very good.

03:28.910 --> 03:31.400
Now we're going to go back to unity.

03:31.760 --> 03:33.980
We can see Inspector is a little bit cleaner.

03:33.980 --> 03:36.350
We don't have an extra variable there.

03:36.350 --> 03:39.170
And same works for mushroom as well.

03:40.240 --> 03:41.830
This is clean and nice.

03:41.830 --> 03:48.160
And now if we go to play mode, you'll see that both enemies can die from the jump on the head and work.

03:48.760 --> 03:51.460
Same as before, but everything is cleaner now.

03:51.490 --> 03:52.690
Oh, wait a second.

03:52.690 --> 03:54.580
I did not kill him for some reason.

03:54.580 --> 03:55.450
Let me check.

03:56.390 --> 04:00.140
So damage trigger is a bit lower than the body collider.

04:00.140 --> 04:01.280
So why did I.

04:02.170 --> 04:06.670
Let me make it lower even more like so, because it's not important.

04:06.670 --> 04:09.550
If we jump on the head of the enemy, he's supposed to die anyway.

04:09.700 --> 04:13.000
Player should be damaged only from the left or right side.

04:15.470 --> 04:16.070
Okay.

04:16.070 --> 04:17.000
Very cool.

04:18.160 --> 04:21.250
There is another thing I want to clean up and we can jump to the next video.

04:21.250 --> 04:23.020
I guess we're going to make enemy there.

04:23.440 --> 04:23.980
Um.

04:27.540 --> 04:30.750
Uh, let's go to assets prefab over here.

04:30.750 --> 04:32.130
I'm going to drag enemy rhino.

04:32.130 --> 04:34.560
So now we have prefab of the rhino as well.

04:34.830 --> 04:41.460
And as of now we have two enemies that using player detection chicken and rhino.

04:41.460 --> 04:44.130
We're also going to have a plant enemy in the next video.

04:44.130 --> 04:46.470
And he's going to use player detection as well.

04:46.470 --> 04:50.040
Also we're going to have a trunk enemy and he's going to use player detection as well.

04:50.040 --> 04:53.730
I know that because I did this before I did prototype of this project.

04:54.210 --> 04:57.510
So I guess it would be a good idea to move detection range.

04:57.510 --> 05:03.630
And the player detected variable into enemy script so multiple enemies can use it.

05:05.380 --> 05:08.710
Let's just go to enemy right over here.

05:08.860 --> 05:11.860
Take these two variables.

05:11.860 --> 05:13.120
Cut it out.

05:14.710 --> 05:19.990
Let's go to enemy all the way to the collision over here.

05:19.990 --> 05:22.090
I'm going to paste this in.

05:22.890 --> 05:24.690
And this should be protected now.

05:27.260 --> 05:29.420
Let's do is player detected.

05:29.420 --> 05:32.570
So we are consistent is player detected.

05:33.140 --> 05:36.890
And this should be protected as well in case we want to change something.

05:37.280 --> 05:38.870
I'm going to move it.

05:40.170 --> 05:42.540
Maybe above the player layer.

05:42.540 --> 05:43.590
What is player?

05:43.590 --> 05:48.990
And let's name it as player detection distance.

05:48.990 --> 05:53.580
So it has, you know like we have it consistent with all other variables here.

05:53.580 --> 05:55.890
Let's make it equals to 15 by default.

05:57.070 --> 06:00.010
Now we need to go to, uh, enemy rhino.

06:00.040 --> 06:02.470
Maybe I should do this at first, but.

06:03.390 --> 06:04.290
Seemed right.

06:04.530 --> 06:11.010
And here, what we need to do is to take this variable and move it inside of a handle collision and

06:11.010 --> 06:14.490
then just, uh, have it all together within one method.

06:14.490 --> 06:15.990
So how are we going to do this?

06:15.990 --> 06:17.070
Well.

06:18.450 --> 06:19.500
Let me think.

06:19.500 --> 06:23.910
We're going to go to definition of a handle collision over here.

06:23.910 --> 06:27.150
We're going to make this player detected equals to.

06:27.150 --> 06:29.340
And let's just go to enemy rhino.

06:30.350 --> 06:32.510
Actually, let's just copy this line of code.

06:32.510 --> 06:34.850
We can do Ctrl X to cut.

06:34.880 --> 06:36.230
Go to enemy.

06:36.890 --> 06:38.180
Paste it in here.

06:39.530 --> 06:41.270
Let's save this.

06:41.270 --> 06:44.570
And now this is on a base method over here.

06:44.570 --> 06:47.210
An enemy rhino still has access to it.

06:47.980 --> 06:51.430
In here and I guess we can keep it like so.

06:51.430 --> 06:57.250
Even now we just override the method and add simple if statement regarding Ondraw gizmos.

06:58.000 --> 07:06.220
We can cut this line, go to Ondraw Gizmos over here, and we can just paste this in over here.

07:06.730 --> 07:07.660
Just like that.

07:08.700 --> 07:13.620
Now it's going to be drawn for all of the enemies, and now it will be available to all of the enemies.

07:14.960 --> 07:16.850
We can go back to enemy rhino.

07:17.940 --> 07:23.070
Over here we can remove Andro gizmos method and it's going to work same as before.

07:23.070 --> 07:27.600
But now method is moved to the base script like the enemy script.

07:28.410 --> 07:29.250
Very cool.

07:29.250 --> 07:31.080
Let's go and fix it on a chicken.

07:32.630 --> 07:33.710
So on the chicken.

07:33.710 --> 07:38.450
Now we have this handle collision inside of base method.

07:38.450 --> 07:39.380
So we don't need it.

07:39.380 --> 07:42.260
We can just remove it like so.

07:43.860 --> 07:46.410
Just Take It Away and Andro Gizmos as well.

07:46.410 --> 07:49.350
It is on the base script, so it's pretty good.

07:49.350 --> 07:52.170
There is something else I want to improve while we're doing this.

07:52.200 --> 07:55.440
We have this anim set float on the enemy chicken.

07:55.440 --> 07:57.060
We have it on the enemy rhino.

07:57.060 --> 07:59.310
We have it on the enemy mushroom.

07:59.310 --> 08:04.290
And we might have other functionality regarding the animator on different enemies.

08:04.290 --> 08:14.100
So what I want to do is go to enemy, make a protected virtual void, and the animator in which we're

08:14.100 --> 08:18.450
going to do anim set float of x velocity.

08:20.090 --> 08:22.370
Two RB velocity dot x.

08:25.250 --> 08:26.390
Very good.

08:26.390 --> 08:35.030
Then we can go to update and in the base update we can do handle animator.

08:35.390 --> 08:39.230
Thanks to this now animations will be on every update of the enemy.

08:39.230 --> 08:41.270
And now we can go to enemy chicken.

08:41.270 --> 08:43.010
Just remove it here.

08:43.010 --> 08:46.850
It's going to be called on the base over here anyway.

08:46.850 --> 08:52.250
And this method has the update of the velocity dot x, so we don't need it here.

08:52.250 --> 08:54.140
Same goes for mushroom.

08:54.800 --> 08:55.850
We don't need it.

08:55.850 --> 08:57.320
Same goes for Rhino.

08:57.320 --> 08:58.130
We don't need it.

08:58.130 --> 09:00.860
And same goes for any enemy we're going to have in the future.

09:03.160 --> 09:04.600
Now let's save it all.

09:04.690 --> 09:08.590
And let's just go back to unity and make sure we did not break anything.

09:08.590 --> 09:10.480
I did not break anything, I hope.

09:13.230 --> 09:14.910
So we're going to go to play mode.

09:16.350 --> 09:18.030
Everything works the same.

09:20.270 --> 09:23.180
Uh, mushroom is not working, but only because he has zero move speed.

09:23.180 --> 09:24.890
Let me give him some speed.

09:24.890 --> 09:26.090
He walks.

09:26.090 --> 09:27.500
I can kill him.

09:27.500 --> 09:28.850
Same goes for Rhino.

09:28.850 --> 09:30.650
Works same as before.

09:30.680 --> 09:31.760
Very cool.

09:34.660 --> 09:35.560
And let's go back.

09:35.560 --> 09:37.330
I have something else to clean up here.

09:37.330 --> 09:42.460
I think we can call this handle collision method on the base update.

09:42.460 --> 09:44.500
So I'm going to go to enemy.

09:44.920 --> 09:48.340
And together with the handle animator I'm going to do handle collision.

09:48.340 --> 09:50.350
Maybe even before the animator.

09:50.350 --> 09:52.360
Let's go to enemy right now.

09:52.360 --> 09:55.120
Now remove handle collision from here.

09:56.520 --> 10:00.210
Let's go to enemy chicken and remove handle collision from here.

10:00.910 --> 10:04.990
And let's go to enemy mushroom and remove handle collision from here again.

10:04.990 --> 10:08.410
Because now all of the enemies have this method in the base.

10:09.820 --> 10:11.440
Let's save all.

10:12.860 --> 10:20.060
And even if you have, uh, functionality overridden like we have on the enemy right over here, we

10:20.060 --> 10:22.550
have override of a handle collision.

10:22.550 --> 10:26.900
Anyway, the base method of the update over here.

10:27.950 --> 10:29.870
We'll call handle collision method.

10:29.870 --> 10:35.900
And if you override it on a child script, it's going to call the overwritten method, just this one.

10:35.900 --> 10:39.200
So you don't need to worry to call it again and update.

10:39.200 --> 10:40.340
It's going to be fine.

10:41.940 --> 10:46.050
Let's go ahead and try this again and you'll see that everything works the same.

10:46.050 --> 10:48.480
I'm going to trigger the rhino.

10:48.480 --> 10:49.800
He runs to me.

10:50.320 --> 10:51.670
And I can kill him.

10:51.700 --> 10:52.900
Okay, cool.

10:53.200 --> 10:55.330
Let me see if anything else can be cleaned.

10:58.800 --> 11:01.320
Maybe this code on the right now could be taken into a method.

11:01.320 --> 11:04.530
Extract method handle speed up.

11:07.770 --> 11:08.460
Yeah.

11:08.610 --> 11:09.570
Why not?

11:11.440 --> 11:13.750
And this could be taken into a method as well.

11:13.750 --> 11:15.730
Let's do extract method.

11:17.840 --> 11:25.940
Speed reset just so it's a bit cleaner, and we understand what we have inside of a method speed reset,

11:25.940 --> 11:31.610
because it's much easier to take a quick look at the method, see that we do speed reset and not worry

11:31.610 --> 11:35.300
about understanding what the code does until we really need it.

11:37.410 --> 11:42.210
In general, there is a good rule if you can put something into a method, do it.

11:46.870 --> 11:50.080
Well, uh, if there is anything else, I'll let you know.

11:50.440 --> 11:53.980
Actually, let's go ahead and click triggers on the enemies.

11:54.550 --> 11:57.640
Since we're doing cleanup, why not let me take enemy mushroom?

11:57.640 --> 12:03.790
So on the enemy mushroom, we need to have damage trigger lower than the body collider.

12:03.790 --> 12:07.240
So I'm going to just reduce the height of it to something like this.

12:09.270 --> 12:10.350
On the enemy Rhino.

12:10.350 --> 12:11.130
We did this.

12:11.130 --> 12:14.220
And on the enemy chicken asset prefab.

12:17.700 --> 12:18.990
Enemy chicken.

12:20.440 --> 12:24.250
Let's replace this damage trigger with a box collider.

12:24.400 --> 12:25.030
Did I not?

12:27.660 --> 12:30.270
Let's replace it with a box Collider 2D.

12:32.690 --> 12:36.380
Let's make it wide, but not tall.

12:36.380 --> 12:38.240
So something like this will work.

12:38.510 --> 12:39.590
And the main trigger?

12:39.590 --> 12:44.150
I think we can lower the main trigger as well, because it's kind of too easy to kill the chicken.

12:44.660 --> 12:46.220
So something like this should do.

12:50.070 --> 12:50.400
Okay.

12:50.400 --> 12:51.180
Very good.

12:51.210 --> 12:53.400
Now let's apply overrides.

12:53.430 --> 12:55.590
Let's apply override for Rhino.

12:55.620 --> 12:57.120
Oh, it is working okay.

12:57.120 --> 12:57.930
For the mushroom.

12:57.930 --> 13:01.500
Let's make move speed two and let's apply overrides.

13:01.890 --> 13:03.600
Okay, now this is good.

13:03.600 --> 13:05.400
We did a bit of a very nice cleanup.

13:05.400 --> 13:06.780
I hope you did like it.

13:06.930 --> 13:09.930
And I'm going to see you in the next video.
