WEBVTT

00:00.050 --> 00:00.560
Hello guys.

00:00.560 --> 00:01.640
Welcome to this section.

00:01.640 --> 00:03.800
In this one we're going to develop enemies.

00:03.800 --> 00:08.240
And I think now development will go much faster because now we know a lot of things.

00:08.240 --> 00:10.550
We learned how to work with timers coroutines.

00:10.550 --> 00:14.600
We know how to move rigidbody with velocity, how to flip the character, all of that things.

00:14.600 --> 00:16.610
So now I'm going to show you how to do stuff.

00:16.610 --> 00:21.710
But things that I explained before I'm not going to, you know, tap on them as much as it was before.

00:21.710 --> 00:22.010
Yeah.

00:22.010 --> 00:23.630
So get ready for development.

00:23.630 --> 00:24.530
Let's go.

00:24.560 --> 00:26.930
We're going to go to Graphics Enemies.

00:26.930 --> 00:29.990
And here we have a bunch of enemies.

00:30.020 --> 00:33.710
We're not going to develop all of them, but we're going to develop a lot.

00:33.710 --> 00:38.900
And I want to start with simple I want to start with a mushroom enemy because it's quite simple.

00:38.900 --> 00:41.270
It just walks left and right and you can hit the enemy.

00:41.270 --> 00:41.840
That's it.

00:41.840 --> 00:46.550
We also need to keep in mind that we're going to develop not one enemy, but a bunch of them, and they're

00:46.550 --> 00:48.440
going to have similar functionality.

00:48.440 --> 00:53.930
Because of that, we better prepare some sort of inheritance that can be used by multiple enemies.

00:53.930 --> 01:00.680
As example, we have this mushroom that will run left and right with this animation over here.

01:00.860 --> 01:03.530
Also, we're going to have an enemy like um.

01:04.530 --> 01:12.630
Let's say rhino horns are going to run left and right, and we could type movement for both of them

01:12.630 --> 01:20.100
in two different scripts, but also we could make one movement function and then just use it on Rhino

01:20.100 --> 01:23.100
and on the mushroom getting me here.

01:23.100 --> 01:25.260
So we're going to develop a mushroom first.

01:25.260 --> 01:26.610
But we're going to do that.

01:26.880 --> 01:31.200
And we're going to keep in mind that we're going to need some of the script by other enemies.

01:31.200 --> 01:32.070
All right.

01:33.260 --> 01:35.600
As always, I offer you to start with simple.

01:35.600 --> 01:38.270
Let's set up controller and animations.

01:38.300 --> 01:41.030
I'm going to go to animations over here.

01:41.030 --> 01:44.030
Let's make a new folder for uh.

01:44.030 --> 01:45.560
Actually we don't need a folder for now.

01:45.560 --> 01:47.270
We can develop things here.

01:47.270 --> 01:49.280
Then we can arrange them into a folder.

01:49.610 --> 01:51.710
Let's do animator controller.

01:51.710 --> 01:55.010
This is going to be enemy mushroom.

01:56.090 --> 01:58.580
We type it together like so and we mushroom AC.

01:59.970 --> 02:02.430
Then we're going to do.

02:02.430 --> 02:02.970
Sorry.

02:02.970 --> 02:04.770
We need to go to graphics.

02:05.250 --> 02:06.360
Enemies.

02:06.360 --> 02:07.980
Let's find mushroom.

02:08.520 --> 02:11.640
Let's take the, uh, idle sprite place.

02:11.640 --> 02:14.250
It here is going to be enemy.

02:15.030 --> 02:15.990
Mushroom.

02:17.700 --> 02:19.710
We're going to give it a layer of, uh.

02:20.460 --> 02:22.140
Enemy just like that.

02:22.140 --> 02:23.730
So now it's going to be visible.

02:23.730 --> 02:24.780
Oh, we need to.

02:24.810 --> 02:25.050
Yeah.

02:25.050 --> 02:28.080
Let's take the sprites and make pixel per unit 16.

02:28.080 --> 02:28.920
Great.

02:28.920 --> 02:31.020
So now we have a mushroom enemy.

02:31.530 --> 02:34.500
We also want to give it a layer of the enemy.

02:34.920 --> 02:35.790
This is important.

02:35.790 --> 02:36.780
We're going to use it later on.

02:36.780 --> 02:38.400
So I'm going to do add layer.

02:38.400 --> 02:40.530
And over here we're going to do enemy.

02:40.530 --> 02:43.320
And since now we're using a couple of different layers.

02:43.320 --> 02:47.970
Let's select the player and give him layer of a player.

02:51.980 --> 02:52.670
Like so.

02:52.670 --> 02:58.880
So this one gonna have player and now this object only for the enemy.

02:58.880 --> 03:00.470
We're going to do enemy.

03:00.620 --> 03:01.730
Great.

03:02.340 --> 03:05.850
Now what do we need for this enemy animator controller?

03:05.850 --> 03:08.220
So we're going to drag it on the enemy like so.

03:08.250 --> 03:12.120
We also want to have a bunch of animations and let's prepare animations.

03:12.120 --> 03:15.960
Now when I do create assets animations.

03:15.960 --> 03:21.600
And over here I'm going to do Enemy mushroom and it's going to be idle.

03:23.560 --> 03:24.940
Let's go to graphics.

03:25.270 --> 03:26.290
Enemies.

03:27.380 --> 03:29.600
Mushroom idol.

03:31.670 --> 03:33.380
The sample array is going to be 20.

03:34.690 --> 03:37.390
Then we're going to need a new clip.

03:37.390 --> 03:39.670
Enemy mushroom.

03:39.730 --> 03:40.210
Uh.

03:40.990 --> 03:41.920
Move.

03:42.640 --> 03:44.530
You're going to have this run animation.

03:46.100 --> 03:47.330
Can I place them here?

03:47.330 --> 03:49.190
Sample rate 20.

03:49.670 --> 03:53.150
Let me make it a bit smaller and I'm going to stretch the window.

03:53.150 --> 03:54.860
So this is the run animation.

03:54.860 --> 03:58.160
And also we're going to have a hit animation.

03:58.160 --> 04:01.070
So we're going to do enemy.

04:01.810 --> 04:02.950
Mushroom.

04:03.070 --> 04:04.030
Let's just call it hit.

04:04.030 --> 04:05.320
Enemy mushroom hit.

04:07.180 --> 04:09.430
Can I take these sprites and place them here?

04:09.430 --> 04:11.470
Sample rate 20.

04:11.500 --> 04:12.460
Nice.

04:12.490 --> 04:14.530
Now let's go to animator of this one.

04:14.530 --> 04:20.080
And for the movement and idle I think I'm going to use the blend tree would be the easiest one right.

04:20.230 --> 04:24.280
Uh because we're going to use velocity rigid body velocity for this enemy.

04:24.280 --> 04:29.260
So let's make a blend tree and name it as move idle.

04:29.260 --> 04:31.270
And that's what we did for the player.

04:31.270 --> 04:32.350
Oh I hate this bug.

04:32.350 --> 04:33.700
This happens all the time.

04:33.700 --> 04:35.380
Move idle.

04:35.380 --> 04:39.280
Uh, I need to restart unity so I can rename this blend tree.

04:39.280 --> 04:40.810
Just give me a minute.

04:41.810 --> 04:43.670
Okay, let's try again.

04:44.670 --> 04:45.840
Idle move.

04:46.560 --> 04:47.220
Good.

04:47.220 --> 04:53.880
Now inside we're going to have two motions one two and let's do three.

04:54.030 --> 05:00.360
Automate threshold and check is going to be mushroom I cannot see anything.

05:00.360 --> 05:01.740
Mushroom move.

05:04.370 --> 05:06.860
Mushroom idol and.

05:07.810 --> 05:09.490
Mushroom move.

05:09.490 --> 05:10.810
I'm going to extend it like so.

05:10.810 --> 05:15.790
So you can see the names you're going to have minus one zero one.

05:15.790 --> 05:18.190
Now if you open preview over here.

05:18.870 --> 05:20.430
And click play.

05:20.460 --> 05:20.970
Oh.

05:21.000 --> 05:21.540
What's up?

05:21.540 --> 05:22.620
Why it's not available.

05:28.020 --> 05:28.650
Yeah.

05:28.770 --> 05:30.330
We need to select it again okay.

05:30.330 --> 05:33.600
So he's moving on the zero.

05:33.600 --> 05:34.890
He's standing.

05:35.910 --> 05:37.320
Then he's moving again.

05:37.470 --> 05:38.340
Cool.

05:38.340 --> 05:39.510
Very cool.

05:39.540 --> 05:42.390
Now let's rename this to x velocity.

05:42.390 --> 05:46.050
And this is the parameter we're going to control on the mushroom.

05:46.140 --> 05:48.810
Now let's go to the base layer.

05:49.560 --> 05:51.570
Take this enemy mushroom hit.

05:51.570 --> 05:53.100
This should be a default.

05:53.100 --> 05:56.640
Then we're going to transfer to enemy mushroom with a trigger.

05:58.720 --> 06:00.700
Trigger hit just like that.

06:00.700 --> 06:01.240
Why not?

06:02.650 --> 06:03.430
Trigger.

06:03.430 --> 06:06.970
Hit exit time and check transition duration zero.

06:06.970 --> 06:12.190
And I suppose we're going to destroy the object or make him fall after we hit the enemy.

06:12.190 --> 06:15.970
This is how it's going to be on a preview of these assets.

06:15.970 --> 06:18.760
You actually can see how enemies are dying.

06:18.760 --> 06:20.440
Let me just quickly find it over here.

06:20.440 --> 06:24.520
You can see when we jump in on the enemy, they sort of fallen off the level.

06:24.520 --> 06:27.760
They do a bit of a rotation, they lose in Collider and they just fall in.

06:27.760 --> 06:29.650
So probably that's what we can do.

06:29.650 --> 06:30.250
Let's see.

06:30.250 --> 06:34.420
Maybe we'll take a different approach because we already know how to make objects fall off the level.

06:34.420 --> 06:34.870
Right.

06:34.870 --> 06:36.610
The falling platform does that.

06:36.610 --> 06:39.160
We just disable colliders and it falls off the level.

06:39.160 --> 06:41.590
So it's not really different from what we've seen just now.

06:41.590 --> 06:42.310
Okay.

06:42.310 --> 06:45.040
So now we need a mushroom script.

06:45.740 --> 06:47.270
An anime script.

06:47.270 --> 06:51.560
Let's go to scripts and I'm going to make C sharp script.

06:51.800 --> 06:52.730
Enemy.

06:54.020 --> 06:56.210
Then I'm going to make C sharp script.

06:59.400 --> 07:01.470
Enemy mushroom.

07:03.840 --> 07:05.550
Here we have enemy mushroom.

07:05.550 --> 07:07.260
Why am I doing it like that?

07:07.260 --> 07:13.800
Well, we're going to put enemy mushroom on the enemy mushroom, and we're going to do the common thing

07:13.800 --> 07:14.850
on the enemy script.

07:14.850 --> 07:17.520
And enemy mushroom will inherit from the enemy.

07:17.520 --> 07:18.870
Let me show you the example.

07:18.870 --> 07:20.700
Let's do inheritance from the enemy.

07:20.700 --> 07:23.850
And we know that most of the enemies are going to have a movement speed.

07:23.850 --> 07:24.360
Right?

07:24.360 --> 07:27.870
I'm going to close all by this and I'm going to find enemy script.

07:29.310 --> 07:35.280
So if most of the enemies have movement speed, then we don't need to make movement speed variable on

07:35.280 --> 07:36.240
each new enemy.

07:36.240 --> 07:38.700
We can do it only once by making it here.

07:38.700 --> 07:40.530
Let's make serialize field.

07:41.100 --> 07:42.540
Uh, protected float.

07:42.540 --> 07:44.010
Move speed.

07:45.100 --> 07:46.300
Now let's save this.

07:46.300 --> 07:47.920
Let's go back to unity.

07:49.440 --> 07:52.710
And we can see that move speed is there for the enemy mushroom.

07:52.710 --> 07:58.770
So now further on when we create new enemy and we need some bunch of parameters that are common between

07:58.770 --> 08:03.090
different enemies, we can easily just make it on the enemy script and use it by others.

08:03.210 --> 08:07.950
Let's go ahead and keep development of the enemy mushroom through the enemy script.

08:07.950 --> 08:12.150
We know that other enemies, not only enemy mushroom, going to have an animator.

08:12.150 --> 08:18.960
So let's do protected animator anim, and probably most of them going to have rigid body.

08:18.960 --> 08:22.170
So let's do protected rigid body RB.

08:22.860 --> 08:28.110
And then we're going to open our wake that we can override because later on we might need to override

08:28.110 --> 08:28.500
our wake.

08:28.500 --> 08:31.560
So I'm going to do protected virtual void awake.

08:31.590 --> 08:35.580
This will allow me to override the method on the enemy mushroom.

08:35.580 --> 08:38.190
As example we can go to enemy mushroom.

08:38.190 --> 08:41.910
And here we can just type override awake.

08:43.010 --> 08:49.130
And this will be a new awake that has functionality of the enemies awake.

08:50.980 --> 08:51.790
Cool.

08:51.850 --> 08:53.770
Now let's go to enemy again.

08:53.770 --> 08:59.740
And over here we're going to do Anim Getcomponent of the animator and RB Getcomponent of Rigidbody.

08:59.980 --> 09:01.030
Very nice.

09:01.060 --> 09:03.040
Now let's say we want to move the enemy.

09:03.040 --> 09:08.860
We could make one script that other enemies would use for the movement, but we are not sure yet how

09:08.860 --> 09:10.420
other enemy is going to act.

09:10.420 --> 09:13.570
If we're going to need to use script on those enemies as well.

09:13.570 --> 09:18.970
That's why for now, I'm going to do handle movement on the enemy mushroom only I'm going to do private,

09:19.090 --> 09:21.940
uh, void handle movement.

09:21.940 --> 09:24.820
And in the handle movement we're going to do RB velocity.

09:24.820 --> 09:30.670
And as you can see it is available to this script because we have it as protected on the enemy over

09:30.670 --> 09:31.300
here.

09:31.990 --> 09:34.780
So velocity equals to new vector two.

09:34.780 --> 09:36.430
And here we have move speed.

09:36.430 --> 09:39.010
And then we're going to do rb velocity dot y.

09:39.010 --> 09:41.500
It also would be nice to know facing direction.

09:41.500 --> 09:44.980
So we can define where enemy should move left or right.

09:44.980 --> 09:47.080
So let's go to enemy over here.

09:47.080 --> 09:53.380
And let's make a protected int facing direction.

09:53.380 --> 09:58.390
And if you remember our mushroom is facing to the left as well as most of the enemies.

09:58.390 --> 10:00.550
So I'm going to make it minus one by default.

10:00.550 --> 10:06.340
Let's save this then on the enemy mushroom we can do multiply by facing direction.

10:07.630 --> 10:09.460
Now we need an update.

10:09.520 --> 10:10.420
Update.

10:10.420 --> 10:11.920
Handle movement.

10:12.750 --> 10:13.650
Cool.

10:13.650 --> 10:15.360
Now let's go.

10:15.360 --> 10:18.300
Uh, actually, we need to update the animations as well.

10:18.300 --> 10:19.260
Would be nice.

10:19.260 --> 10:27.030
So I'm going to do anim set float of x velocity to the RB velocity dot x.

10:28.190 --> 10:29.390
Very nice.

10:29.420 --> 10:31.250
Let's go back to unity.

10:31.280 --> 10:35.870
This enemy needs to have a collider so we can give it different colliders.

10:35.870 --> 10:37.160
Let's try.

10:37.730 --> 10:39.800
Let's try Box Collider for now.

10:40.850 --> 10:42.410
And we're going to just.

10:43.930 --> 10:48.790
Decrease it to the size of this maybe is going to be a body collider.

10:48.790 --> 10:51.700
We're going to use it to detect collision around the mushroom.

10:51.700 --> 10:55.780
So he will not fall through the ground or he will not pass by walls.

10:55.780 --> 10:57.700
And we also need a rigid body on this one.

10:57.700 --> 10:59.680
So I'm going to do rigid body 2D.

11:00.710 --> 11:02.990
Let's do interpolate collision detection.

11:02.990 --> 11:04.100
Continuous.

11:04.700 --> 11:06.770
Now we can go to play mode.

11:07.100 --> 11:08.510
He's going to stand there.

11:09.940 --> 11:11.170
In the idle.

11:11.320 --> 11:12.490
Oh, what is that?

11:13.290 --> 11:16.080
Variable of a waypoint have not been assigned.

11:16.810 --> 11:17.290
Over here.

11:17.290 --> 11:18.670
We need to remove none.

11:23.360 --> 11:24.950
Okay, so now mushroom stands there.

11:24.950 --> 11:28.700
I can give him movespeed of two and he's moving to the left.

11:29.300 --> 11:30.170
A very funny guy.

11:30.170 --> 11:30.770
I like it.

11:30.770 --> 11:34.280
So he walks like that and then falls down.

11:34.280 --> 11:38.780
What do we need now is to detect the ground in front of the mushroom.

11:38.780 --> 11:46.220
And then if there is no ground in front of him like so, then he'll be like, okay, I need to go to

11:46.220 --> 11:47.930
another side and he's going to flip.

11:47.930 --> 11:52.430
So if that's what we need, then we need collision detection on the mushroom.

11:52.430 --> 11:56.000
And we need a flip method just like we had on the player.

11:56.960 --> 12:02.060
Also, I think that lots of enemies are going to need a collision detection probably.

12:02.060 --> 12:05.120
So I'm going to make a header over here.

12:06.090 --> 12:06.750
Header.

12:06.780 --> 12:08.790
Basic collision.

12:08.790 --> 12:10.260
Let's do serialize field.

12:10.740 --> 12:11.880
Protected.

12:12.850 --> 12:17.860
Float and it's going to be, uh, ground check distance.

12:17.860 --> 12:19.990
And now it's going to be 1.1.

12:19.990 --> 12:21.700
So let's do it as a default value.

12:21.700 --> 12:26.950
Then we need serialize field protected float wall check distance.

12:27.280 --> 12:29.140
Uh let's do 0.7 F by default.

12:29.140 --> 12:30.850
But we can always adjust.

12:30.880 --> 12:32.350
We're also going to need a layer.

12:32.350 --> 12:37.750
So protected layer mask what is ground.

12:37.750 --> 12:40.930
And we cannot check the ground under the mushroom.

12:40.930 --> 12:43.570
We need to make ground check in front of him.

12:43.570 --> 12:47.020
Because of that we're going to have serialized field protected transform.

12:47.860 --> 12:50.590
Uh, ground check.

12:50.710 --> 12:51.700
Just like that.

12:52.360 --> 12:55.030
And I think that's good for now.

12:55.030 --> 12:57.010
Now let's go ahead and make a method.

12:57.010 --> 12:59.110
And I feel a little bit lazy.

12:59.110 --> 13:01.780
So I'm going to go to the player script.

13:03.080 --> 13:04.430
All the way down.

13:05.410 --> 13:07.180
We have this handle collision.

13:07.180 --> 13:09.790
We have our flip methods.

13:09.790 --> 13:11.140
So we're going to take them.

13:11.140 --> 13:14.230
Let's take these three copy it.

13:14.230 --> 13:16.720
Go to enemy face them in.

13:17.230 --> 13:20.230
Also we need this handle collision method.

13:20.230 --> 13:21.640
Let's copy this.

13:21.670 --> 13:25.660
Go to enemy and paste it over here.

13:25.660 --> 13:28.570
Now we need to fix this bunch of errors and it's going to be good.

13:28.570 --> 13:30.850
We have a boolean that we need to create.

13:30.850 --> 13:35.530
Let's go and make protected bool facing right.

13:35.530 --> 13:39.550
And it's going to be false by default because we know mushroom is facing left.

13:40.860 --> 13:44.460
Now for these booleans over here we need.

13:46.270 --> 13:52.030
Private and let's do protected protected bull is wall detected.

13:52.030 --> 13:56.440
And also I want to have protected bull is ground detected.

13:57.460 --> 13:58.900
We can go down.

14:01.190 --> 14:05.390
And over here we're going to type is ground detected.

14:06.790 --> 14:07.660
Nice.

14:07.660 --> 14:09.370
Now for the ground check.

14:09.370 --> 14:13.240
Instead of transform, we're going to use ground check position.

14:14.130 --> 14:14.910
Very good.

14:14.910 --> 14:19.050
Also, we need to change it here for the gizmos ground check position.

14:20.580 --> 14:22.170
Ground check position.

14:22.950 --> 14:24.240
Ground check position.

14:25.580 --> 14:26.060
Cool.

14:26.060 --> 14:30.110
So now we have collision check and gizmos check on the enemy.

14:30.230 --> 14:36.710
This is the handle flip method, which will act a bit differently according to the enemy we have.

14:36.710 --> 14:38.330
Let me quickly explain.

14:38.720 --> 14:45.590
If a mushroom is moving to the left, then probably we have to just adjust, you know, like set, handle,

14:45.590 --> 14:48.830
flip according to the velocity of the rigid body on the x.

14:48.830 --> 14:52.730
But there is such enemy as a, uh, bat.

14:53.330 --> 14:54.110
As example.

14:54.110 --> 14:54.440
Yeah.

14:54.440 --> 14:56.420
Graphics enemies bet.

14:56.960 --> 15:02.180
And this enemy will fly towards the player when it detects the player.

15:02.180 --> 15:05.210
And here we're not going to have our base velocity.

15:05.210 --> 15:08.930
We're probably going to use move towards, just like we did with the trap.

15:08.930 --> 15:15.980
So for that we cannot use our base velocity, but we can use position of the player to flip the enemy

15:15.980 --> 15:17.720
to know where he needs to face.

15:17.720 --> 15:21.110
So as you can see, it can be different for different enemies.

15:21.110 --> 15:27.440
Because of that, I'm going to pass a variable over here, float x value and we're going to use x value.

15:28.320 --> 15:28.800
Here.

15:28.800 --> 15:32.520
So now this method is flexible can be used with any x we have.

15:32.520 --> 15:33.300
Cool.

15:34.580 --> 15:35.810
We have a lot.

15:35.810 --> 15:36.800
Um.

15:39.280 --> 15:41.050
I'm going to go to enemy mushroom.

15:41.410 --> 15:44.200
I'm going to call handle collision over here.

15:44.850 --> 15:45.810
Oh, it's not available.

15:45.810 --> 15:48.000
Okay, let's go to enemy.

15:48.240 --> 15:51.870
Make this as protected virtual void.

15:52.950 --> 15:54.690
Gizmo is going to work like that, I think.

15:54.690 --> 15:55.800
So we don't need it.

15:55.800 --> 16:03.480
And the flip can be as protected virtual void and better make it, uh, for the flip as well.

16:04.730 --> 16:07.100
Nice nice nice nice.

16:07.100 --> 16:09.110
So so far nothing new.

16:09.110 --> 16:10.910
We had this functionality on the player.

16:10.910 --> 16:17.120
We know how it works and just on the enemies script, so it can be used by many enemies and how it can

16:17.120 --> 16:18.380
be used by many enemies.

16:18.380 --> 16:20.270
I'm going to show you on the enemy mushroom.

16:20.270 --> 16:22.670
So now we can type here handle collision.

16:24.660 --> 16:31.680
And now in the update we can check if no is ground detected means we do not have anything in front of

16:31.680 --> 16:36.390
the mushroom, and it's time to flip the mushroom so he can walk to another side.

16:36.390 --> 16:38.130
Let's do flip over here.

16:38.430 --> 16:40.140
Now I'm going to save it.

16:40.500 --> 16:41.940
Go back to unity.

16:43.360 --> 16:43.900
Over here.

16:43.900 --> 16:47.260
We need to make an empty transform for the mushroom.

16:47.290 --> 16:50.260
Let's do create empty ground.

16:50.260 --> 16:51.160
Check.

16:51.370 --> 16:53.830
And I'm going to assign it over here.

16:53.830 --> 16:56.020
And also we need to choose what is ground.

16:57.790 --> 16:58.480
Nice.

16:58.480 --> 17:01.030
Now the ground check is far enough.

17:01.030 --> 17:01.450
It's okay.

17:01.450 --> 17:04.420
So 1.1 was good for the wall check.

17:06.040 --> 17:09.850
Yeah, point nine will be okay.

17:09.850 --> 17:14.860
Now, if I'm not wrong and I'm pretty sure I'm right, we need to hit the play button.

17:14.860 --> 17:20.050
Mushroom will walk up to here, and then he'll decide to flip back because there is no ground in front

17:20.050 --> 17:20.560
of him.

17:21.500 --> 17:26.480
We only need to take this ground check and move it forward like so.

17:26.810 --> 17:28.880
Just so we're doing check in front of him.

17:28.880 --> 17:30.380
Let's go.

17:30.380 --> 17:32.210
And mushroom is there.

17:32.210 --> 17:35.810
And then he's like haha and walks back.

17:35.810 --> 17:36.710
Perfect.

17:36.710 --> 17:39.920
Now we want to flip the mushroom when he's facing the wall.

17:40.310 --> 17:42.650
Let's go back to the script.

17:43.310 --> 17:47.030
And over here we're going to do or is well detected.

17:47.210 --> 17:48.230
Nice.

17:48.230 --> 17:51.710
Now you're going to flip, uh, when he faces the wall as well.

17:51.860 --> 17:56.690
Then next thing I want to do is to give him a bit of the idle time.

17:56.690 --> 18:02.630
I want him to walk up to the wall or ledge and wait for a while.

18:02.630 --> 18:04.790
Let me quickly check the flip with the wall.

18:07.240 --> 18:07.600
Yeah.

18:07.600 --> 18:08.350
Good.

18:11.340 --> 18:15.390
So to add the idle time, we're going to do something that we did before.

18:15.390 --> 18:17.820
We're going to use time dot delta time and a timer.

18:17.850 --> 18:20.100
Let me quickly explain how it's going to work.

18:20.100 --> 18:23.250
And I think we might need this idle time on other enemies.

18:23.250 --> 18:31.200
So I'm going to go ahead here and make serialized field private or sorry, let's do protected float

18:31.200 --> 18:32.730
idle time.

18:32.730 --> 18:33.930
And.

18:34.720 --> 18:39.730
And let's do protected float, idle timer.

18:40.060 --> 18:42.610
And it's going to be very easy to make a mistake like that.

18:42.730 --> 18:47.740
Let's do, uh, let's do idle timer and idle duration.

18:48.360 --> 18:53.700
Although I like idle time better, we need to be sure we don't make mistake in the future.

18:53.700 --> 18:54.180
Okay.

18:54.300 --> 18:57.720
Let me maybe do serialize field so we can see how it's working.

18:57.720 --> 18:58.320
Okay.

18:58.320 --> 19:00.780
And I'm going to just.

19:02.390 --> 19:05.480
Let's do protected virtual update.

19:06.360 --> 19:07.170
Avoid.

19:07.170 --> 19:07.890
Sorry.

19:09.380 --> 19:10.700
Void update.

19:11.060 --> 19:12.230
What's up man?

19:13.070 --> 19:13.550
Yeah.

19:13.550 --> 19:14.210
Okay.

19:14.390 --> 19:15.230
Update.

19:15.530 --> 19:16.460
And.

19:17.400 --> 19:18.720
On the enemy mushroom.

19:18.720 --> 19:22.200
Instead of doing private void, we can do override update.

19:22.230 --> 19:27.390
Protected override okay, so now this is overriding the update we have here.

19:27.390 --> 19:30.690
But because we did not type base it's not going to work.

19:30.690 --> 19:33.240
We just need to type base update.

19:33.240 --> 19:37.200
Now it's going to have functionality of the base update from the enemy.

19:37.200 --> 19:42.630
And in the base update we can do idle timer minus equals time dot delta time.

19:42.630 --> 19:45.120
So we're going to decrease the value all the time.

19:45.210 --> 19:47.700
Let's save this.

19:48.150 --> 19:50.550
Let's go back to uh unity.

19:50.550 --> 19:52.140
And I'm going to show you what we're going to do.

19:54.030 --> 19:56.400
So you can see idle timer is decreasing.

19:56.430 --> 20:00.840
Once we set value to positive let's say duration will be two.

20:00.840 --> 20:02.730
And we're going to give here two.

20:02.880 --> 20:04.950
This is the time you're going to stand on the ground.

20:04.950 --> 20:06.000
We're not going to walk.

20:06.000 --> 20:09.240
And then once it's less than zero then we can walk again.

20:09.240 --> 20:10.320
That's the goal.

20:10.680 --> 20:11.910
How are we going to achieve this.

20:11.910 --> 20:17.820
Let's go to enemy mushroom and we can override the flip method.

20:18.210 --> 20:20.310
Override flip.

20:20.310 --> 20:27.120
And once we flip we're going to do idle timer equals to idle duration.

20:27.120 --> 20:33.420
So every time we flip the character we're also going to uh set idle timer to idle duration.

20:33.420 --> 20:37.440
If you would like you can just open curly brackets and do it here.

20:37.440 --> 20:38.670
That would work as well.

20:41.100 --> 20:42.630
Probably makes more sense.

20:44.970 --> 20:48.810
And then we need to stop movement.

20:48.810 --> 20:55.830
If idle timer is, uh, less than zero, we can do that simply by going to handle movement and type.

20:55.830 --> 20:59.880
If idle timer is less than zero, then return.

20:59.880 --> 21:05.430
Uh, so every time we flip, we're going to set idle time and mushroom going to stand and wait.

21:05.430 --> 21:08.100
Let's give it idle duration of 1.5.

21:08.100 --> 21:09.300
And let's see this.

21:10.130 --> 21:11.810
You're gonna walk there.

21:13.270 --> 21:14.440
Come on, man, why?

21:14.440 --> 21:15.460
He's not working.

21:15.490 --> 21:16.600
Did I make a mistake?

21:16.600 --> 21:18.160
I think I did make a mistake.

21:29.250 --> 21:29.610
Yeah.

21:29.610 --> 21:32.400
If idle time are bigger than zero, then we return.

21:32.400 --> 21:34.680
So if we still have idle time, we cannot walk.

21:34.710 --> 21:36.390
Yes, this is how it should be.

21:36.390 --> 21:37.350
I'm sorry.

21:37.470 --> 21:39.450
This is how it should be.

21:39.930 --> 21:41.100
Let's go.

21:45.990 --> 21:53.100
And he works there and then he's like flipping and waiting for a while, then walks back.

21:53.880 --> 21:55.860
Also, I've seen a bit of a slide.

21:55.860 --> 21:57.390
I didn't really like that.

21:59.210 --> 22:01.370
Uh, let me make his speed faster.

22:02.770 --> 22:06.760
And he's like, well, that's not what we need.

22:07.030 --> 22:09.460
Uh, let's go to enemy mushroom.

22:09.940 --> 22:11.260
And I think.

22:11.710 --> 22:17.320
We can do RB velocity equals to new vector or just vector zero vector zero.

22:17.320 --> 22:21.010
So we're going to stop his velocity once he is leaping.

22:21.280 --> 22:22.750
Yeah that would be a bit better.

22:22.750 --> 22:26.200
Also, let's apply this only if ground is detected.

22:29.270 --> 22:32.750
And that would remove Jitterin when he's falling off the ground.

22:33.050 --> 22:34.250
Let's go back.

22:35.010 --> 22:37.890
Let's say he moves faster and then he is like stopping.

22:38.840 --> 22:39.890
Okay.

22:42.290 --> 22:43.430
Very good.

22:45.760 --> 22:46.330
Nice.

22:46.330 --> 22:51.070
I'm going to give it smooth speed of, uh, two because I like how it worked.

22:51.430 --> 22:52.630
Let me check something.

22:52.630 --> 22:54.850
I want to see if he's in the air.

22:54.850 --> 22:55.900
What's he going to do?

22:57.020 --> 22:59.210
For any reason he could be in the air.

22:59.840 --> 23:01.310
He's flipping endlessly.

23:02.470 --> 23:03.910
Okay.

23:04.210 --> 23:05.710
How could we stop this?

23:05.920 --> 23:08.500
Well, just to be sure, let's do this.

23:08.500 --> 23:16.330
We're gonna go to enemy and rename this to Ground forward detected or ground in front.

23:16.510 --> 23:20.650
Um, is in front detected.

23:20.650 --> 23:24.640
And then we're going to make one more check for the ground that it's going to be two different checks.

23:24.640 --> 23:27.310
Private ball is grounded.

23:27.310 --> 23:31.540
Then we're going to go back to the handle.

23:31.540 --> 23:35.080
Collision and do is grounded equals to physics.

23:37.410 --> 23:38.940
2D raycast.

23:40.790 --> 23:45.770
And this one can be done from a transform position vector two dot down.

23:47.090 --> 23:49.880
Uh, ground check distance as well, I think.

23:49.880 --> 23:50.930
What is ground?

23:50.930 --> 23:51.770
Nice.

23:51.770 --> 23:53.840
And then we're going to need the gizmos.

23:53.840 --> 23:56.930
So I'm going to just duplicate this line of code.

23:58.230 --> 24:00.270
Change it to a transform position.

24:00.270 --> 24:03.240
So we basically add in one more collision check.

24:03.240 --> 24:07.320
And if character is not grounded, he will not be able to flip.

24:10.080 --> 24:10.740
Yeah.

24:10.740 --> 24:12.150
This is what I want to do.

24:12.650 --> 24:15.170
So everything works the same over here.

24:15.320 --> 24:17.810
Except that over here.

24:22.120 --> 24:22.390
How?

24:22.390 --> 24:24.400
Maybe even here I can do.

24:24.970 --> 24:25.180
Wait.

24:25.480 --> 24:26.740
Let's do if.

24:27.450 --> 24:30.270
He is grounded equals to false, then return.

24:31.000 --> 24:31.630
Yeah.

24:33.010 --> 24:34.960
And then we do need it over here.

24:36.230 --> 24:37.700
But now we're going to check how it's working.

24:37.700 --> 24:38.600
Then I'm going to take a look.

24:38.600 --> 24:42.920
If we clean this up and we're going to move to the next video, and I know it took a bit longer than

24:42.920 --> 24:47.600
it like we wish it would take, but we did set up for other enemies.

24:47.600 --> 24:49.730
So it's good.

24:49.760 --> 24:50.660
Now he's walking.

24:50.660 --> 24:53.780
They're leaping, going back.

24:53.780 --> 24:58.670
If I take him and pull him into the air, he's going to just fall.

24:58.700 --> 24:59.240
Cool.

24:59.240 --> 25:00.170
This is what we need.

25:00.170 --> 25:00.740
All right?

25:00.740 --> 25:01.340
I like it.

25:01.340 --> 25:01.760
Now.

25:01.760 --> 25:04.520
If you would like, you could make him like.

25:04.520 --> 25:05.180
You would make.

25:05.210 --> 25:09.080
You could make a possibility of a trampoline bounce in the enemies.

25:09.560 --> 25:11.660
But that would take another ten minutes.

25:11.660 --> 25:13.730
And I want to spend time on other enemies.

25:13.730 --> 25:18.890
Maybe we can do this with a polish, but if you want to do it yourself, you basically make a method

25:18.890 --> 25:21.710
for the enemy mushroom, same as we did for the player.

25:21.860 --> 25:22.820
Like.

25:24.180 --> 25:25.080
This one.

25:25.500 --> 25:26.520
Push method.

25:26.520 --> 25:27.480
Where is the push?

25:29.730 --> 25:30.150
Push.

25:30.150 --> 25:33.900
So you take this functionality, give it to the enemy mushroom.

25:33.900 --> 25:36.330
Then on a trampoline you detect if.

25:38.740 --> 25:43.960
Player or enemy walked into the trampoline and then just call the method and bounce.

25:43.960 --> 25:46.180
The mushroom would be fun I'm sure.

25:46.360 --> 25:48.610
Anyway, this is a great job!

25:48.610 --> 25:50.080
Thank you so much for paying attention.

25:50.080 --> 25:51.640
Oh, let me hide this timer.

25:54.970 --> 25:56.020
We don't need serialize field.

25:56.020 --> 25:57.670
Now we know how it works.

25:57.670 --> 25:59.110
And on enemy mushroom.

25:59.110 --> 25:59.710
Give me a second.

25:59.710 --> 26:01.540
I'll see if we can clean this up.

26:02.430 --> 26:02.760
Okay.

26:02.760 --> 26:04.230
We don't need a wake.

26:12.170 --> 26:22.940
And I think we can we can remove this from here, extract it as a extract method and they'll turn around.

26:22.940 --> 26:25.970
That's what I wanted to call and I'll do if.

26:26.890 --> 26:27.220
Uh.

26:27.940 --> 26:29.230
He's grounded.

26:29.350 --> 26:31.720
Poles then return.

26:32.840 --> 26:35.000
Or maybe let's just do if is grounded.

26:36.660 --> 26:38.100
Then handle turnaround.

26:38.100 --> 26:38.910
Okay?

26:39.000 --> 26:42.060
Yeah, this is a bit cleaner, I like it.

26:42.060 --> 26:45.270
This should be for the, like, handle animations.

26:45.270 --> 26:46.110
Could we do that?

26:46.110 --> 26:47.220
What do you think?

26:47.220 --> 26:48.810
Well, let's keep it like that for now.

26:48.810 --> 26:49.320
It's good.

26:49.320 --> 26:50.730
I'll see you in the next video.
