WEBVTT

00:00.040 --> 00:02.080
We will use singleton pattern.

00:02.520 --> 00:07.120
So instead of find any object of type we're going to use singleton pattern.

00:07.280 --> 00:13.720
And if you don't know what is singleton pattern then you can uh you can see the previous lecture.

00:13.720 --> 00:19.520
It's it may be something around lecture number 38 or 30 9 or 40.

00:19.560 --> 00:21.200
So you can check that out.

00:21.240 --> 00:22.880
I already explained it.

00:23.000 --> 00:26.680
What is singleton pattern and how you can use that.

00:26.760 --> 00:28.720
So we gonna do that.

00:28.760 --> 00:31.600
Basically we just want to implement our code.

00:31.720 --> 00:36.440
So I'm going to select my player game object and let's go over to Inspector then player Skip.

00:36.440 --> 00:40.720
And let's double click on that in order to open up in Visual Studio.

00:41.120 --> 00:47.480
Uh so as you can notice right over here we're using this find any object of type camera check.

00:47.480 --> 00:48.840
And this this check.

00:48.880 --> 00:51.120
As you can see it's pretty long.

00:51.120 --> 00:55.880
So instead of doing that doing this we're going to use the singleton pattern.

00:55.880 --> 01:00.320
So we'll so simply we're just going to move over to our camera shake script.

01:00.320 --> 01:07.350
And we will go to top of our class, and we're going to make it public so we can access public, and

01:07.350 --> 01:09.190
we're gonna use static.

01:09.190 --> 01:15.590
So we using this static so we can simply access any variable function from this class.

01:15.910 --> 01:19.110
So without basically typing anything like that.

01:19.110 --> 01:22.670
So we can directly uh directly access.

01:22.670 --> 01:29.950
So that's why we type static and we need to uh, we basically have to name the variable which is our

01:29.950 --> 01:30.310
camera.

01:30.710 --> 01:34.310
So basically the we need to type the script name.

01:34.550 --> 01:39.190
And we need, need to uh, we basically need to name the variable.

01:39.190 --> 01:41.870
So let's call it instance something like that.

01:41.870 --> 01:44.110
And let's close that up with semicolon.

01:44.110 --> 01:48.910
And if you little bit confused then you can just go ahead and check that lecture.

01:49.310 --> 01:53.430
I have explained it in the Endless runner courses.

01:53.430 --> 01:56.510
And the lecture number may be something like 38.

01:56.510 --> 01:58.230
So you can check that out.

01:58.590 --> 02:00.350
So we're going to use the start function.

02:00.350 --> 02:02.430
So let's use this start That function.

02:02.590 --> 02:09.150
So what we want to do over here, we just want to check if there is a no instance, then we're going

02:09.150 --> 02:11.070
to set the instance of this class.

02:11.430 --> 02:18.310
And if they are already instance basically an object for this class then we're going to destroy that.

02:18.550 --> 02:20.030
So firstly we will check.

02:20.030 --> 02:21.630
So let's use this if condition.

02:21.630 --> 02:24.550
Let's make first bracket and some curly brackets.

02:24.550 --> 02:30.750
And we want to check if our instance equals equals to null means we don't have it.

02:30.750 --> 02:36.350
So what we will do we will set the instance equal to this, instance equal to this.

02:36.350 --> 02:42.310
And let's suppose let's suppose there is a already instance of this camera check script.

02:42.310 --> 02:43.790
So we're gonna destroy that.

02:43.790 --> 02:44.430
So else.

02:44.430 --> 02:48.670
If they are already a instance then what we're gonna do, we're gonna destroy that.

02:48.670 --> 02:53.870
So we're going to use this destroy function, and it's going to take the game object that we want to

02:53.910 --> 02:54.510
destroy.

02:54.510 --> 02:57.230
So we want to destroy this instance itself.

02:57.230 --> 03:01.310
So we're going to pass this this and let's close that up with semicolon.

03:01.310 --> 03:06.900
And we're going to press down Ctrl s to save our ship, and now we can just move over to our player

03:06.900 --> 03:07.260
ship.

03:07.300 --> 03:08.100
Enemy one.

03:08.140 --> 03:08.900
Enemy two.

03:09.180 --> 03:13.180
And we can just directly access the check function without typing.

03:13.340 --> 03:17.620
Like find any object by type, any object by type.

03:17.780 --> 03:21.100
And inside those arrows, we need to pass the script name.

03:21.380 --> 03:23.100
So we don't need to do that.

03:23.260 --> 03:28.780
And we can just directly access the check function from any class that we want.

03:28.980 --> 03:33.220
So I'm going to open up my enemy one game object first and then enemy two.

03:33.620 --> 03:38.900
And we're going to call the function there once our enemy one or enemy two game object.

03:38.900 --> 03:43.540
Just gonna just gonna take damage or this player game object.

03:43.900 --> 03:50.500
So we storing all of the information in this instance parameter right over here, as you can see.

03:50.740 --> 03:53.700
And we're storing all of this like this check function.

03:53.700 --> 03:58.540
All of these parameters start function and all of this into this instance.

03:58.540 --> 04:01.860
So basically we need to go over to this camera check then instance.

04:01.860 --> 04:07.770
And we can just access basically call the seek function and we can just do that instead of typing.

04:07.810 --> 04:09.490
Find any object by type.

04:09.610 --> 04:11.770
Now the compilation is done.

04:11.770 --> 04:15.370
So let's go to script and sorry let's go to unity Editor.

04:15.610 --> 04:18.690
And I'm going to select my player game object in the hierarchy.

04:18.690 --> 04:21.770
And let's open up the player script by double clicking on it.

04:21.770 --> 04:24.530
And let's move over to our Takedamage function.

04:24.530 --> 04:27.930
So over here as you can notice we taking damage.

04:27.930 --> 04:29.850
So we need to check our camera.

04:29.850 --> 04:32.850
And we're using this find any object by type.

04:32.850 --> 04:39.650
So instead of doing this we can directly uh put the directly put the class name which is camera.

04:40.050 --> 04:42.690
And then we're going to access the instant.

04:42.690 --> 04:47.330
And we do know we stored all of the information this instant parameter.

04:47.330 --> 04:49.010
So we're gonna go to instant.

04:49.010 --> 04:54.450
And then we're gonna call the shake function which is we need to type the shake function.

04:54.450 --> 04:56.450
And now we need to make first bracket.

04:56.450 --> 04:58.450
Then close that up with semicolon.

04:58.450 --> 05:02.250
And we definitely know that this shake function as a first argument.

05:02.250 --> 05:04.410
It's going to take the duration of the shake.

05:04.450 --> 05:10.890
So I'm going to pass something, a smaller number, something a few seconds, something like uh, 0.1

05:10.930 --> 05:11.330
F.

05:11.330 --> 05:17.490
And the second thing, it's going to take the intensity with how much intense we want to shake our camera.

05:17.850 --> 05:21.290
So we're going to check with something like three F or three unit.

05:21.490 --> 05:22.850
So what are we doing.

05:22.890 --> 05:28.330
We simply going over to this camera shake script and then going over to this instant parameter.

05:28.330 --> 05:32.530
And by this instant parameter we basically calling this shake function.

05:32.530 --> 05:34.850
As you can notice right over here.

05:34.850 --> 05:36.850
And we're going to copy that line of code.

05:36.850 --> 05:39.850
So let's move over to our player script one more time.

05:39.850 --> 05:41.850
And let's copy this by Ctrl C.

05:42.090 --> 05:46.010
And make sure you press Ctrl S in order to save your script.

05:46.170 --> 05:48.810
And we will move over to our anime one.

05:49.010 --> 05:50.410
And let's remove this.

05:50.410 --> 05:52.730
Find any object by type.

05:52.730 --> 05:58.610
Instead of doing that, we're going to paste this line of code which is camera shake uh, dot instant

05:58.650 --> 05:59.290
dot shake.

05:59.330 --> 06:04.090
Basically we're just going over to that camera shake script, then going over to instant.

06:04.090 --> 06:08.360
And by this instant we which is calling the save function, as you can notice.

06:08.560 --> 06:15.440
And if you, uh, you can just play around with the value, uh, how much intense or how much duration,

06:15.480 --> 06:17.560
you just want to shake your camera.

06:17.680 --> 06:22.360
So for my enemy cases, I'm just gonna do a little bit higher.

06:22.640 --> 06:26.200
Uh, something like 0.2, uh, point two duration.

06:26.400 --> 06:29.560
And also, I just want to increase the intensity.

06:29.560 --> 06:31.400
Something around four unit.

06:31.560 --> 06:33.680
And I'm going to copy this line of code.

06:33.680 --> 06:36.680
And now let's move over to our enemy two script.

06:36.840 --> 06:42.760
And before you do that make sure you press on control S as well inside this animation script.

06:42.760 --> 06:45.560
So whatever changes you made, it's gonna be saved.

06:45.720 --> 06:51.440
And now we're just gonna move over to our enemy two script and let's move over to Takedamage function.

06:51.440 --> 06:59.080
Over here we can see enemy is taking damage, so let's paste the face that line of code as well over

06:59.080 --> 06:59.640
here.

06:59.640 --> 07:02.880
And now I'm going to press down Ctrl S to save my script.

07:02.880 --> 07:04.920
And now let's move over to unity.

07:04.920 --> 07:11.710
And you will see as soon as our enemy or player just gonna take damage, the camera will start shaking

07:11.950 --> 07:16.110
because we just calling the shape function and we just type static.

07:16.110 --> 07:23.270
So we just type static because we can directly access any value or any function as you seen.

07:23.270 --> 07:25.910
So that's why we need to type public static.

07:26.070 --> 07:30.590
And the class name and the parameter name that you want to name.

07:30.870 --> 07:36.510
So now if we just go ahead and then click on this play button in the top.

07:36.510 --> 07:42.790
You will see as soon as we can take damage from this enemy one game object or from this player game

07:42.830 --> 07:45.430
object camera will start shaking.

07:46.830 --> 07:48.670
So just wait a little moment.

07:52.230 --> 07:58.190
So now if we take damage, as you can see, indeed we take camera is shaking.

07:58.190 --> 08:06.190
But once I, uh, basically attack on my enemy one game object, we can see the camera is shaking more

08:06.190 --> 08:13.590
intensely because we just as a second argument, we pass the intense a little bit higher as compared

08:13.590 --> 08:15.150
to our player one.

08:15.310 --> 08:17.750
Sorry from this player game object.

08:17.750 --> 08:23.790
So I hope you get the idea that how you can do it, as you can see, uh, as you can notice, we taking

08:23.830 --> 08:28.750
damage and the camera just shaking and that's fine.

08:28.750 --> 08:31.150
And we can attack in order to shake.

08:31.630 --> 08:33.390
So this are working fine.

08:33.990 --> 08:37.390
And we're gonna do the same thing with enemy two game object.

08:37.390 --> 08:39.870
And we do know that we already done it.

08:39.870 --> 08:44.630
So I'm going to get out of play mode and I'm going to select my enemy one game object.

08:44.630 --> 08:48.670
And let's uh, let's disable for a few moment.

08:48.670 --> 08:51.790
So I'm going to select my enemy one GameObject in the hierarchy.

08:51.830 --> 08:53.310
Let's go over to Inspector.

08:53.310 --> 08:55.950
And we're gonna uncheck basically disabling that.

08:55.990 --> 08:59.870
And now we're going to select our enemy to let's go over to Inspector.

08:59.870 --> 09:01.030
And we're going to check this.

09:01.030 --> 09:04.670
Basically we just enabling our enemy to game object.

09:04.670 --> 09:08.070
And let's check with this enemy to game object.

09:08.070 --> 09:08.710
So that's why.
