We will use singleton pattern.
So instead of find any object of type we're going to use singleton pattern.
And if you don't know what is singleton pattern then you can uh you can see the previous lecture.
It's it may be something around lecture number 38 or 30 9 or 40.
So you can check that out.
I already explained it.
What is singleton pattern and how you can use that.
So we gonna do that.
Basically we just want to implement our code.
So I'm going to select my player game object and let's go over to Inspector then player Skip.
And let's double click on that in order to open up in Visual Studio.
Uh so as you can notice right over here we're using this find any object of type camera check.
And this this check.
As you can see it's pretty long.
So instead of doing that doing this we're going to use the singleton pattern.
So we'll so simply we're just going to move over to our camera shake script.
And we will go to top of our class, and we're going to make it public so we can access public, and
we're gonna use static.
So we using this static so we can simply access any variable function from this class.
So without basically typing anything like that.
So we can directly uh directly access.
So that's why we type static and we need to uh, we basically have to name the variable which is our
camera.
So basically the we need to type the script name.
And we need, need to uh, we basically need to name the variable.
So let's call it instance something like that.
And let's close that up with semicolon.
And if you little bit confused then you can just go ahead and check that lecture.
I have explained it in the Endless runner courses.
And the lecture number may be something like 38.
So you can check that out.
So we're going to use the start function.
So let's use this start That function.
So what we want to do over here, we just want to check if there is a no instance, then we're going
to set the instance of this class.
And if they are already instance basically an object for this class then we're going to destroy that.
So firstly we will check.
So let's use this if condition.
Let's make first bracket and some curly brackets.
And we want to check if our instance equals equals to null means we don't have it.
So what we will do we will set the instance equal to this, instance equal to this.
And let's suppose let's suppose there is a already instance of this camera check script.
So we're gonna destroy that.
So else.
If they are already a instance then what we're gonna do, we're gonna destroy that.
So we're going to use this destroy function, and it's going to take the game object that we want to
destroy.
So we want to destroy this instance itself.
So we're going to pass this this and let's close that up with semicolon.
And we're going to press down Ctrl s to save our ship, and now we can just move over to our player
ship.
Enemy one.
Enemy two.
And we can just directly access the check function without typing.
Like find any object by type, any object by type.
And inside those arrows, we need to pass the script name.
So we don't need to do that.
And we can just directly access the check function from any class that we want.
So I'm going to open up my enemy one game object first and then enemy two.
And we're going to call the function there once our enemy one or enemy two game object.
Just gonna just gonna take damage or this player game object.
So we storing all of the information in this instance parameter right over here, as you can see.
And we're storing all of this like this check function.
All of these parameters start function and all of this into this instance.
So basically we need to go over to this camera check then instance.
And we can just access basically call the seek function and we can just do that instead of typing.
Find any object by type.
Now the compilation is done.
So let's go to script and sorry let's go to unity Editor.
And I'm going to select my player game object in the hierarchy.
And let's open up the player script by double clicking on it.
And let's move over to our Takedamage function.
So over here as you can notice we taking damage.
So we need to check our camera.
And we're using this find any object by type.
So instead of doing this we can directly uh put the directly put the class name which is camera.
And then we're going to access the instant.
And we do know we stored all of the information this instant parameter.
So we're gonna go to instant.
And then we're gonna call the shake function which is we need to type the shake function.
And now we need to make first bracket.
Then close that up with semicolon.
And we definitely know that this shake function as a first argument.
It's going to take the duration of the shake.
So I'm going to pass something, a smaller number, something a few seconds, something like uh, 0.1
F.
And the second thing, it's going to take the intensity with how much intense we want to shake our camera.
So we're going to check with something like three F or three unit.
So what are we doing.
We simply going over to this camera shake script and then going over to this instant parameter.
And by this instant parameter we basically calling this shake function.
As you can notice right over here.
And we're going to copy that line of code.
So let's move over to our player script one more time.
And let's copy this by Ctrl C.
And make sure you press Ctrl S in order to save your script.
And we will move over to our anime one.
And let's remove this.
Find any object by type.
Instead of doing that, we're going to paste this line of code which is camera shake uh, dot instant
dot shake.
Basically we're just going over to that camera shake script, then going over to instant.
And by this instant we which is calling the save function, as you can notice.
And if you, uh, you can just play around with the value, uh, how much intense or how much duration,
you just want to shake your camera.
So for my enemy cases, I'm just gonna do a little bit higher.
Uh, something like 0.2, uh, point two duration.
And also, I just want to increase the intensity.
Something around four unit.
And I'm going to copy this line of code.
And now let's move over to our enemy two script.
And before you do that make sure you press on control S as well inside this animation script.
So whatever changes you made, it's gonna be saved.
And now we're just gonna move over to our enemy two script and let's move over to Takedamage function.
Over here we can see enemy is taking damage, so let's paste the face that line of code as well over
here.
And now I'm going to press down Ctrl S to save my script.
And now let's move over to unity.
And you will see as soon as our enemy or player just gonna take damage, the camera will start shaking
because we just calling the shape function and we just type static.
So we just type static because we can directly access any value or any function as you seen.
So that's why we need to type public static.
And the class name and the parameter name that you want to name.
So now if we just go ahead and then click on this play button in the top.
You will see as soon as we can take damage from this enemy one game object or from this player game
object camera will start shaking.
So just wait a little moment.
So now if we take damage, as you can see, indeed we take camera is shaking.
But once I, uh, basically attack on my enemy one game object, we can see the camera is shaking more
intensely because we just as a second argument, we pass the intense a little bit higher as compared
to our player one.
Sorry from this player game object.
So I hope you get the idea that how you can do it, as you can see, uh, as you can notice, we taking
damage and the camera just shaking and that's fine.
And we can attack in order to shake.
So this are working fine.
And we're gonna do the same thing with enemy two game object.
And we do know that we already done it.
So I'm going to get out of play mode and I'm going to select my enemy one game object.
And let's uh, let's disable for a few moment.
So I'm going to select my enemy one GameObject in the hierarchy.
Let's go over to Inspector.
And we're gonna uncheck basically disabling that.
And now we're going to select our enemy to let's go over to Inspector.
And we're going to check this.
Basically we just enabling our enemy to game object.
And let's check with this enemy to game object.
So that's why.