WEBVTT

00:00.280 --> 00:01.480
What we're going to do now.

00:01.520 --> 00:07.840
Instead of printing out message, we basically need to get a little bit of a distance, basically.

00:07.880 --> 00:14.120
We will get closer to our player game object, and once the enemy somewhere is over here, or the player

00:14.160 --> 00:21.160
just get closer to, uh, to this enemy game object somewhere over here, then this enemy need to stop

00:21.440 --> 00:26.160
walking towards our player and it's need to play the attack animation clip.

00:26.200 --> 00:29.240
But before we do, we also have a problem.

00:29.240 --> 00:34.600
That enemy is not gonna look towards the direction our player just gonna go.

00:34.760 --> 00:35.760
Let me show you.

00:35.760 --> 00:39.560
So let's click on this play button in the top in order to play our game.

00:39.560 --> 00:41.560
And let's have a look on this.

00:41.640 --> 00:48.480
So as soon as we're gonna get a closer to this enemy, basically enemy just not looking in the direction,

00:48.560 --> 00:55.480
uh, our player is going or player, uh, or this enemy is not facing this enemy too.

00:55.680 --> 00:59.800
So if I get closer, firstly, we need to make the speed a little bit up.

00:59.800 --> 01:02.890
So I'm going to select my anime to GameObject in the hierarchy.

01:03.170 --> 01:06.730
Let's go over to Inspector Anime Script, anime two script.

01:06.730 --> 01:09.930
And I'm going to make this bit something like uh one.

01:09.930 --> 01:17.010
So as you can see, as soon as we get inside of this attack range radius of this anime two game object,

01:17.050 --> 01:19.290
this anime just stops moving.

01:19.490 --> 01:26.410
And if we jump over or top of it, as you can see, it's not looking in the direction player is going.

01:26.410 --> 01:30.330
So we need to flip this anime to the right direction as well.

01:30.330 --> 01:31.930
So we will get out of play mode.

01:31.930 --> 01:34.690
To do that, we're going to approach the same way.

01:34.690 --> 01:41.810
So what we can simply do, we can simply copy our, uh, flip function from our enemy one script, and

01:41.810 --> 01:44.890
we can just paste it inside this enemy two script.

01:44.890 --> 01:50.290
So let's do that instead of making a from scratch or doing it from scratch.

01:50.810 --> 01:58.530
So we will open up our anime one script so you can select your anime one game object in your basically

01:58.530 --> 01:59.890
prefabs folder.

02:00.090 --> 02:06.100
So in my prefabs folder I'm going to select my anime -- game object and let's open up the anime one

02:06.100 --> 02:12.820
script, go over to Inspector and then double click on this anime one script by basically open that

02:12.820 --> 02:13.700
up quickly.

02:13.980 --> 02:17.060
And over here we're going to copy the flip function.

02:17.060 --> 02:18.300
So let's find it.

02:18.300 --> 02:21.260
So we have the flip function as you can see.

02:21.260 --> 02:28.940
So what we simply do we're gonna copy this by Ctrl C and we will go over to our anime two script.

02:28.980 --> 02:34.740
And so let's open that up and we're gonna paste it inside this anime two script.

02:34.900 --> 02:38.900
And let's do it somewhere over here by Ctrl V.

02:38.940 --> 02:40.220
We're gonna paste it.

02:40.260 --> 02:46.700
Now, as you can see we created and but the problem we have that we don't have the player position.

02:46.700 --> 02:49.260
So we need to grab the player position reference.

02:49.260 --> 02:52.220
But before I do I'm gonna arrange like this.

02:52.820 --> 02:56.340
So we need our player position player position.

02:56.340 --> 03:02.860
So we need a reference of transform of this player game object, so let's grab it.

03:02.860 --> 03:08.060
To do so, I will move over to the top of my class as somewhere over here.

03:08.380 --> 03:10.500
Let's make a serialized file.

03:10.500 --> 03:13.460
I'm going to make it over here serialized field.

03:14.460 --> 03:15.500
Serialized field.

03:15.700 --> 03:17.380
And I'm going to click here.

03:17.820 --> 03:20.900
This so serialized field private.

03:20.900 --> 03:22.860
And we're looking for player position.

03:22.860 --> 03:26.900
So we're going to type transform which is responsible for position.

03:26.900 --> 03:30.300
And we can call this one player or player position.

03:30.300 --> 03:34.140
Since over here we can see that this is player.

03:34.140 --> 03:35.900
So that's why we're going to name it.

03:36.180 --> 03:40.900
We're going to name as a player we we will leave it as player name.

03:40.900 --> 03:44.380
As you can see now the error is gone by now.

03:44.380 --> 03:48.460
So we also need to call this flip function in our update function.

03:48.460 --> 03:54.620
So when we're going to call it once our player is in attack range is player in attack range.

03:54.620 --> 04:01.830
So in that moment basically if the player inside of that circle basically inside of this animator circle,

04:01.830 --> 04:03.790
then we will call the flip function.

04:03.790 --> 04:04.630
So that's why.

04:04.670 --> 04:06.510
So we're going to do it over here.

04:06.510 --> 04:08.470
So let's call the flip function.

04:08.470 --> 04:09.110
And let's make.

04:09.150 --> 04:11.590
First bracket and close that up with semicolon.

04:11.870 --> 04:17.270
Press down Ctrl S on your keyboard in order to save your script and then move over to unity.

04:17.510 --> 04:25.510
So what we need to do now underneath our enemy to script attack checking title, we will, uh, get

04:25.550 --> 04:31.190
uh, you can say we will get a transform field and by default it will be none.

04:31.190 --> 04:34.510
And we just, uh, we just named that as player.

04:34.510 --> 04:38.830
So we will select our player GameObject from hierarchy from the hierarchy.

04:38.830 --> 04:40.870
And we're going to drag over to that field.

04:40.870 --> 04:43.030
So we're going to have the player transform.

04:43.070 --> 04:45.510
Or you can simply say the player position.

04:45.510 --> 04:53.150
And then uh we we basically doing that basically uh flipping the enemy in the direction player is going.

04:53.630 --> 04:54.830
So just wait a moment.

04:54.830 --> 05:01.000
Once our compilation is done, we can go ahead and drag our player Clear transform.

05:01.000 --> 05:02.360
So just wait a moment.

05:02.520 --> 05:06.160
And now enemy two script is compiled by now.

05:06.480 --> 05:13.080
So underneath our enemy two script, we can see in the bottom, uh, player and it says None transform.

05:13.080 --> 05:18.040
So we will select our player game object in the hierarchy and let's drag it over to that field.

05:18.040 --> 05:22.560
And now we can see we have the player transform basically player position.

05:22.560 --> 05:28.400
And if we go ahead and then click on this play button and I'm gonna bump up the work speed a little

05:28.440 --> 05:31.640
bit, I'm going to make it a, a b a a a.

05:33.040 --> 05:40.240
You can say a, a max value, something like I'm gonna bump up with, uh, let's do it.

05:40.440 --> 05:43.720
Let's go with, uh, two unit of speed.

05:43.760 --> 05:46.080
Now we're going a little bit faster.

05:46.080 --> 05:54.160
And if we move over to the right side, we not able to see, uh, that thing, basically, we have done

05:54.160 --> 05:55.120
some mistake.

05:56.440 --> 06:03.690
So if we move over to console tab, we if we get out of play mode, we can see we have a error message.

06:03.890 --> 06:10.930
So if I scroll down to the top, I don't know where we're getting that error message.

06:11.810 --> 06:12.330
Sorry.

06:12.370 --> 06:13.210
We will go to.

06:13.490 --> 06:15.930
So we have transform okay.

06:16.930 --> 06:18.490
So what we need to do we need to.

06:18.690 --> 06:21.730
Oh just go over to overrides and then apply all.

06:22.250 --> 06:24.330
And now this is just going to be fine.

06:24.370 --> 06:29.730
So if we go ahead and then click on this play button in the top, it's going to be fine by now.

06:29.730 --> 06:31.490
So let's take a look on this.

06:31.530 --> 06:35.850
Make sure you select your player object from your hierarchy tab.

06:35.850 --> 06:40.010
And then drag it over to this M2 script in your player slot.

06:40.010 --> 06:41.690
So you're going to have the reference.

06:41.970 --> 06:44.650
So let's do it.

06:45.170 --> 06:49.850
So we still are not able to flip this anime that direction.

06:49.850 --> 06:53.890
So let's get out of play mode and we will move over to our script.

06:54.010 --> 06:55.970
And let's check it over here.

06:56.250 --> 07:00.850
As you can see we created a facing left right over here.

07:01.250 --> 07:04.450
So what we're going to do we're going to call this flip function.

07:04.690 --> 07:09.890
Instead of calling right over here we're going to call it in outside of it.

07:09.970 --> 07:14.250
So let's call that function here top of our update function.

07:14.250 --> 07:18.170
And now we're going to press down Ctrl S in order to save our script.

07:18.770 --> 07:24.930
You what you can simply do you can just cut this line of code and you can paste it in update function

07:24.930 --> 07:25.610
as well.

07:25.890 --> 07:27.330
It's basically up to you.

07:27.370 --> 07:31.890
So I'm going to press Ctrl s on my keyboard in order to save my project.

07:32.090 --> 07:32.530
Sorry.

07:32.570 --> 07:33.490
Save my script.

07:33.490 --> 07:37.170
And let's head over to Unity editor one more time.

07:37.170 --> 07:39.410
And now let's take a look on it.

07:39.450 --> 07:40.530
It should be work.

07:40.570 --> 07:41.130
Fine.

07:41.410 --> 07:44.730
If it's not, then we need to do some settings.

07:45.010 --> 07:46.530
So just wait a moment.

07:47.850 --> 07:49.810
So wait a moment.

07:49.810 --> 07:50.610
And now.

07:51.050 --> 07:52.770
So let's have a look.

07:52.810 --> 07:55.210
I guess this is fine.

07:55.250 --> 07:57.130
Okay so let's go to unity.

07:57.370 --> 08:01.740
And I'm going to make the work speed of this anime game object to something like one.

08:01.940 --> 08:04.460
Let's go to overrides and then apply all.

08:04.660 --> 08:08.100
Let's click on this play button in the top to play our game.

08:08.460 --> 08:11.060
And let's and let's have a look on this.

08:15.020 --> 08:18.100
It should work fine or other other way.

08:18.100 --> 08:19.820
We need to do something.

08:20.220 --> 08:21.820
So let's get closer.

08:21.820 --> 08:28.820
We can see now as you can notice, this anime two game object just flipping in the direction player

08:28.820 --> 08:29.660
is going.

08:29.700 --> 08:31.340
So as you can notice.

08:31.340 --> 08:33.060
So I'm going to get out of play mode.

08:33.060 --> 08:40.020
So basically what you have to do you need to move your this uh flip function, flip flip function uh

08:40.220 --> 08:42.420
in the top of your update function.

08:42.460 --> 08:47.020
Don't put it inside this like is player in attack range if it's false.

08:47.060 --> 08:49.180
Basically we just call it over here.

08:49.180 --> 08:56.260
And we can notice that if this player attack in attack range is false, basically means a enemy is over

08:56.260 --> 08:56.860
here.

08:56.860 --> 09:01.150
So in that moment we just flipping but we don't want to do that.

09:01.150 --> 09:02.150
So that's why.

09:02.350 --> 09:07.630
So what we can simply do we can put this flip function as well inside this else condition.

09:07.830 --> 09:08.950
In the else condition.

09:08.950 --> 09:11.990
What we're doing we're saying player in attack range.

09:11.990 --> 09:17.030
So we can put this flip function right over here instead of doing here.

09:17.430 --> 09:21.950
But I'm gonna leave it in top of my update function and it's gonna work fine.

09:22.270 --> 09:26.750
So basically we have done the same thing as we have done with enemy one script.

09:26.790 --> 09:33.270
I hope you get the idea that how you can do it, and we can move this flip function as well inside this

09:33.310 --> 09:34.310
else condition.

09:34.350 --> 09:36.630
Else else if our enemy.

09:36.990 --> 09:43.230
Sorry if our player game object inside of this enemy to attack range, then what we're doing, we simply

09:43.230 --> 09:46.950
printing out this message saying player in attack range.

09:46.950 --> 09:50.790
So we can just put the flip function here over here.

09:51.030 --> 09:56.390
So the and the flip function just gonna flip the enemy in the direction player is going.

09:56.790 --> 09:59.390
So we can put this line of code which is flip.

09:59.390 --> 09:59.910
So let's.

10:01.120 --> 10:03.640
basically press down Ctrl X for cut.

10:03.640 --> 10:07.280
And we're going to paste it inside this else condition by Ctrl V.

10:07.600 --> 10:14.600
And now if we press Ctrl S on our keyboard to save our script and then move over to unity, it's gonna

10:14.640 --> 10:15.480
work fine.

10:15.560 --> 10:21.120
Basically our enemy to game object just gonna flip in the direction player is going.

10:21.160 --> 10:26.680
So let's have a look on this and let's have a look on this first.

10:26.920 --> 10:32.320
So what I'm going to do now, uh, I'm going to click on this play button in the top in order to play

10:32.320 --> 10:33.160
our game.

10:33.160 --> 10:35.120
And let's take a look on this.

10:35.480 --> 10:43.320
So if our if we are over here then you will see this enemy just gonna flip in the right direction.

10:43.320 --> 10:44.320
So let's have a look.

10:44.800 --> 10:48.440
So we will get closer to this enemy game object first.

10:48.440 --> 10:54.840
And if I jump over top of it we can see enemy just flipping in the direction the player is going.

10:55.240 --> 10:56.720
Uh, player is going.

10:56.720 --> 11:00.520
So what we need to do, we need to do some, uh, changes.

11:00.840 --> 11:05.880
Uh, but the problem is that Collider is, uh, not, uh, perfectly fit.

11:06.160 --> 11:14.360
Uh, we can see that our anime to fit, uh, just overlaps with this ground, uh, overlap with this

11:14.400 --> 11:15.480
white ground.

11:15.480 --> 11:20.320
So we need to adjust our box collider 2D inside this anime to game object.

11:20.320 --> 11:25.200
So let's get out of play mode, and let's select our animator GameObject in the hierarchy.

11:25.240 --> 11:28.280
Then go over to Inspector Box Collider 2D component.

11:28.320 --> 11:30.320
Then click on this Edit Collider.

11:30.320 --> 11:36.400
And we're gonna edit the Collider as the size of our anime sprite, and make sure you just make it as

11:36.400 --> 11:41.480
the size of your sprite, so it's not gonna overlap with other sprites.

11:41.480 --> 11:43.080
So basically that is the thing.

11:43.240 --> 11:49.200
And if you just change the thing, make sure you just go over to Inspector Overrides and then apply

11:49.200 --> 11:51.080
all and that's gonna be fine.

11:51.200 --> 11:56.080
And we will press down Ctrl s on our keyboard in order to save our project.

11:56.080 --> 11:59.200
And we can just move over to console tab in the top left.

11:59.200 --> 12:05.450
We can see clear, and if we want to clear all of the messages, then we can simply click on this clear

12:05.450 --> 12:08.050
in order to clear out all of the messages.

12:08.250 --> 12:14.050
And if we go ahead and then click on this play button, you will see the fit of this player game object

12:14.090 --> 12:18.650
not gonna overlap with this white ground, so it's gonna be fine by now.

12:18.770 --> 12:19.730
So let's have a look.

12:19.730 --> 12:25.130
So make sure you just adjust your box Collider 2D as the size of your enemy sprite.

12:25.170 --> 12:28.330
Now we can see it's not overlapping at all.

12:28.650 --> 12:35.970
And once our player in attack range in attack range radius, uh, basically the circle collided with

12:35.970 --> 12:37.170
this player game object.

12:37.210 --> 12:42.570
We can notice this enemy game object just stops moving.

12:42.970 --> 12:46.770
And we need to play some other animation clip as well.

12:46.770 --> 12:48.330
So I'm going to get out of play mode.

12:48.330 --> 12:51.130
So I hope you get the idea that how you can do it.

12:51.130 --> 12:57.410
And I'm gonna press down Ctrl s or one more time in order to save my project, uh, on my keyboard.

12:57.410 --> 13:00.730
So whatever I have done so far, it's gonna be saved.
