Let's create a script in order to trigger that shape parameter.
So we're going to create a script for that.
So let's head over to this project tab, then assets then scripts folder.
Let's head over to that.
Let's right click there create and then Monobehaviour script.
Because we're going to attach this script to our game object in hierarchy and name it to something like
Camera check.
I already have created a camera check script and you can see it over here, as you can see.
So we're going to attach this script to any game object of our hierarchy in our hierarchy.
Or we can simply select this camera parent.
Or we also can create a game object for that.
So we're going to create that.
So let's go over to hierarchy.
Let's right click there or hit this place.
Sorry I'm going to enable this one.
So let's go to Inspector and check it.
And let's go to hierarchy.
Right click or hit this plus icon.
And we're going to create an empty game object.
And I'm going to name this game object as the script name something like camera check.
So I'm going to call it camera check.
Sorry.
Camera check.
Camera shake.
Let's call it camera check.
And let's go to transform.
Let's reset the transform.
And I'm going to select my camera check script.
Sorry.
Camera check game object.
And I'm going to drag it all the way to the top in hierarchy.
And let's select the script and let's drag it over to this Add Component section.
And let's double click this to open it up in Visual Studio.
So I'm going to remove this start function.
And also this uh comment from here I'm going to remove that instead.
What we basically need we need a reference of that animator component.
So if we moved over to camera parent then main camera and then move over to animator tab, we can see
the animation is created inside this main camera.
And if we select our main camera then go over to Inspector.
We can see an animator component added once we created this animation clip, this check, and the ideal
one.
So we need a reference of this animator component, uh, in this main camera.
Then we can just trigger this check parameter in animator tab, as you can see.
So we need a reference of this animator component of this main camera.
So let's open up our camera check split by double clicking on it.
And let's go to top of our class.
And let's make a serialized file a serialized file.
So let's make a serialized file private, uh, private.
And we're going to call it, uh, basically we need the reference of that animator component so we can
simply pass animator or the component that we're looking for.
And I'm going to call it animator.
And let's close that up with semicolon.
It's a if you just a bit or worry it about this serialized field, then you can check the chapter where
I have explained how you can make your code clean.
Uh, so you can just check the previous Years, a chapter and the lectures of this.
So now we're going to save by control S in order to save our script.
But before we save, let's also, uh, basically create instance of this camera set so you can go that
chapter or that lecture in order to get, uh, this a singleton pattern and how you can use this serialized
field.
So I'm going to make a public static static.
And we're going to make a camera check, camera check.
And we're going to call it instance.
So we're going to call it instance.
And let's move over to this start function.
And you can use find any object by type as well.
If you just don't want to use this public static camera stick instance.
But I'm going to use that to keep my clean code.
So that's why.
Or you can just ignore this process as well.
It's totally up to you.
Instead of saying serialize will, you can simply mark as a public, then animator and animator.
so you can just do that as well if you, uh, just don't want to do this, so you can do that.
So, uh, what I'm going to do now, I'm going to check if instance is equals, sorry is instance is
equals equals to null.
Then we're going to say sorry we will set instance equal to this.
But before we do I'm going to make the first brackets and let's put it over here.
So I'm going to say instance instance equal to this.
If already instance there then I'm going to make else uh so make some curly brackets.
Then I'm going to destroy the instance if already there is a instance.
So let's close that up with semicolon.
And let's press down Ctrl S to save our script.
And let's move over to animator.
Uh, sorry.
Let's move over to unity.
So underneath our camera script we can see, uh, camera shake script, we can see a animator and it
says none animator.
So the animator component is attached to this main camera.
So I'm going to select our main camera and let's drag it into that slot.
And now we can see we have main camera animator component.
As you can see underneath our camera shake speed basically means we have this animator component by
now of this main camera.
And we are ready to trigger this shake parameter.
So let's go to our script and let's make a function for that.
So I'm going to make it somewhere in the bottom.
And we're going to make that function public.
Why.
Because we're going to call this function through another class or another C script.
So we're going to call that.
So that's why we're going to mark that function as a public.
So we're going to make a public.
And for function we're going to type void.
And I'm going to call this function to something like shake shake.
And let's make first bracket and some curly brackets.
And what we basically want to do, we have the reference of this animator component, as you can see
in the inspector underneath our camera shake script over here, as you can see.
So we have the reference.
So we can use this animator in order to trigger this shake parameter in animator tab we can see.
So let's go to our script.
So what we will do inside this function.
Firstly we will move over to animator component.
And then we're going to use this set trigger function.
Why are we using this set trigger function.
Because this parameter is a trigger parameter.
So that's why we need to use this set trigger function in order to trigger that parameter.
And inside this bracket in set trigger it's going to take the parameter name in quotation.
So in quotation we're going to pass the parameter name.
And the parameter name is sick.
So we're going to simply pass sick.
And let's close that up with semicolon.
So what we simply doing.
We just going over to animator and then triggering that parameter.
Basically we just going over to this main camera animator component and by this animator component we
just triggered in this triggering this shake parameter.
As you can see in the animator tab.
So basically that is the thing.
So whenever we're going to call this check function and what check function is just going to do, it's
going to move over to animator.
And it's going to trigger the parameter called shake.
As you can see.
So we need to call the function when we should call this function once we take damage or shoot.
But for now we're going to press down Ctrl S to save our script.
And let's move to unity.
Or simply you can just toggle it in the player script.
But for for depth explanation I'm going to do uh, in unity editor.
So just wait for to compile.
And once it's done, we are ready to do that.
So now so, uh, when we just want to call the check function once, this enemy game object takes damage
as well as this player game object takes damage.
So we're going to call the function there.
And that function is just going to trigger this check parameter, which basically is checking our camera.
So I'm going to open up my player script.
So let's select the player game object in the hierarchy.
And let's open up the player script by double clicking on it.
So we're going to call the function when we took a taking damage from that enemy.
So as you can see over here we're just taking the damage from the that enemy game object.
So after taking damage we also want to shake our camera.
So we're going to use this camera shake.
Or you can simply use this find any object by type uh and then make this arrows.
And into this arrows.
You need to pass the script name that which script you just looking for.
So in our case camera shake and then make first bracket.
And you can simply call the function by dot.
But instead of doing this I have created the singleton pattern.
As you can see in this camera shake camera shake script which is public static camera shake instant
as you can see.
So I can simply pass the script name which is camera check.
And then by this instance parameter I can call this check function.
So that's why I created the singleton pattern instead of using find any object by type because that
is fairly taxing.
So that's why.
Or you can just ignore this.
Well if you just don't want to, don't want to basically call it a something like with singleton pattern,
then you can just ignore this as start function as well as this one.
So I hope you get the idea.
So I'm gonna move over to my player script.
So I have the singleton pattern.
So I'm going to simply pass my camera check script name.
And then I'm going to move over to Winston.
And then I'm going to call the check function.
And let's make first bracket and let's call close that output semicolon.
So basically just going over to this camera check is a class.
And then by this instance parameter I'm just calling the check function.
And what check function is doing is just checking our camera or basically triggering that parameter,
which is creating the shake animation.
So let's go to player skip.
Basically that we just simply calling the shake function, as you can see over here.
But when we calling it once we're taking damage as you can see.
So we're going to press down Ctrl S to save our skip.
And let's move over to unity.
So we're gonna able to see that.
So just wait for to compile.
So whenever we're gonna take damage from this enemy game object we will able to see the camera.
Just gonna check right away.
So let's click on this play button in the top to have a look on this.
And let's check that out.
Let's let's get closer to this enemy game object in order to take the damage from this enemy.
So let's have a look on this.
So just wait.
As you can see, as soon as we taking damage from this enemy anime game object.
We can see the camera is shaking and we also want to shake the camera.
Once we once this anime to game object as well as the first anime taking damage.
We also want to call the check function.
Basically, it's going to check our.
You can simply say our camera.
So let's get out of play mode and let's select the anime to game object in the hierarchy.
And let's open up the anime to script.
So let's go to that function where we taking damage so we can see we taking damage over here.
So after subtracting health I'm going to call the check function.
So let's go to camera check.
So camera check is a script that instant.
And then let's call the check function and make first bracket.
Close that up with semicolon.
And I'm going to copy this line of code by Ctrl C.
And I'm going to paste it in anime one as well.
But before we do let's press Ctrl S to save our script.
And let's go to first anime or anime one script.
So this is a this script is attached to the first anime game object.
So let's move over to Takedamage function.
So over here we're taking damage.
So I'm going to paste it by Ctrl V basically calling the check function as you can see.
So once we take damage and I'm going to press down Ctrl S to save my script.
But I also want to check my script once our enemy or the player game object totally get explored.
So over here we can see we have the die function in inside our first enemy script.
So I'm going to call the function over here as well by Ctrl V.
So let's paste it basically calling the check function.
And I'm going to do the same thing with my enemy too and as well as a player.
So let's go to enemy two script.
Let's move over to die function.
And I'm going to paste the function over here as well as I'm going to move over to my player script
and let's paste the dive.
Uh, let's paste that line of code inside this dive function by Ctrl V.
Now this is fine means whenever we just destroying our player enemy one and enemy two game GameObject.
We are also calling the check function which is checking our camera.
So I'm going to press down Ctrl S to save my script.
And let's move to unity by now.
So whenever this player and this enemy GameObject are just gonna explode or die, you will see.
The camera will also say so just wait for it to compile and once it's done, we are ready to do that.
So I'm going to click on this play button in the top to play our game.
And let's take a look of what we have done so far.
So just wait for to start our game.
And once it's done, we are ready to do that.
So I'm gonna, uh, shoot on this enemy game object.
And as soon as enemy is taking damage, as you seen, uh, as you saw, the camera is just shaking right
away.
So I'm going to get out of play mode.
I'm going to check it one more time.
So let's click on this play button.
But before I do, I'm just gonna give a little more health to this enemy GameObject for testing purposes.
So I'm gonna select my enemy to GameObject in the hierarchy.
And let's give Maxhealth something around five.
And let's click on this play button in the top.
So I just increased my enemy to health.
For testing purposes.
But it's totally up to you how much health you just want to give to your enemy GameObject.
It's totally up to you!
So I'm going to shoot on this enemy GameObject.
As you can see, as soon as it's taking damage, the camera just shaking right away and we can see that.
I hope you get the idea.
So I'm gonna get out of play mode and let's take a look on this player.
Basically, once this player GameObject takes damage or basically a die, the camera just gonna shake,
uh, as well.
So let's click on this play button and I'm not gonna attack on this enemy two game object right now
because I want to check for now our with our player game object.
So let's have a look on this.
Firstly we need to get closer to our enemy GameObject.
Then it's gonna attack on our player Layer GameObject.
And as you can see indeed our player just taking damage and once it's die, the camera is also getting
shaking.
So whenever it takes damage player the camera is seeking as well as when it's dying.
I hope you get the idea.
So I'm gonna get out of play mode and that's it.
And I'm going to make this camera shake game object as well a prefab.
So we're going to have the copy.
So I'm going to select my camera GameObject in the hierarchy.
And let's drag it over to this prefab folder.
Or you can simply drag it into this assets folder in project tab.
But I'm going to drag it over my, uh, over on my prefabs folder because I have created a folder for
our prefabs and I'm going to select my camera shake prefab, and I'm gonna drag it over to this, uh,
object sub folder because, uh, I created a separate folder for those objects prefab.
You can simply say.
And now let's press down Ctrl s.