WEBVTT

00:00.040 --> 00:04.640
We will create a anime script in order to attack on this player game object.

00:04.640 --> 00:05.680
So let's do that.

00:05.680 --> 00:09.200
So we're gonna move over to project tab assets and then script folder.

00:09.200 --> 00:14.800
Let's right click there create and then Monobehaviour script and name it to something like anime.

00:14.840 --> 00:18.160
I already created this anime script as you can see.

00:18.160 --> 00:19.360
So now we need to attach.

00:19.360 --> 00:22.680
So let's select our anime one game object in the hierarchy.

00:22.680 --> 00:27.600
And we're going to drag this anime anime script in this add component section.

00:27.600 --> 00:29.280
And now we can see we attach.

00:29.320 --> 00:32.200
And let's open up this script by double clicking on it.

00:32.520 --> 00:35.720
So what we need we need our player transform.

00:35.760 --> 00:39.120
So basically you can simply call player position.

00:39.120 --> 00:41.680
So we can just keep track of where player is.

00:41.720 --> 00:47.680
And if the player gets an attack range of this enemy game object, then this enemy need to play the

00:47.680 --> 00:55.280
attack animation as well as it need to fire on this anime game object, or it's just need to shoot on

00:55.280 --> 00:57.000
this enemy game object.

00:57.000 --> 00:58.120
So we're going to do that thing.

00:58.120 --> 01:03.560
So let's open up our anime script and we what we need our player transform.

01:03.600 --> 01:09.840
So let's go to the top of our class and we're going to declare, or we're gonna grab the reference of

01:09.840 --> 01:11.320
our player position.

01:11.320 --> 01:16.480
So let's create a public that will be transform, which is responsible for position.

01:16.480 --> 01:20.400
And we can simply call it something like player or player position.

01:20.600 --> 01:26.000
And also we're going to create a second parameter or we're going to call it something like attack range.

01:26.160 --> 01:33.840
And it's just going to keep track of the basically we can define an range and you can simply call a

01:33.840 --> 01:34.480
attack range.

01:34.480 --> 01:40.680
And if this player game object gets inside of that attack range, then this enemy game object need to

01:40.720 --> 01:43.000
fire on this player game object.

01:43.000 --> 01:44.440
So let's go to script.

01:44.440 --> 01:48.600
So we're going to define or declare another variable which will be float.

01:48.600 --> 01:52.000
And we can call this one to something like attack range.

01:52.240 --> 01:54.160
So we're going to call it attack range.

01:54.160 --> 01:59.040
And by default I'm going to give a default value something like five f or five unit.

01:59.040 --> 02:01.520
And let's close that up with some my colon.

02:01.800 --> 02:05.000
And let's press control S in order to save our skin.

02:05.080 --> 02:07.080
And now let's move over to unity.

02:07.560 --> 02:11.200
So we're going to able to see that underneath our anime script.

02:11.200 --> 02:12.840
We need to drag our player.

02:12.880 --> 02:16.120
Transform means player position into that slot.

02:16.600 --> 02:20.280
And we're also going to able to see the attack range.

02:20.480 --> 02:26.320
So as soon as the anime script gets compiled, we can see underneath our anime script and it says Player

02:26.960 --> 02:27.440
Transform.

02:27.440 --> 02:31.600
So we need to select our player game object in the hierarchy and drag it right there.

02:31.640 --> 02:36.080
Now we can see we have player transform means position and attack range.

02:36.080 --> 02:40.600
I'm going to set in Inspector something like let's set it to seven unit.

02:40.600 --> 02:44.000
But you can increase the increase this value as well.

02:44.000 --> 02:46.400
It's basically totally up to you.

02:46.400 --> 02:52.560
So now let's open up our anime script and let's move over to this update function and what we want to

02:52.560 --> 02:53.040
do.

02:53.080 --> 02:54.960
We're going to use this physics 2D.

02:55.240 --> 03:04.440
So physics 2D dot overlap circle overlap circle overlap circle and make first bracket and then close

03:04.440 --> 03:05.800
that output semicolon.

03:05.800 --> 03:07.440
Basically what we want to do.

03:07.440 --> 03:13.360
We're going to create a circle around this anime game object with this much of attack range radius.

03:13.360 --> 03:15.360
As you can see, we set it seven.

03:15.360 --> 03:20.400
So with seven unit we're going to create a circle around this anime game object.

03:20.400 --> 03:26.560
And if this player game object collides with that circle or just gets in attack range of that radius

03:27.280 --> 03:33.000
than this anime game object need to attack, or it just should play the attack animation or the fire

03:33.040 --> 03:35.880
animation of this anime game object.

03:35.880 --> 03:37.480
So let's go to script.

03:37.480 --> 03:40.320
So this physics studio overlap circle.

03:40.520 --> 03:43.720
We're going to use this function in order to create the circle.

03:43.720 --> 03:49.960
So we need to use this physics overlap circle function in order to create the circle from this anime

03:49.960 --> 03:52.000
game object center position.

03:52.000 --> 03:57.440
So as a first argument it's going to take the position from where we want, where you just want to create

03:57.440 --> 03:58.160
the circle.

03:58.160 --> 04:01.000
So we're going to create our transform dot position.

04:01.000 --> 04:06.920
And transform dot position basically means the current position of the center position of our enemy

04:06.960 --> 04:07.760
game object.

04:07.800 --> 04:13.320
Comma as a second argument, it's going to take the radius with how much radius you just want to create

04:13.320 --> 04:14.000
the circle.

04:14.000 --> 04:17.440
So we're going to create the circle around this attack range radius.

04:17.440 --> 04:21.600
So we can simply call this one as well attack radius something like that.

04:21.600 --> 04:23.600
But I'm going to leave it to attack range.

04:23.600 --> 04:26.480
So I'm going to pass as a second argument attack range.

04:26.480 --> 04:31.400
And the third argument it's going to take the layer mask or basically which game object you want to

04:31.400 --> 04:31.840
target.

04:31.840 --> 04:33.840
So we're going to target this player game object.

04:33.840 --> 04:37.560
So we need to assign this player game object as a player layer.

04:37.800 --> 04:39.680
So how you can just do that.

04:39.680 --> 04:41.880
But before we do let's go to script.

04:41.880 --> 04:43.880
And we need to go to top of our class.

04:43.880 --> 04:47.600
And we need to assign or declare that variable as well.

04:47.600 --> 04:51.240
So I'm going to make this one public and that will be layer mask.

04:51.400 --> 04:55.000
And we can call this one to something like target layer or attack layer.

04:55.000 --> 04:57.640
So I'm going to call it instead target layer.

04:57.880 --> 05:04.120
And let's close that up with semicolon as a third parameter I'm going to pass the target layer and let's

05:04.400 --> 05:07.400
press down Ctrl S in order to save our script.

05:07.400 --> 05:13.320
And let's move to unity because we need to assign Assign those variable first one by one, and then

05:13.320 --> 05:15.680
we can just create the circle right away.

05:15.680 --> 05:20.120
But before we do we're going to assign this player game object as a player layer.

05:20.120 --> 05:21.440
So how you can do that.

05:21.440 --> 05:24.200
You need to select the game object first in the hierarchy.

05:24.200 --> 05:25.640
And then you have to do it.

05:25.640 --> 05:27.800
So we're going to select our player game object.

05:27.800 --> 05:30.880
And if we go to Inspector top right we can see layer.

05:30.880 --> 05:32.200
And it says default.

05:32.200 --> 05:37.880
So we need to assign this player as a player layer in order to attack this player game object.

05:37.880 --> 05:38.680
So that's why.

05:38.720 --> 05:42.720
So let's go to click on this layer and then click on this add layer.

05:43.040 --> 05:47.320
And we're going to create a another layer for this player game object.

05:47.320 --> 05:50.000
So I'm going to call this one something like player.

05:50.320 --> 05:51.280
And hit enter.

05:51.280 --> 05:56.440
And make sure you select the game object one more time the player and then go to Inspector and Layer.

05:56.440 --> 05:58.280
As you can see it says default.

05:58.280 --> 05:59.800
So we're going to assign with player.

05:59.920 --> 06:02.240
And we're going to do with this game object only.

06:02.240 --> 06:03.920
So that's why we're going to click on this.

06:03.960 --> 06:05.480
Yes or no.

06:05.520 --> 06:05.960
Sorry.

06:06.000 --> 06:07.920
We're not going to change the children.

06:08.040 --> 06:13.710
Instead we're going to do no this game object only or we basically just doing with this player game

06:13.750 --> 06:14.590
object only.

06:14.590 --> 06:18.870
And if I select the child game object and then move to Inspector, we can see this.

06:18.870 --> 06:23.670
It says Layer player, but I'm going to remove that instead I'm going to set it to default.

06:23.670 --> 06:27.070
And now what we will do, we will select our enemy game object.

06:27.070 --> 06:32.590
And we can see underneath our enemy script a target layer pops up and it says nothing.

06:32.590 --> 06:35.630
And we do know we assign it this player as a player layer.

06:35.630 --> 06:37.270
So I'm going to choose that player.

06:37.270 --> 06:38.230
So let's click there.

06:38.230 --> 06:43.470
And I'm going to choose this player so that the circle only going to check for this player game object.

06:43.510 --> 06:45.630
Basically we want to damage our player.

06:45.630 --> 06:51.710
So that's why we just only have chosen that player layer as you can see as a target layer.

06:51.710 --> 06:58.430
So now let's move to a script and we can see we use this function physics 2D dot overlap circle.

06:58.430 --> 07:01.390
In order to create the circle around center position.

07:01.390 --> 07:05.350
With this much of radius and we have only chosen target layer.

07:05.350 --> 07:08.310
And we do know as a target layer we have chosen player.

07:08.310 --> 07:11.670
So the circle only going to check for that player game object.

07:11.670 --> 07:17.110
So this can be true or false depending upon if the circle is really colliding with this player game

07:17.150 --> 07:17.990
object or not.

07:18.030 --> 07:21.070
If it's a, then this is gonna give us a true value.

07:21.110 --> 07:23.550
Else it's gonna give us a false value.

07:23.550 --> 07:27.310
So what we will do, we will store the result into a collider 2D.

07:27.350 --> 07:27.790
Why?

07:27.830 --> 07:29.950
Because this is work like as a collision.

07:29.950 --> 07:33.990
So that's why we're gonna store the result into a collider 2D variable.

07:33.990 --> 07:34.990
So let's store that.

07:34.990 --> 07:39.950
So we're going to say we're going to set Collider 2D equal to call info.

07:39.950 --> 07:44.390
Or you can simply say call information or collision information.

07:44.390 --> 07:46.590
And we're gonna set equal to this.

07:46.910 --> 07:53.510
So this call info can be true or false depending upon what this function gives us or return us.

07:53.550 --> 07:57.470
If the circle is really colliding, then it's going to give us a true value.

07:57.470 --> 08:02.710
So that's why this call info is just gonna be true right away, because we directly setting it, as

08:02.710 --> 08:03.390
you can see.

08:04.150 --> 08:07.190
And now what we're going to do, we're going to check in the next line.

08:07.190 --> 08:14.350
We will check if so make first bracket and some curly brackets and what we want to check over here.

08:14.350 --> 08:17.030
We want to check if call info is true.

08:17.030 --> 08:22.990
So we're going to say if call info is equals, equals to true basically means the circle is really collided

08:22.990 --> 08:24.550
with this player game object.

08:24.950 --> 08:31.070
Uh so call info is going to be true means the circle is really colliding with this player game object.

08:31.110 --> 08:35.190
Then what we need to do this player game object need to attack on this.

08:35.230 --> 08:38.270
Or it should fire on this player game object.

08:38.470 --> 08:42.750
So for now we're going to print out a message if it's perfectly working or not.

08:42.750 --> 08:43.550
So we're going to check.

08:43.550 --> 08:47.750
So we're going to type debug dot log in order to print out a message.

08:47.750 --> 08:50.670
Then make first bracket and close that up with semicolon.

08:50.670 --> 08:53.150
And we're going to print out a message something like.

08:53.150 --> 09:01.110
So I'm going to print out attack attack on player and make sure you just close that off with semicolon.

09:01.110 --> 09:03.910
And we're going to press down Ctrl S to save our script.

09:04.030 --> 09:05.910
And let's move over to unity.

09:06.430 --> 09:11.670
But we're not going to able to see the circle that we created around this enemy game object.

09:11.670 --> 09:18.390
So what we need to do, we need to use another function in order to draw the circle around this A game

09:18.430 --> 09:18.950
object.

09:19.190 --> 09:24.990
So as soon as the anime script gets compiled, we're not able to see our circle.

09:24.990 --> 09:29.870
So it would be nice if we able to see our circle with this much of radiance.

09:29.870 --> 09:30.990
So let's go to script.

09:30.990 --> 09:36.190
And for that we're going to use another function and it's called on draw gizmo selected function.

09:36.190 --> 09:36.910
We don't know.

09:37.070 --> 09:40.310
So first thing we uh, we want to draw.

09:40.430 --> 09:46.110
And in order to draw we need to use this gizmos dot draw function draw wire sphere function.

09:46.110 --> 09:49.310
So make sure you are doing as I did over here.

09:49.350 --> 09:50.910
Else it's not going to work.

09:50.910 --> 09:55.510
So make sure you just use this function on draw gizmo selected.

09:55.510 --> 09:58.630
Uh and this function is unity callback function.

09:58.630 --> 10:01.230
So you need to make as I did over here.

10:01.270 --> 10:05.110
Else you're not going to able to draw the circle as a first argument.

10:05.110 --> 10:07.750
This gizmo straw where sphere.

10:07.750 --> 10:11.590
It's going to take the position from where you want to draw the circle.

10:11.590 --> 10:16.870
So I'm going to pass transform dot position means the center position of our anime game object.

10:16.910 --> 10:19.270
And the second thing it's going to take the radius.

10:19.270 --> 10:24.830
So we're going to pass this attack range because with this attack range, we're just creating our radius

10:24.830 --> 10:28.470
or that circle with this much radius as you can see.

10:28.510 --> 10:31.630
So as a second argument I'm going to pass my attack range.

10:31.630 --> 10:33.710
And let's close that up with semicolon.

10:33.710 --> 10:39.470
And if you want to choose a different color then before you draw the circle you can choose the color

10:39.470 --> 10:42.950
as well by using this gizmos dot color.

10:42.950 --> 10:45.670
And you can set the color you would like to go with.

10:45.670 --> 10:49.550
So I'm going to set equal to something like color dot red.

10:49.790 --> 10:52.070
And you can choose any color that you like.

10:52.350 --> 10:55.230
And let's save our script and head over to unity.

10:55.750 --> 11:01.950
So uh, we will see as soon as our enemy script is gonna get compiled, we will be able to see that

11:01.950 --> 11:08.110
circle will be drawn from this enemy GameObject center position with this much of a radius, which is

11:08.150 --> 11:09.990
attack range, as you can see.

11:10.110 --> 11:17.830
And as soon as gets our, uh, our enemy script gets compiled, we can see the circle has been drawn

11:17.830 --> 11:22.270
from this enemy center position, and we can play around with this attack range.

11:22.270 --> 11:25.470
Basically attack radius you can call as well.

11:25.470 --> 11:27.750
So I'm going to set something like this much.

11:28.110 --> 11:30.750
And I guess it would be nice.

11:30.750 --> 11:34.550
So let's select the console top console tab in order to see the message.

11:34.550 --> 11:38.310
And let's click on this play button in the top to play our game.

11:39.630 --> 11:45.230
So as soon as this player game object is going to collide with this enemy circle or this circle, that

11:45.230 --> 11:48.430
message just going to pop up right away says attack on player.

11:48.470 --> 11:54.230
As you can see, the message just getting pop up every single time because we're doing inside that update

11:54.230 --> 11:54.990
function.

11:54.990 --> 12:01.270
So let's get out of play mode and what it's need to do this enemy game object, it's need to attack

12:01.270 --> 12:06.750
on this player game object, or it's basically need to shoot on this player game object.

12:06.750 --> 12:11.710
I hope you get the idea that how you can do it or how you can just do that.

12:11.710 --> 12:14.190
Let's go to overheads and then apply all.

12:14.190 --> 12:21.070
So the changes we made over here, it's going to be saved into this enemy one GameObject prefab as well.

12:21.070 --> 12:24.470
And I'm going to press down Ctrl S in order to save my project.
