WEBVTT

00:00.040 --> 00:03.400
We will shoot out a ray from this anime game object.

00:03.400 --> 00:07.480
So we're going to do the same thing as we have done with this player game object.

00:07.480 --> 00:10.120
So let's open up our anime script quickly.

00:10.640 --> 00:13.720
Let's double click on that in order to open that up.

00:13.840 --> 00:15.520
So we're going to delete some line of code.

00:15.560 --> 00:21.800
Basically setting that line renderer component as using that we have done with this player game object.

00:21.800 --> 00:23.080
So let's go to scape.

00:23.080 --> 00:29.360
But before we do make sure you just go to the top of your class and include this namespace call using

00:29.400 --> 00:31.160
system dot collection.

00:31.160 --> 00:37.640
So it's going to allow us in order to delay some line of code, or it's going to allow us to use coroutine

00:37.680 --> 00:38.640
function.

00:38.640 --> 00:41.600
So we're going to do it somewhere over here.

00:41.800 --> 00:45.800
And for we're going to use this AI enumerator function.

00:45.800 --> 00:47.360
So AI enumerator.

00:47.360 --> 00:50.280
And we have to name the function what we want to call it.

00:50.280 --> 00:56.440
So I'm going to call it something like shoot shoot raycast something like that.

00:56.760 --> 00:59.920
And let's make first bracket and some curly brackets.

00:59.920 --> 01:01.760
So why are we using this function.

01:01.760 --> 01:03.600
Because we're going to delete some line of code.

01:03.640 --> 01:10.080
Basically, we want a line renderer component to shoot out from this anime game object to the left direction.

01:10.600 --> 01:16.120
So we need to enable as well as after a short period of time we need to disable that.

01:16.280 --> 01:20.080
So we just want to delete something after enabling it.

01:20.080 --> 01:26.920
So that's why we're going to use this function in order to delay or in order to do some specific things

01:26.920 --> 01:29.080
after a short period of time.

01:29.560 --> 01:33.280
So this is the reason we're using that.

01:33.280 --> 01:36.480
So first thing what we're going to do we're going to shoot out a ray.

01:36.720 --> 01:39.520
So we're going to use a function called raycast 2D.

01:39.520 --> 01:41.920
So we're going to use this physics 2D.

01:41.920 --> 01:47.440
So we need to use this physics 2D dot raycast function and this function as a first argument.

01:47.440 --> 01:51.320
It's going to take the position from where we want to shoot out the ray.

01:51.320 --> 01:56.160
So we're going to shoot out the ray from this enemy GameObject gun point to the left direction.

01:56.160 --> 01:58.600
And the second thing it's going to take the direction.

01:58.600 --> 02:01.480
And the third thing it's going to take the length of the ray.

02:01.480 --> 02:06.000
And the fourth thing it's going to take the layer mask, basically which game object we want to target.

02:06.000 --> 02:08.360
So we're going to target this player game object.

02:08.360 --> 02:10.600
So as a layer mask we're going to choose player.

02:10.600 --> 02:14.080
So let's go to the top of our class and let's grab those things.

02:14.080 --> 02:16.040
So first thing we need a position.

02:16.040 --> 02:18.880
So let's make a public transform.

02:18.880 --> 02:22.560
And we can call this one to something like shoot point or fire point.

02:22.560 --> 02:24.640
So I'm going to call it fire point.

02:25.080 --> 02:27.600
And the second thing we need the direction.

02:27.600 --> 02:29.080
But we're going to use vector two.

02:29.080 --> 02:31.520
Dot left basically means the left direction.

02:31.520 --> 02:34.680
So we don't need to create a variable for that.

02:35.360 --> 02:40.800
But we need a variable about our length of the ray which is just going to be the distance.

02:40.800 --> 02:43.480
So I'm going to make a another public float.

02:43.480 --> 02:46.240
And we can call this something like ray length.

02:46.240 --> 02:48.560
And I'm going to call it instead distance.

02:48.880 --> 02:50.520
And let's set equal to.

02:50.720 --> 02:52.320
So I'm going to give a default value.

02:52.320 --> 02:58.240
Or you can just simply close that up with semicolon if you just want to set and then underneath your

02:58.240 --> 02:58.680
script.

02:58.680 --> 03:01.640
But I'm going to give a default value something like ten F.

03:01.920 --> 03:07.370
And the third uh, fourth thing we need the layer mask or which game object really we want to target.

03:07.370 --> 03:13.810
So let's create another public layer mask and we can call this one to something like attack layer or

03:13.810 --> 03:14.410
target layer.

03:14.410 --> 03:16.850
So I'm going to call it target Layer instead.

03:17.010 --> 03:20.330
And now let's go to that function where we're shooting.

03:20.330 --> 03:22.290
Also we're doing it over here.

03:22.450 --> 03:27.370
So first thing it's just gonna need the position from where we want to shoot shoot out.

03:27.370 --> 03:31.330
So I'm going to pass our fire point dot position from the.

03:31.370 --> 03:35.890
So make sure you just type this dot position basically accessing the position.

03:35.890 --> 03:38.650
And the second thing we need are the direction.

03:38.650 --> 03:41.250
So we're going to shoot out the ray in the left direction.

03:41.250 --> 03:43.330
So we can simply use Vector2 dot.

03:43.770 --> 03:46.090
It basically means -one inch the x.

03:46.090 --> 03:49.090
So it's going to go to negative and y in the zero.

03:49.490 --> 03:51.850
So we're not going to shoot out the ray in the y.

03:51.890 --> 03:54.650
So that's why it's going to be zero in the y.

03:54.690 --> 03:57.170
So we're going to use that vector2 dot left.

03:57.170 --> 03:59.610
So we're going to use vector2 dot left.

03:59.650 --> 04:01.930
It's basically means -one inch the x.

04:01.930 --> 04:06.410
So the ray just going to shoot out to the left direction and why in this video?

04:06.410 --> 04:09.490
So the ray not gonna shoot out at all in the Y.

04:09.530 --> 04:11.930
So it's gonna shoot out in the left direction.

04:12.250 --> 04:15.570
And the third thing we need the distance or the length of the ray.

04:15.570 --> 04:18.290
So I'm going to pass my distance parameter.

04:18.290 --> 04:21.690
And by default I set a ten unit as you can see.

04:22.970 --> 04:24.410
So we're getting an error.

04:24.410 --> 04:28.810
Basically we already created a target layer as you can see.

04:29.250 --> 04:30.770
So I'm going to remove this.

04:31.130 --> 04:34.570
And instead of we just ignore this parameter.

04:34.570 --> 04:40.010
Or we can simply create a layer for this for this as well.

04:40.010 --> 04:45.370
So in the top over here we can see we already have a target layer variable name.

04:45.370 --> 04:50.810
So that's why we just getting a one getting error in order to create one more time.

04:50.810 --> 04:52.650
Because we already have that.

04:52.650 --> 04:55.930
So you need to some you need to name it something else.

04:55.930 --> 04:58.570
Or you can just simply use this one as well.

04:58.610 --> 05:02.290
Because as a target layer we have chosen that player game object.

05:02.290 --> 05:06.330
We do know and we really want to damage our player game object.

05:06.330 --> 05:08.930
So that's why we can use this as well.

05:08.930 --> 05:12.890
So we can just going to remove this or you can simply create one as well.

05:12.890 --> 05:14.170
It's totally up to you.

05:14.170 --> 05:18.530
But I'm going to use this one because as a target layer I have chosen player.

05:18.530 --> 05:21.450
And we really want to damage our player game object.

05:21.450 --> 05:25.570
So that is the reason I'm gonna use this one more time.

05:25.570 --> 05:27.970
So that's why I didn't create at all.

05:28.170 --> 05:33.890
The second parameter, if you just want then you can create one more layer mask and you can call it

05:33.890 --> 05:36.210
something like Attack Layer and Attack Layer.

05:36.210 --> 05:41.490
You can simply choose your player game object or which game object you just want to damage.

05:41.810 --> 05:43.210
So I'm gonna use that.

05:43.210 --> 05:46.810
So as a fourth argument I'm going to pass this target layer.

05:46.970 --> 05:52.130
And as a target layer I have already chosen the player which I really want to damage.

05:52.130 --> 05:54.730
And then we're going to close that up with semicolon.

05:54.730 --> 06:00.650
So this physics 2D dot raycast it's gonna shoot out the ray from this fire point dot position in the

06:00.650 --> 06:03.290
left direction with this much of length of the ray.

06:03.370 --> 06:06.810
And then it's only going to target our player game object.

06:06.810 --> 06:13.490
So this over here, this ray just gonna return us a true value or false value, depending upon if the

06:13.490 --> 06:16.530
ray is really collide with this player game object or not.

06:16.570 --> 06:23.010
If it's, then it's gonna give us a true value, else it will give us a false value.

06:23.010 --> 06:23.850
So what are we going to do?

06:23.850 --> 06:27.690
We're going to store the result into a raycast hit 2d parameter.

06:27.730 --> 06:28.170
Why?

06:28.210 --> 06:32.650
Because this physics 2D dot raycast it's going to work as a ray.

06:32.650 --> 06:37.530
So that's why we're going to store the result into another raycast hit 2d parameter.

06:37.810 --> 06:39.810
So let's create a raycast.

06:40.050 --> 06:43.850
Hit 2d and we have to call it what we want to name it.

06:43.850 --> 06:48.170
So I'm going to call it something like hit info or hit in hit information.

06:48.170 --> 06:50.450
And I'm going to set equal to this.

06:50.450 --> 06:52.090
So this hit info over here.

06:52.090 --> 06:57.730
It can be true or false depending upon if the ray is really colliding with this player game object or

06:57.730 --> 06:57.930
not.

06:57.970 --> 07:01.010
If it's then this function is just going to be true right away.

07:01.010 --> 07:07.370
So that's why this hit info is going to be true, because we're storing this result into this hit info

07:07.370 --> 07:09.700
container or into this parameter.

07:09.900 --> 07:14.340
So what we do want to do now we're going to check if hit info is true.

07:14.340 --> 07:17.660
Basically means the array really collide with this player game object.

07:17.660 --> 07:22.140
So in that situation what we need to do we need to damage our player game object.

07:22.140 --> 07:24.980
But before we do let's print out a message.

07:24.980 --> 07:26.940
So I'm going to move over to our script.

07:26.940 --> 07:30.700
And over here we're going to check if raycast.

07:30.740 --> 07:31.140
Sorry.

07:31.180 --> 07:35.860
If hit info is equals equals to true if that is the case.

07:35.860 --> 07:42.660
So let's make some curly brackets and if hit info is true, basically means the ray is colliding with

07:42.660 --> 07:43.940
that player game object.

07:43.940 --> 07:46.980
Or we can simply just pass this hit info.

07:47.260 --> 07:47.980
Hit info.

07:47.980 --> 07:54.180
Basically, it also means the same thing means if hit info is true, then what we need to do, we need

07:54.180 --> 07:55.820
to damage our player.

07:55.820 --> 07:58.260
But for now we're going to print out a message.

07:58.260 --> 08:02.660
So that's why we're going to type this debug debug dot log.

08:03.060 --> 08:08.900
And we're going to print out message something like uh the player game object takes damage.

08:08.900 --> 08:11.660
So we're going to use this parameter which is hit info.

08:12.060 --> 08:18.100
So by this hit input parameter we can extract info something like the name of the game object that we

08:18.140 --> 08:19.060
collided with.

08:19.100 --> 08:21.140
And I think that we can access.

08:21.140 --> 08:26.860
So what I'm going to do I'm going to use I'm gonna go over to this parameter which is hit info.

08:27.100 --> 08:29.500
And then we're going to access the game object.

08:29.500 --> 08:32.660
So sorry, we're going to access the collider.

08:32.980 --> 08:36.380
Or we can simply access the transform as well.

08:36.380 --> 08:39.860
And then we're going to access the game object and then name.

08:40.060 --> 08:42.780
And top of that I'm going to add a custom message.

08:42.780 --> 08:45.500
So we're going to use this plus icon and make quotation.

08:45.500 --> 08:48.460
And I'm going to say something like text damage.

08:49.580 --> 08:51.580
And let's close that up with semicolon.

08:51.580 --> 08:56.180
So what we basically doing we just taking this hit info parameter.

08:56.180 --> 09:03.020
So we're using this hit info parameter in order to extract information about the ray collided with.

09:03.020 --> 09:06.260
So let's suppose the ray collided with this player game object.

09:06.260 --> 09:12.260
So what we do what we're doing in in that moment we're just going to that game object transform component

09:12.260 --> 09:14.900
and then access the accessing the game object.

09:14.980 --> 09:17.660
So over here hit info dot transport.

09:17.700 --> 09:21.300
Game object basically means the game object that they collided with.

09:21.340 --> 09:23.980
So the ray is just going to collide with this player game object.

09:23.980 --> 09:27.580
So we basically going over to this player game object Transform.

09:27.580 --> 09:33.660
And by this transform we just accessing the whole game object of whole game object in our case player.

09:33.660 --> 09:38.620
And then we just accessing the name and we can see in the inspector the name is player.

09:38.620 --> 09:42.420
So this over here just going to be player the name of the game object.

09:42.420 --> 09:47.540
And top of that we just added a custom message says take damage as you can see.

09:47.540 --> 09:51.300
So this is going to be a message something like player takes damage.

09:51.300 --> 09:54.860
So this message just going to pop up in the console right away.

09:55.460 --> 10:00.260
So we can simply press down control S in order to save our script.

10:00.260 --> 10:05.420
But before we do that, as you can see we're using this short raycast function.

10:05.420 --> 10:10.300
So what we have to do, we need to use this return because we are getting an error.

10:10.460 --> 10:14.020
As you can see over here in our function.

10:14.260 --> 10:23.020
So what we need to do we need to use this yell return new Waitforseconds new wait four seconds and make

10:23.060 --> 10:25.540
first bracket and close that up with semicolon.

10:25.660 --> 10:28.060
So we're just gonna wait this amount of time.

10:28.060 --> 10:29.460
So you need to pass in.

10:29.460 --> 10:31.300
Wait four seconds over here.

10:31.300 --> 10:34.820
The time after that amount of time, what you want to do.

10:35.100 --> 10:43.620
So I'm gonna I'm gonna do something like after 0.1 or 1 to 4 second, we want to do some specific thing.

10:43.620 --> 10:46.460
So we haven't set up the line renderer yet.

10:46.460 --> 10:48.660
So we're going to print out a message first.

10:48.980 --> 10:53.020
Uh, something like uh debug dot log for printing out message.

10:53.420 --> 10:59.500
And we can simply say uh in quotation, uh, disable something like that.

10:59.660 --> 11:01.500
Disable Ray.

11:01.980 --> 11:05.460
Uh, or we can simply say LR means line renderer.

11:05.460 --> 11:07.740
And we're going to close that up with semicolon.

11:07.940 --> 11:12.220
So we also need to call this function, uh where we're going to call this function.

11:12.220 --> 11:15.220
We're going to call it inside this function.

11:15.220 --> 11:18.780
As you can see, we're just printing out a message in the console.

11:18.820 --> 11:22.100
Say instead of doing that we're going to call this function.

11:22.100 --> 11:24.500
This should should raycast function.

11:24.500 --> 11:26.100
And what this function is doing.

11:26.140 --> 11:28.140
Firstly it's just creating the ray.

11:28.180 --> 11:33.220
And then if the ray just collide with this player game object, then we just printing out the name of

11:33.220 --> 11:34.140
the game object.

11:34.140 --> 11:40.540
As you can see, uh name, sorry name, as well as uh, which is uh, adding a custom message.

11:40.540 --> 11:45.060
So this player takes damage messages gonna be pop up right away.

11:45.060 --> 11:47.100
And after this amount of time.

11:47.140 --> 11:47.500
Yield.

11:47.500 --> 11:47.900
Return.

11:47.900 --> 11:48.220
New.

11:48.380 --> 11:54.460
Uh, wait for a second, then what we're doing, we just setting that disable alert so we don't, uh,

11:54.540 --> 11:57.500
haven't set up the line renderer component yet.

11:57.500 --> 12:00.820
So that's why we just, uh, printing out a message for now.

12:00.820 --> 12:06.140
So we also need to call this function and this function, just gonna do this thing and make sure you

12:06.140 --> 12:08.220
just using this yield return new.

12:08.220 --> 12:11.620
Wait for a second and you can put the time in this brackets.

12:11.900 --> 12:17.430
Uh, the time after that amount of time, you can just do specific things that you want to do.

12:17.630 --> 12:22.310
So I'm just setting I'm just printing out a message says disable error.

12:22.350 --> 12:23.510
As you can see.

12:23.510 --> 12:28.750
So instead of printing out message over here in this shoot function we're going to call this shoot raycast

12:28.750 --> 12:29.630
function.

12:29.630 --> 12:37.190
But we do know in Ienumerator or in order to call a enumerator function we need to use this Startcoroutine.

12:37.430 --> 12:41.110
It's kind of a bit weird, but we have to use and then make first bracket.

12:41.110 --> 12:44.910
And this first bracket you need to put uh the function name.

12:44.910 --> 12:47.750
So in our case this should raycast hit.

12:47.750 --> 12:52.030
So we need to call the function inside this first brackets.

12:52.030 --> 12:56.150
So in our case shoot raycast and then make this first bracket.

12:56.150 --> 12:58.230
And then close that up with semicolon.

12:58.470 --> 13:02.110
So we do know that we're calling the shoot function through animation event.

13:02.110 --> 13:07.390
Basically whenever our enemy game object playing the animation clip which is the shooting one.

13:07.430 --> 13:12.390
Then we're calling the shoot function and we also calling this shoot raycast function.

13:12.390 --> 13:17.550
Once we call this shoot function and what should raycast function is doing?

13:17.590 --> 13:23.510
Firstly, it is creating the ray from this position in the left direction with this much of length.

13:23.510 --> 13:28.230
And then we only targeting our player game object and we checking in the next time.

13:28.230 --> 13:35.310
If hit info means is true means our ray just colliding with that player game object, then we just printing

13:35.310 --> 13:36.910
out a message in the console.

13:36.950 --> 13:39.510
Says player takes damage as you can see.

13:39.710 --> 13:45.670
And after this amount of time we just printing out message disable error as you can see.

13:45.790 --> 13:48.990
So we're going to press down Ctrl S in order to save our script.

13:48.990 --> 13:50.990
And now let's move over to unity.

13:54.030 --> 13:56.950
So we will able to see that message.

13:58.390 --> 14:03.910
So we basically have done the same thing as we have done in our player game object.

14:03.910 --> 14:10.230
What we have done in shooting lecture on this player game object, we basically approaching the same

14:10.270 --> 14:13.630
way or with our enemy game object.

14:13.630 --> 14:19.110
So I'm going to select my console tab and let's click on this play button in the top to play our game.

14:19.630 --> 14:25.950
And you will see once this anime game object is just gonna play the attack animation, it's gonna call

14:25.950 --> 14:26.830
the shoot function.

14:26.830 --> 14:30.790
And inside that function we just basically calling the shoot raycast.

14:30.790 --> 14:35.790
We just creating, but we are we gonna get a point basically error.

14:36.550 --> 14:38.470
As you can see we're getting an error.

14:38.470 --> 14:39.830
So we're going to get out of play mode.

14:39.870 --> 14:43.950
Basically we need to assign those variable one way one by one.

14:43.950 --> 14:48.150
So if I select my enemy GameObject in the hierarchy then go over to Inspector.

14:48.150 --> 14:51.670
We can see Fire Point and it says None Transform.

14:51.670 --> 14:56.030
So we haven't created any fire point yet to this player game object.

14:56.030 --> 14:58.270
So we need to assign as a target layer.

14:58.270 --> 15:02.270
We already have chosen player, so make sure you choose that and fire point.

15:02.270 --> 15:03.790
It says none transform.

15:03.790 --> 15:10.070
So we need to create a fire point or a shoot point from where we want to shoot our way in this anime

15:10.070 --> 15:10.830
game object.

15:10.830 --> 15:13.110
So let's select the anime GameObject in the hierarchy.

15:13.110 --> 15:16.470
Let's right click on it, and let's create an empty game object.

15:16.470 --> 15:19.630
And I'm going to call this game object to something like fire point.

15:20.230 --> 15:21.470
Fire point.

15:22.030 --> 15:24.910
And we're going to move this point somewhere over here.

15:24.910 --> 15:28.150
But before we do I'm going to select my enemy game object.

15:28.350 --> 15:31.790
Then I'm gonna move over to this animation tab and then shooting.

15:31.990 --> 15:38.790
So we need to drag the fire point somewhere over here on top of our gun in this anime game object.

15:38.790 --> 15:42.990
So I'm going to select our fire point and we're gonna drag it somewhere over here.

15:43.310 --> 15:45.110
So this is going to be perfect time.

15:45.110 --> 15:45.550
Sorry.

15:45.590 --> 15:46.670
Perfect position.

15:48.270 --> 15:50.030
Uh, somewhere over here.

15:50.030 --> 15:56.430
So if you just, uh, getting snipping or just moving one, uh, grid, uh, kind of a bit weird.

15:56.430 --> 15:58.750
Then in the top, you can see magnetic.

15:58.750 --> 16:01.390
So you can just click that in order to disable.

16:01.390 --> 16:07.390
And now you can freely move that point anywhere you want, or we, uh, basically we're just getting

16:07.390 --> 16:10.830
an error in order to move this, uh, fire point.

16:10.830 --> 16:13.150
And I'm going to select and gizmos for that.

16:13.150 --> 16:13.590
To do.

16:13.590 --> 16:14.950
So let's go to Inspector.

16:14.950 --> 16:20.240
Click on this box and we're going to select this yellow Christmas, something like that.

16:20.240 --> 16:22.840
And now we're going to select our enemy one game object.

16:22.840 --> 16:25.320
And let's select the fire point from that hierarchy.

16:25.320 --> 16:28.320
And let's drag it over to this fire point slot.

16:28.640 --> 16:30.720
And we're going to move over to overrides.

16:30.720 --> 16:32.600
And then click on Apply All.

16:32.840 --> 16:34.680
And now it should be fine.

16:34.680 --> 16:40.360
But before I'm going to select my console tab and let's click on this layer in order to clear out all

16:40.360 --> 16:41.400
of the messages.

16:41.720 --> 16:46.200
And now let's click on this play button in the top in order to play our game.

16:46.520 --> 16:52.960
So as soon as this enemy game object is going to shoot out on this, uh, our, on our player game object,

16:53.280 --> 16:54.160
that should function.

16:54.160 --> 16:54.760
Gonna call.

16:54.760 --> 17:01.040
And in that shoot function, we just are calling the shoot raycast function, which is just doing the

17:01.040 --> 17:03.240
best thing, which is creating our way.

17:03.760 --> 17:10.120
As you can see, as soon as we just collided with this circle, we can see a message just getting pop

17:10.160 --> 17:11.080
up in the console.

17:11.080 --> 17:12.240
Says player takes damage.

17:12.240 --> 17:16.360
So I'm going to click on this pause button in the top in order to pause my game.

17:16.480 --> 17:23.280
And we can see player takes damage after, uh, after some period of time or short period of time,

17:23.280 --> 17:27.280
we can see a message, gets pop up, says disable error.

17:27.480 --> 17:30.360
Uh, basically, we also are doing a thing.

17:30.400 --> 17:34.880
Basically, after this amount of time, we just printing out a message in the console.

17:34.920 --> 17:36.080
Disable error.

17:36.120 --> 17:37.240
As you can see.

17:37.360 --> 17:42.960
So instead of doing all of these things, what we need to do, we need to, uh, show our line renderer

17:43.000 --> 17:46.120
or the laser that we shoot in the left direction.

17:46.120 --> 17:53.360
So we also need to do that, uh, do the thing as we have done in this player game object, which we,

17:53.520 --> 17:56.320
uh, basically attach the line renderer component.

17:56.320 --> 18:00.360
So we need to do the same thing with this player game object as well.

18:00.400 --> 18:06.240
I'm going to click on this play button in the top in order to get out of play mode and make sure you

18:06.240 --> 18:12.480
just go to uh, select your enemy one game object and make that one first prefab and then go to overrides

18:12.480 --> 18:15.400
in the Inspector and then click on Apply All.

18:15.400 --> 18:21.400
So the changes you made, it's going to be saved into this enemy one GameObject prefab as well.
