WEBVTT

00:00.080 --> 00:00.530
Hello guys.

00:00.530 --> 00:01.490
Welcome to the video.

00:01.490 --> 00:03.320
In this one we're going to make another enemy.

00:03.320 --> 00:05.840
This time it's going to be enemy that shoots bullets.

00:05.840 --> 00:07.940
It's going to be easy to do quite fast one.

00:07.940 --> 00:12.890
And I want to say that I guess we're going to make maybe 2 or 3 more enemies, and then we're going

00:12.890 --> 00:16.520
to be done with enemies for now, because we need to develop other parts of the game.

00:16.520 --> 00:21.350
We cannot make enemies only, and I think this setup will be enough to make a base game, and other

00:21.350 --> 00:25.250
enemies can be as additions later on, sort of a, you know, DLC.

00:25.280 --> 00:31.550
Okay, so I'm going to remove a chicken and I'm going to remove Rhino and I'm going to duplicate the

00:31.550 --> 00:32.360
mushroom.

00:32.510 --> 00:35.840
Let me duplicate it, move it down over here.

00:35.900 --> 00:38.900
Then we're going to do a prefab.

00:39.620 --> 00:41.270
Unpack completely.

00:42.220 --> 00:44.710
Let's change the name to Enemy Plant.

00:45.720 --> 00:52.710
Then we want to replace sprite on a sprite renderer we need to go to graphics enemies.

00:54.440 --> 00:55.370
Plant.

00:56.410 --> 01:00.820
Let's take the idol and make sure they all have pixel per unit 16.

01:01.030 --> 01:06.220
Then let's take one of the sprites and drag them on a sprite renderer over here.

01:06.250 --> 01:07.210
Very good.

01:07.240 --> 01:11.140
Now we need to reset Collider and we can take away this script.

01:11.140 --> 01:12.280
So I'm going to remove it.

01:13.630 --> 01:18.700
Gonna reset Collider and going to adjust it to something like.

01:19.940 --> 01:20.900
This.

01:20.900 --> 01:21.350
Yeah.

01:21.350 --> 01:22.040
Very nice.

01:22.040 --> 01:23.870
Now let's take damage trigger.

01:23.870 --> 01:24.980
Make it.

01:26.940 --> 01:28.710
Wider like so.

01:28.710 --> 01:29.400
Maybe.

01:31.020 --> 01:32.610
Yeah, this should be fine.

01:32.670 --> 01:34.230
Now I'm going to move it down.

01:34.230 --> 01:36.630
And we need an animator controller for this one.

01:36.630 --> 01:37.950
And script.

01:38.130 --> 01:40.620
And let's go quickly make a script.

01:42.210 --> 01:44.820
Can I name it as enemy plant?

01:46.980 --> 01:48.540
I'm gonna put it on the enemy.

01:52.110 --> 01:53.460
I'm going to put it on the enemy.

01:53.460 --> 01:57.420
And now we're going to go to animations enemies.

01:57.660 --> 01:59.730
We need a new folder for plant.

02:01.040 --> 02:07.760
And let's go inside and let's make animator controller enemy land.

02:08.960 --> 02:09.650
Okay.

02:09.950 --> 02:13.760
Gonna drag it to this field over here instead of mushroom.

02:13.790 --> 02:17.630
Then we're going to go to Animation Window and create a couple of new clips.

02:17.630 --> 02:20.990
One of them going to be Enemy Plant Idle.

02:25.130 --> 02:26.840
We need to go to graphics folder.

02:26.840 --> 02:28.310
Enemies land.

02:28.310 --> 02:30.590
Let's take the idle sprites.

02:30.830 --> 02:31.610
Put them here.

02:31.610 --> 02:33.110
Sample rate 20.

02:33.230 --> 02:35.330
Now we're going to go and make a new clip.

02:35.360 --> 02:38.330
Enemy plant attack.

02:40.330 --> 02:42.250
Let's take the attack animation.

02:42.250 --> 02:43.300
Drag it here.

02:43.300 --> 02:45.100
Sample rate 20.

02:45.770 --> 02:48.920
And we need one more hit.

02:49.010 --> 02:53.390
So I'm going to do create new clip enemy planned hit.

02:54.050 --> 02:56.180
I'm going to take these sprites drag them here.

02:56.180 --> 02:57.950
Sample rate 20.

02:58.100 --> 03:04.190
Before we go to animator we need to go to Asset Animations Enemies plant.

03:04.190 --> 03:05.570
We're going to take these.

03:06.820 --> 03:10.330
Hit and attack and we need to uncheck loop time over here.

03:10.330 --> 03:11.530
This is important.

03:11.890 --> 03:13.630
Okay this is good.

03:13.630 --> 03:17.200
Now we can go to animator of the enemy plant.

03:19.330 --> 03:21.460
We're going to have idle by default.

03:22.410 --> 03:24.600
And we're going to need two triggers.

03:24.600 --> 03:31.260
One of them going to be hit to trigger animation of the death, and another one going to be trigger

03:31.260 --> 03:34.230
attack to trigger attack animation.

03:34.230 --> 03:37.890
Let's make transition from any state to enemy planned hit.

03:39.100 --> 03:39.940
Over here.

03:39.940 --> 03:45.190
Hit and transition duration zero and transit to self uncheck.

03:47.040 --> 03:50.550
Then for the attack, we're going to transfer to attack.

03:50.670 --> 03:53.610
If trigger was called, let's select it.

03:53.610 --> 03:55.500
Choose attack trigger.

03:56.460 --> 03:57.720
And check exit time.

03:57.720 --> 03:59.580
Transition duration zero.

03:59.670 --> 04:04.740
Then we're going to transfer back to idle after let's say one.

04:04.740 --> 04:06.270
So exit I'm going to be one.

04:06.270 --> 04:07.710
Transition duration zero.

04:07.710 --> 04:10.410
We're going to play animation once and then go back to idle.

04:13.150 --> 04:14.020
Very good.

04:14.020 --> 04:15.970
Most of the functionality is ready.

04:15.970 --> 04:17.890
Let's go to enemy plant.

04:18.690 --> 04:20.370
Let's go inside of a script.

04:25.790 --> 04:26.330
Over here.

04:26.330 --> 04:28.130
We're going to inherit from the enemy.

04:29.550 --> 04:30.390
Enemy.

04:30.390 --> 04:34.290
And then we really have most of the functionality.

04:34.290 --> 04:36.450
Let's override update.

04:37.160 --> 04:39.770
And let's make a method private void.

04:39.770 --> 04:40.400
Um.

04:40.640 --> 04:41.480
Attack.

04:43.960 --> 04:47.410
This method will set trigger of the attack parameter.

04:48.220 --> 04:55.450
Then in the update I'm going to do, if player detected is player detected then attack.

04:56.560 --> 04:57.190
All right.

04:57.190 --> 04:59.650
So basically this is enough to make an attack.

04:59.650 --> 05:01.030
It's not going to be perfect.

05:01.030 --> 05:02.530
We still need to do a bunch of things.

05:02.530 --> 05:04.900
But for now let's go and see this.

05:04.900 --> 05:07.540
Let's see how fast it was and how easy.

05:07.750 --> 05:11.650
Let me do close all by this and let's go back to unity.

05:14.750 --> 05:16.880
We have a plant here.

05:16.880 --> 05:17.720
Let's make.

05:17.720 --> 05:18.620
What is ground?

05:18.620 --> 05:19.280
Ground?

05:19.280 --> 05:20.390
Let's assign ground.

05:20.390 --> 05:21.980
Check over here.

05:22.220 --> 05:23.510
What is player?

05:23.510 --> 05:24.620
Player.

05:24.620 --> 05:26.690
And I think.

05:29.510 --> 05:30.830
Uh, what else do we need?

05:30.860 --> 05:35.240
We can take ground check and move it down a little bit, but it's not as important because the enemy

05:35.240 --> 05:36.950
plan is not going to move anywhere.

05:36.950 --> 05:41.840
So these parameters are here, just, you know, just to be there.

05:41.840 --> 05:42.530
Really.

05:44.130 --> 05:47.160
Okay, uh, let's go to play mode.

05:50.050 --> 05:52.420
So plant stands in idle.

05:52.480 --> 05:53.980
I actually bit in the air.

05:53.980 --> 05:55.360
So we're going to fix that.

05:56.160 --> 05:59.790
And I'm going to jump and be triggering attack animation.

05:59.880 --> 06:02.460
For now, this is quite fast and that is the problem.

06:02.460 --> 06:04.290
And we're going to fix it also.

06:04.290 --> 06:10.800
By the way, there is a message parameter x velocity does not exist on the animator because this one

06:10.800 --> 06:14.460
now is uh, in the method handle animator.

06:16.210 --> 06:18.850
And it is called in the update all the time.

06:20.030 --> 06:23.240
And it is also called on the update over here.

06:23.240 --> 06:27.320
So somehow we need to solve this problem over here.

06:27.320 --> 06:29.120
How can we solve this?

06:29.480 --> 06:32.330
Uh, we don't need this enum set float on the enemy planned.

06:32.330 --> 06:33.080
Really?

06:33.620 --> 06:35.000
Uh, let's try this.

06:35.000 --> 06:36.260
Let's go to enemy planned.

06:36.260 --> 06:37.010
Let's go.

06:37.010 --> 06:39.290
Override handle animator.

06:39.290 --> 06:41.690
And over here we're going to remove base.

06:41.690 --> 06:43.040
So now there's going to be empty.

06:43.040 --> 06:48.920
Animator and enemy plant will call this method in the update instead of the one that is in the base.

06:48.920 --> 06:51.500
And because it is empty we're not going to do anything.

06:51.500 --> 06:52.040
So.

06:52.860 --> 06:53.580
Keep it empty.

06:57.180 --> 07:00.090
You need to update parameters.

07:00.480 --> 07:00.990
Okay.

07:00.990 --> 07:01.890
Very good.

07:02.100 --> 07:04.140
Now, uh, let's go back.

07:06.170 --> 07:08.390
Now let's go back and.

07:09.200 --> 07:11.900
Fix that collider on the enemy plant.

07:12.980 --> 07:15.290
Gonna just raise it a bit like so.

07:16.160 --> 07:17.690
And take the plant itself.

07:17.690 --> 07:18.590
Move it up.

07:23.860 --> 07:25.480
Yeah, this should be good.

07:25.480 --> 07:28.570
Now, let's keep talking about the attack functionality.

07:28.570 --> 07:32.830
We need to make cooldown for the attack because now it is too often for us, right?

07:32.830 --> 07:34.060
We don't need it like that.

07:38.390 --> 07:41.120
Uh, for that we can use time dot time.

07:41.120 --> 07:47.510
If you never heard of a time dot time, it basically returns the info of the current time in the game

07:47.510 --> 07:49.010
since the game begun.

07:49.010 --> 07:52.160
Let's make, uh, header.

07:53.480 --> 07:54.980
Land details.

07:54.980 --> 07:57.080
And over here I'm going to make public float.

07:57.080 --> 07:59.780
Last time attacked.

08:01.290 --> 08:06.690
Then every time we attack, I'm going to do last time attacked equals to time dot time.

08:07.110 --> 08:12.960
Now let's go back to unity and see what's the value is equals to when plant is attacking.

08:13.590 --> 08:16.440
So from the start of the game we can see it is zero.

08:16.440 --> 08:17.970
We're going to go to play mode.

08:19.300 --> 08:20.200
He is.

08:20.200 --> 08:21.070
It is zero.

08:21.070 --> 08:24.820
I'm going to jump into the view of the plant and we have 3.5.

08:24.820 --> 08:28.360
And every time he attacks, value is updated.

08:29.870 --> 08:30.560
Right.

08:31.520 --> 08:38.540
So we can use it to detect if plant is ready to attack again once cooldown is over, or if cooldown

08:38.540 --> 08:39.380
is not over.

08:39.380 --> 08:44.300
We can do that simply by checking time dot time at the moment of his attack.

08:44.300 --> 08:50.660
And if time to time at the moment of his attack is bigger than last time attacked, plus cooldown,

08:50.660 --> 08:52.940
then we are ready to attack again.

08:54.270 --> 08:57.660
Uh, I'm going to go here and quickly try to explain.

08:57.660 --> 08:58.920
Let's make.

09:00.740 --> 09:02.420
Let's make a boolean over here.

09:02.420 --> 09:10.580
Bool can attack and it's going to be equals to Time.time is bigger than last time attacked plus cooldown.

09:10.580 --> 09:13.010
And I guess we can have a dedicated variable for this.

09:13.010 --> 09:17.060
So I'm going to do serialize field private float attack cooldown.

09:17.060 --> 09:21.230
Let's make it equals to 1.5 by default and use it over here.

09:22.320 --> 09:23.610
Now how it's going to work.

09:23.610 --> 09:25.710
Let me try to explain real fast.

09:25.710 --> 09:27.420
I'm going to put some comments here.

09:27.420 --> 09:33.000
And let's say when we attacked for the first time over here, we registered last time, attacked into

09:33.000 --> 09:34.980
this variable, last time attacked.

09:34.980 --> 09:42.000
So we attacked when it was zero five seconds in the game.

09:42.870 --> 09:43.380
Last time.

09:43.380 --> 09:45.870
Attack is zero five seconds in the game.

09:45.870 --> 09:46.410
Okay.

09:48.150 --> 09:51.270
Next time we try to attack, let's say time to time.

09:51.270 --> 09:57.300
The time in the game is equals to seven, time in the game is seven and we try to attack and we see

09:57.300 --> 09:58.410
if seven.

09:59.170 --> 09:59.950
If seven.

09:59.950 --> 10:01.210
The current time in the game.

10:01.210 --> 10:01.930
Time the time.

10:01.930 --> 10:02.830
This is just example.

10:02.830 --> 10:03.970
I'm trying to explain it this way.

10:03.970 --> 10:12.070
Okay, so if it is bigger than five plus attack cooldown 1.5 F means we are ready to attack again because

10:12.070 --> 10:14.260
seven is definitely bigger than 6.5.

10:14.260 --> 10:20.770
But if we would do this test in the time of let's say six seconds since the start of the game, then

10:20.770 --> 10:23.230
this would be false and we are not ready to attack again.

10:23.230 --> 10:24.550
This is how it works.

10:25.820 --> 10:26.600
So we're here.

10:26.600 --> 10:28.820
We're going to place and can attack.

10:29.480 --> 10:34.520
This is very good because this is basically all what we need to make a cooldown for this attack.

10:34.730 --> 10:36.140
I'm going to go ahead.

10:37.230 --> 10:38.340
And try this.

10:38.340 --> 10:41.040
Let's see this how it's working right now.

10:41.370 --> 10:45.600
Uh, we're going to just jump in front of the plant and we're going to see the time of the last time

10:45.600 --> 10:48.360
attacked and also the frequency of his attacks.

10:53.840 --> 10:58.370
And now we can see that last time attacked is bigger by 1.5.

10:58.370 --> 11:03.710
Every time we use an attack, it's a bit difficult to catch because values are so random.

11:04.100 --> 11:07.910
But let's say I'm going to make it 25 over here.

11:09.170 --> 11:09.890
Okay.

11:11.970 --> 11:13.350
26.5.

11:13.380 --> 11:14.280
28.

11:14.310 --> 11:15.900
29.5.

11:15.930 --> 11:17.190
31 and so on.

11:17.190 --> 11:19.920
So we do attack every 1.5 seconds.

11:19.920 --> 11:21.840
And that is exactly what we need.

11:22.230 --> 11:24.270
Uh, now, let's make bullets for him.

11:24.270 --> 11:28.260
Let's make him shoot bullets and see if this is too easy for the player.

11:28.260 --> 11:30.360
And then we're going to proceed with development.

11:30.360 --> 11:32.100
How do we make a bullet?

11:32.370 --> 11:34.620
Uh, we need asset graphics.

11:34.620 --> 11:35.730
Enemies.

11:36.420 --> 11:37.080
Plant.

11:37.080 --> 11:39.420
And we need this bullet sprite over here.

11:40.470 --> 11:44.490
Okay, let's bring it to the, uh, enemies layer.

11:46.050 --> 11:52.620
Let's rename it to Enemy Bullet because this one will be used by another enemy as well.

11:54.540 --> 11:57.060
Then on the bullet we need rigidbody 2D.

11:58.950 --> 12:02.100
We need to switch off gravity scale for this object.

12:02.100 --> 12:07.260
Also, let's make a interpolate interpolate collision detection continuous.

12:07.710 --> 12:08.640
Very good.

12:08.640 --> 12:10.650
Now we need circle Collider.

12:12.810 --> 12:14.430
Let's make it smaller.

12:14.430 --> 12:17.550
And I know that point three should be okay.

12:18.240 --> 12:19.080
Very good.

12:19.080 --> 12:20.790
Now we need a script for this.

12:20.790 --> 12:23.610
I want to make a new script for enemy bullet.

12:24.330 --> 12:25.380
Sharp script.

12:25.380 --> 12:27.330
Enemy bullet.

12:32.320 --> 12:34.720
Let's place it on the enemy bullet.

12:34.720 --> 12:35.560
Over here.

12:36.390 --> 12:41.280
This will be the object that we use as a prefab and create every time plant is attacking.

12:41.460 --> 12:46.650
We need to go to prefabs and turn it into a prefab like so.

12:47.200 --> 12:48.070
Very good.

12:48.070 --> 12:50.110
Now we can delete it from the scene.

12:51.140 --> 12:53.990
We're going to go to enemy plant and do something here.

12:53.990 --> 12:59.090
First of all, we want to hide this variable because now we know how it is working.

12:59.120 --> 13:02.840
Now we want to have reference to a GameObject prefab.

13:02.840 --> 13:04.130
So serialize field.

13:04.130 --> 13:08.060
Private GameObject bullet prefab.

13:10.180 --> 13:16.900
And we need sort of a gun point, because if we use the, um, transform position of the plant.

13:18.210 --> 13:22.200
Then bullet will be created over here and it will fly from here.

13:22.200 --> 13:24.840
But we want it to fly from here, right?

13:28.260 --> 13:31.020
So let's go to script.

13:32.810 --> 13:33.860
Let's make serialize.

13:33.860 --> 13:34.250
Field.

13:34.250 --> 13:34.730
Private.

13:34.730 --> 13:35.660
Transform.

13:36.260 --> 13:37.970
Gunpoint.

13:38.510 --> 13:39.320
Nice.

13:39.320 --> 13:40.100
Very good.

13:40.100 --> 13:42.080
Now let's go and make a method.

13:42.620 --> 13:43.490
Private void.

13:43.490 --> 13:47.750
Create bullet, in which we're going to instantiate the object.

13:47.900 --> 13:50.540
And before we did instantiate the game object.

13:51.080 --> 13:53.720
But now we're going to need to instantiate game object.

13:53.720 --> 13:58.130
And then we need to get access to rigid body of the bullet or script of the bullet.

13:58.130 --> 13:59.750
So we can give it a velocity.

13:59.750 --> 14:05.450
Because of that, I think I'm going to create a variable of an enemy bullet as a local variable over

14:05.450 --> 14:06.080
here.

14:07.680 --> 14:10.380
And it's going to be equals to instantiate.

14:12.060 --> 14:13.380
Bullet prefab.

14:14.110 --> 14:15.220
At gunpoint.

14:15.460 --> 14:18.820
Position with the quaternion identity.

14:20.470 --> 14:25.930
And over here, instead of having variable of a game object, we can make a variable of enemy bullet.

14:25.960 --> 14:31.420
Now this will be instantiated and together with it we can get access to the script and do something

14:31.420 --> 14:31.930
with it.

14:31.930 --> 14:34.000
But let's not do anything for now.

14:36.540 --> 14:40.470
Let's move to enemy bullet script for a bit.

14:40.470 --> 14:42.000
Let's go to enemy bullet.

14:42.390 --> 14:46.620
So on the bullet, we're going to need a reference to the rigidbody.

14:46.650 --> 14:50.070
Let's make a private rigidbody to the RB.

14:50.580 --> 14:55.380
And we need a wake, which would just get component of a rigidbody.

14:56.240 --> 14:59.690
Then we need a method that we can call to set up velocity of the bullet.

14:59.720 --> 15:02.870
I assume we're going to use this script for other bullets as well.

15:02.870 --> 15:05.930
That fly not on the left and right, but also up and down.

15:05.930 --> 15:13.250
So I'm going to make a public void set velocity which would take a vector two as a variable.

15:13.250 --> 15:16.850
And then over here we're going to do RB velocity equals to velocity.

15:16.850 --> 15:19.490
Then we're going to need ontriggerenter 2d.

15:19.580 --> 15:25.790
And here if bullet is colliding with player, it's supposed to kick the player and then destroy itself.

15:25.790 --> 15:29.450
And if it's colliding with the ground, it's supposed to just destroy itself.

15:29.450 --> 15:36.770
So this is what we're going to do if collision GameObject dot player is equals to layer mask name to

15:36.770 --> 15:37.220
layer.

15:37.220 --> 15:40.730
And here we need to be sure we don't make any mistake I'm going to do.

15:40.760 --> 15:40.940
Yeah.

15:40.940 --> 15:42.140
Let me do string here.

15:42.140 --> 15:44.420
So it's easier to double check.

15:44.450 --> 15:50.060
And I do serialize field private string player layer name.

15:50.060 --> 15:52.310
It's going to be player by default.

15:52.550 --> 15:55.250
And also we're going to need ground layer name.

15:58.660 --> 15:59.860
Ground layer name.

15:59.860 --> 16:01.840
It's going to be ground by default.

16:03.020 --> 16:03.470
Okay.

16:03.470 --> 16:07.910
This is another way to detect collision between objects by using layers.

16:07.910 --> 16:10.010
So over here we can just.

16:11.250 --> 16:13.440
Say that if we collide it with a.

16:15.130 --> 16:16.720
Layer layer name.

16:16.930 --> 16:21.100
Then we're going to do collision get component of a layer.

16:21.700 --> 16:23.620
And then we're going to do um.

16:25.620 --> 16:26.640
What was that?

16:27.170 --> 16:28.040
Knockback.

16:30.750 --> 16:31.470
What's up boy?

16:31.650 --> 16:32.730
Oh, and we need to give it.

16:32.730 --> 16:33.540
Okay.

16:33.540 --> 16:36.480
Transform position dot x.

16:39.460 --> 16:42.310
And then we're going to do destroy game object.

16:43.080 --> 16:50.130
Then if collision game object layer is equals to layer mask named layer.

16:51.070 --> 16:56.020
Ground layer name, then we're going to just destroy game object.

16:56.020 --> 16:57.040
Simple as that.

16:57.040 --> 16:58.570
Again, it could be done differently.

16:58.570 --> 17:01.450
It could be done same way as we did with the damage trigger.

17:01.450 --> 17:07.900
Could be just by using layer matrix in a preference of unity, like in a project settings.

17:08.560 --> 17:08.680
Uh.

17:08.680 --> 17:09.580
Over here.

17:11.590 --> 17:17.050
Our project settings, we could set up collision between Bullet and Player only, but this time I want

17:17.050 --> 17:20.440
to show you how to use, uh, name of the layer to detect collisions.

17:20.440 --> 17:23.320
So this is just one of the ways of doing it.

17:23.320 --> 17:23.920
Okay.

17:24.370 --> 17:27.220
Now let's go to enemy plant.

17:27.220 --> 17:32.260
And over here, after we create a bullet, we're going to do new bullet set velocity.

17:32.260 --> 17:34.240
And here we need bullet speed.

17:36.260 --> 17:40.700
So let's make serialize field private float, bullet speed.

17:40.700 --> 17:42.800
Let's make it equals to seven by default.

17:42.800 --> 17:44.600
And we're going to do bullet speed.

17:45.680 --> 17:47.930
Multiplied by facing direction.

17:47.930 --> 17:50.330
And for the Y, we're going to use zero.

17:53.560 --> 17:55.510
I think we need to turn it into vector two.

17:55.540 --> 17:56.920
I forgot, let's do.

17:58.290 --> 17:59.070
Victor two.

17:59.100 --> 18:01.440
Bullet speed equals two.

18:02.200 --> 18:07.000
New vector tool facing direction multiplied by bullet speed with a zero on the Y.

18:07.030 --> 18:12.940
Okay, when something like this happens, it means you're using same name on a local variable and on

18:12.940 --> 18:14.110
a global variable.

18:14.110 --> 18:16.900
One of the ways to fix it is to use different name.

18:17.050 --> 18:23.170
Or you can just type this over here and then it will use global variable.

18:23.170 --> 18:28.810
From this script you can see it says class enemy planned bullet speed.

18:29.830 --> 18:32.920
But not to confuse us more than we already did.

18:32.950 --> 18:38.290
I'm going to change the name of the variable over here to, um, new velocity.

18:39.990 --> 18:43.590
Okay, now over here we're going to pass new velocity.

18:43.590 --> 18:46.650
Or maybe it's better to use bullet velocity.

18:47.720 --> 18:50.210
Let's rename it Bullet Velocity.

18:50.660 --> 18:56.030
And just in case bullet will fly off the level, maybe you will not have, uh, walls on your level.

18:56.030 --> 19:01.550
We need to destroy the bullet after some delay so we don't create lots of objects, and then they will

19:01.550 --> 19:02.360
fly forever.

19:02.360 --> 19:10.880
So let's do destroy, um, new bullet, a new bullet game object after, let's say 10s.

19:10.880 --> 19:12.830
Okay, now we need to call this method.

19:12.830 --> 19:16.520
At the moment of the attack, we're going to go back to unity.

19:16.520 --> 19:20.120
Find the animation on the enemy plant.

19:20.120 --> 19:22.820
Let's go to enemy plant attack.

19:23.240 --> 19:24.740
Find the frame of the attack.

19:24.740 --> 19:26.810
So I guess it is here.

19:26.810 --> 19:27.410
Yeah.

19:27.410 --> 19:33.080
Over here we're going to add event and choose enemy plant methods.

19:33.080 --> 19:34.400
Create bullet.

19:35.400 --> 19:37.080
Then we need a gunpoint.

19:37.080 --> 19:40.740
So I'm going to make him at gunpoint.

19:42.040 --> 19:44.500
Going to drag it over here.

19:44.920 --> 19:47.710
Let me move this gun point somewhere here.

19:47.710 --> 19:51.610
So bullet will start here and it will fly to the facing direction.

19:51.610 --> 19:56.080
Then on the enemy plant, we need to open Project Asset Prefab.

19:56.990 --> 19:59.450
And assign enemy bullets over here.

20:03.310 --> 20:04.810
Okay, this is done.

20:04.810 --> 20:06.220
Let's go and see this.

20:09.110 --> 20:10.370
And there is the enemy.

20:10.400 --> 20:11.420
I'm standing there.

20:11.420 --> 20:12.320
He shoots!

20:14.000 --> 20:14.780
Oh my God!

20:16.380 --> 20:17.700
This is so random.

20:18.300 --> 20:19.500
Uh, we need to.

20:19.530 --> 20:21.330
We need to go to enemy bullet.

20:22.440 --> 20:24.510
And check is trigger.

20:26.080 --> 20:27.460
Okay, let's go back.

20:30.620 --> 20:33.050
Now he'll shoot me and damage me.

20:34.580 --> 20:39.440
And if we jump away, then it will be destroyed on collision with a wall.

20:41.290 --> 20:42.100
Cool, right?

20:42.100 --> 20:45.700
I think it's destroyed too early when it's colliding with a wall.

20:45.700 --> 20:50.620
So maybe I will do a bit of a trick here on the enemy bullet.

20:50.620 --> 20:52.210
Let's do delay here.

20:52.210 --> 20:59.770
Maybe of 0.05 F, like a really small delay, and it will destroy bullet a bit later than it's supposed

20:59.770 --> 21:00.190
to be.

21:00.190 --> 21:03.460
And because of that, it's going to look like it flying into the ground.

21:03.550 --> 21:05.380
Now let's go ahead and see this.

21:05.410 --> 21:07.150
I'm going to jump in front of the enemy.

21:07.910 --> 21:08.900
Going to get damage.

21:08.900 --> 21:09.500
Okay.

21:09.500 --> 21:10.160
Very good.

21:10.160 --> 21:14.390
Now when I'm jumping away, going to be destroyed on hit with a wall.

21:14.390 --> 21:18.080
Would be nice to make some sort of visual effect, but now it's not that important.

21:18.080 --> 21:20.840
So we're going to do it later on a Polish stage.

21:20.840 --> 21:22.190
But now we need to do something else.

21:22.190 --> 21:23.480
And it is very important.

21:23.480 --> 21:29.870
We need to be able to flip enemies in the editor before we start the game, because as of now, my plan

21:29.870 --> 21:31.220
can look only to the left.

21:31.220 --> 21:35.990
But what if I want to place him over here, for example, or over here?

21:35.990 --> 21:39.800
Then I would have to go back to the script, change his facing direction.

21:39.800 --> 21:40.640
Lots of headache.

21:40.640 --> 21:42.920
So this is what I offer you to do.

21:42.950 --> 21:49.340
We can flip the sprite on the X in the editor, just so we can see where enemies should be facing.

21:49.340 --> 21:54.740
Then in the script we can read this parameter and flip him if we need to do so.

21:54.740 --> 21:56.270
So this is how it's going to be.

21:56.270 --> 21:58.790
We're going to go to the script of enemy.

22:00.690 --> 22:02.400
Let's go all the way down.

22:03.780 --> 22:06.210
Uh, maybe somewhere here.

22:06.210 --> 22:07.650
We're going to make a.

22:08.940 --> 22:13.920
We're going to make a public void lip default facing direction.

22:13.920 --> 22:15.060
It's a bit long, but it's okay.

22:15.060 --> 22:16.050
It will do the job.

22:16.050 --> 22:22.410
Now inside we need access to the sprite renderer, so I think we can go all the way above.

22:23.850 --> 22:29.520
Let's make a private sprite sprite renderer.

22:31.580 --> 22:36.020
SR and it's going to return us get component of a sprite renderer.

22:36.600 --> 22:37.680
Just like that.

22:37.710 --> 22:40.290
Let me clean this up because there is lots of extra stuff.

22:40.290 --> 22:43.620
Then we can go down back to this method.

22:43.620 --> 22:50.880
And over here we're going to do uh SR flip x equals to not sr flip x.

22:50.880 --> 22:54.600
So it will set the value of that boolean to an opposite value.

22:54.750 --> 23:00.390
And we need to call this method from the editor so we can type here Contextmenu.

23:01.570 --> 23:05.500
Lip facing direction or change facing direction.

23:07.260 --> 23:08.400
Just like that.

23:08.400 --> 23:12.660
And thanks to this, now we can go to the start method.

23:12.660 --> 23:21.480
And here we can check if SR flip dot x is equals to true and not facing right.

23:21.480 --> 23:23.010
Then we need to flip the enemy.

23:23.010 --> 23:25.290
And we're going to do flip the enemy.

23:25.290 --> 23:28.650
And we're going to do SR flip dot x equals to false.

23:28.650 --> 23:33.390
So we're going to switch off the flip uh of the sprite on the x.

23:33.390 --> 23:35.250
Now let's save this.

23:35.460 --> 23:37.020
Let's go back to unity.

23:38.690 --> 23:40.940
Now I can take the enemy plant.

23:42.880 --> 23:44.620
Click right button over here.

23:44.620 --> 23:48.100
Find change facing direction I'm going to flip the sprite.

23:48.100 --> 23:49.060
So that's good.

23:49.060 --> 23:51.220
Now I can see enemies facing to the left.

23:51.220 --> 23:53.260
And when I'm going to the play mode.

23:54.730 --> 23:59.050
We're going to disable this flip X and flip the enemy entirely.

23:59.050 --> 24:04.360
So now he's facing to the right, and I can jump in front of him and he's going to shoot bullets into

24:04.360 --> 24:05.740
an opposite direction.

24:05.740 --> 24:08.110
And this will work for all of the enemies.

24:08.110 --> 24:10.030
Just before it was not so important.

24:10.030 --> 24:14.740
For example, for mushroom, it's not that necessary because anyway, you're going to walk left and

24:14.740 --> 24:15.850
right all the time.

24:15.850 --> 24:18.490
But for enemies like plant, it is very important.

24:18.490 --> 24:20.140
That's why I wanted to do it here.

24:20.230 --> 24:21.040
Okay.

24:22.560 --> 24:24.450
Let's change it back.

24:24.840 --> 24:26.280
Let's take the enemy plant.

24:26.280 --> 24:27.900
Change it back as well.

24:28.500 --> 24:30.300
Then I'm going to go to prefabs.

24:31.270 --> 24:33.730
Make the prefab out of the enemy plant.

24:33.760 --> 24:36.550
Also want to double check collision over here is trigger.

24:36.580 --> 24:37.870
Okay, good.

24:37.870 --> 24:39.100
Now I can delete this.

24:39.100 --> 24:40.990
And let's go to the next video.
