Now we're gonna make the cinemachine camera stick with our Cinemachine camera, so we will select our
Cinemachine camera in the hierarchy, go over to Inspector, then Cinemachine camera, and in the bottom
you can see noise and it says none.
So just click there and choose this basic multi-channel Perlin.
And as soon as we clicked on it and if we scroll down to the bottom, we can see a component just added
up, which is Cinemachine basic multi-channel Perlin component.
And we can see noise profile and it says noise setting.
So what we need to do we need to select 66.
So just click in this settings icon.
In the right side you can see basically the icon.
Then go over to presets and then choose the 6666.
And now if we go ahead and then click on this play button, you will see as soon as our game just gonna
start, the camera will start shaking with this amplitude gain and this frequency gain.
So firstly what you need to do.
You basically need to move over to Cinemachine camera and then you need to choose choose this noise
profile to basic multi-channel burning.
So just wait a little moment.
So as you can see, as soon as our game just started, camera started shaking and we can just play around
with the value.
So if I move over to my inspector, scroll down to Cinemachine basic multi-channel Perlin component
and increase this amplitude gain something like two.
As you can see, it's shaking more instantly and we can play around with the frequency gain as well
as well.
So we can see camera is shaking with too much fast, basically, uh, too much with too much intensity.
And if we set back this amplitude back to zero, we can notice that camera stops shaking.
And if we set this amplitude gain to some numbers with some numbers something, let's suppose two.
We can notice that it's shaking.
But once we set back this amplitude gain to zero, we can notice that the camera just stops shaking.
So what we basically need we need a reference of this cinemachine basic multichannel Berlin component.
And then we can just access the value and we can just do that.
So I'm going to get out of play mode.
And by default amplitude again I'm gonna set it to zero since I don't want my camera to check right
after the game start.
And I want to increase the frequency gain to something around two.
So firstly, what you need to do, let me explain.
What one more time.
Select your cinemachine camera in the hierarchy, then go over to Inspector, then Cinemachine camera
component.
And in the bottom you can see noise and it will be none.
So choose this basic multichannel Perlin and as soon as you gonna select that in the bottom scroll down
a component just gonna attach which says Cinemachine basic multi-channel Perlin.
As you can notice a noise profile.
It will be done.
So just click on the settings icon in the right side or this cog icon.
Then go over to presets and you need to choose this 66.
And as soon as you're going to do that and then you're going to get this amplitude gain value.
And basically the camera just gonna start shaking.
If we set this amplitude gain with some numbers.
And we also need to set back to zero.
So it's not gonna shake our camera at all.
So what we need we need a reference of this cinemachine camera component camera component or basically
this Cinemachine camera.
And after that we can access this component and we can just access this parameter and we can set with
some numbers.
So let's create a game object for that.
Or we can simply select our Cinemachine camera GameObject in the hierarchy.
Then go over to Inspector.
Click on Add Component Add component.
So just click there, then New script.
Name the script whatever you like, something like Camera Shake and then click on this Create an add
button in the bottom you can notice.
So I hope you get the idea that how you can do it so or else what you can do, you can just move over
to your project app assets and then script folder, right click there, create, then Monobehaviour
script.
And you can name that script to something like camera shake and attach that script to this Cinemachine
camera camera, game object.
Or you can just create a separate GameObject in order to make that.
So as you can notice right over here, I already have a camera shake script.
I already created that.
So I'm going to choose my Cinemachine camera in the hierarchy.
And let's choose that camera shake script and drag it over to this add component section in the inspector,
or else you can just create a separate GameObject and you can just attach that script to that GameObject.
It's basically up to you.
And now we're gonna open up our Camera Shake script by double clicking on it.
So what we need we need our Cinemachine.
So before we get the reference of this Cinemachine camera, we need to use this namespace.
So whenever we working with Cinemachine camera, we need to move over to top of our class and we need
to include this namespace.
It's called using a using unity dot unity dot Cinemachine as Unity's Cinemachine.
And if you're using older version of unity, then you have to use using Cinemachine.
I guess like that, but in new we have to use this.
It's called basically we need to include this namespace.
It's called using unity dot Cinemachine.
So make sure you do that.
And now what we're going to do we're going to remove this start function.
Or we can leave that to grab the reference.
I'm not gonna grab the reference in the start of the game or with this start function I'm just gonna
grab a manually.
So that's why I'm gonna remove this start function.
And we're gonna.
So we need a reference of this cinemachine camera basically this one.
So after that we can just access this cinemachine basic multi-channel component and we can set the amplitude
value.
So let's go to our script first and let's grab the reference of the camera first which is Cinemachine
camera.
So let's make it public.
So let's make a marked as serialized field instead of doing that.
So our code is just going to be a little bit clean private.
And this will be Cinemachine camera.
So we're going to type Cinemachine camera the component.
Sorry the GameObject are we looking for is Cinemachine camera.
And we can call this one Cinemachine camera.
Or we shortly let's call it cam for camera.
And now we're going to press down Ctrl S in order to save our script and then move over to unity.
So make sure as soon as your script just gonna compile, take your Cinemachine camera and drag it over
to that slot.
So we will have the reference.
And after that, we can set the amplitude gain to any value that we would like to go with.
So just wait a moment and once our compilation is done, we're just gonna select our cinemachine camera
and we're gonna drag it over to our cam slot.
And now we can notice that as soon as our camera camera script compiles, we can see came and it says
none Cinemachine camera.
So we're going to take our Cinemachine camera from here and drag it over to that slot.
Or you can simply select the game object and then drag it over to the slot.
And it's going to do the same thing.
Basically, you need to take your Cinemachine camera and drag it over to that cam slot or into that
variable that we created.
And now we have the reference as you can notice, and we are ready to shake it.
So we're going to open up our camera shake script by double clicking on it.
And let's make a function for shaking.
And we will make this function public.
Since we're going to call this function from another class or another script like once our enemy game
object or player game object takes damage, then we just want to shake our camera.
So that's why we're going to mark this as function as public, since we're going to call this function
from another class or another script.
So that's why.
So we're going to mark this function public.
And for function we're going to type void.
And we have to name the function.
So I'm going to call it something like shake.
And let's make first bracket and some curly brackets.
And this function over here as a first argument it's going to take the duration of the shake.
And later on we're going to replace with some number.
Uh so let's create a variable in the top of our uh class to just keep track of when we started shaking.
So what we're going to do, we're going to create a private and this will be float.
And we can call this one something like a start time.
So let's call it start time.
And we're going to close that off with a semicolon.
And in the end what we're going to do now.
So this shape function just going to take two arguments.
First thing it's going to take the duration.
And the second thing it's going to be the intensity with how much intense.
We just want to shake our camera.
And we created this start time variable up here to just keep track of when we started shaking our camera.
So basically that is the thing.
I hope you get the idea.
So the first thing float, this will be float.
And we can call this one duration or shake time something like that and start time.
Sorry I'm going to call this as shake time instead of calling start time.
So I'm going to call it Shake time.
And second thing this shake function just going to take the intensity or how much intense we want to
shake our camera intensely.
We want.
And that will be also a float.
And we can call this one intensity intensity.
So and later on we're going to replace this duration and this intensity with some number something like
we duration.
We can just set it to something around 0.2 second.
We just want to check and intensity.
We can just pass something like two.
So we can set the amplitude gain to something like two three.
So that's why we just given arguments.
And later on when we're going to call this check function, we're going to replace this duration and
the intensity with real numbers.
And that will be float like 0.1 or 1, two, three, four like that.
And now so before so when we're going to call the seq function, we're going to check our check time.
And we're going to set equal to our duration a duration.
And let's close that up with semicolon.
So what we have done over here we just set our check time equal to duration.
So we just created this time variable to just keep track of when we started checking.
So let's suppose uh as a duration we pass something like 0.1.
So this this duration right over here it's going to be 0.1.
So that's why this check time over here it's going to be 0.1.
Why?
Because we directly setting it as you can see, and we just set equal to this set equal to our duration
to just keep track of when we started shaking our camera.
So that's is the thing.
And now what we need, we need a reference.
Uh, basically we need to get this animation basic multi-channel multi-channel perlin component.
We have the Cinemachine camera and reference.
So we're going to get the component which is Cinemachine basic multi-channel Perlin.
So let's get the component.
So the component we're looking for.
So which is Cinemachine.
So what we're going to do firstly we will go to cam.
Cam.
Basically we're just going over to that Cinemachine camera.
And now we're going to use this dot get component uh dot get component.
And inside this arrows it's going to take the component that we want to get.
So we want to get that basic multi-channel and perlin as you seen.
So I'm going to pass inside this arrow which is basic multi cinemachine basic multi-channel burning
and then make this first bracket and then close that up with semicolon.
So now we have the reference as you can notice.
And now what we're going to do, we're going to store this cinemachine basic multi-channel Berlin into
another cinemachine basic multi-channel Berlin variable.
Or you can simply say in a container a container and the container should be type Cinemachine basic
multi-channel Berlin.
Why?
Because this we're just getting the component Cinemachine basic multi-channel Berlin.
So that's why we need to put it in a variable, or in a container or a parameter with the same type.
And we have to name the container or the variable.
And we're going to call it Berlin simply.
And let's set equal to this.
So firstly we're going over to Cinemachine camera.
As you can see name came.
Then we just using this dot get component.
And the component we're looking for is Cinemachine basic multi-channel Berlin.
As you can notice right over here basically we're just going over to this animation camera uh camera
component first.
Or you can simply say Cinemachine camera.
Camera.
And after that we just taking this cinemachine basic multi-channel.
Berlin component.
And we storing that into this Berlin as you can see right over here.
So basically we're just taking that and basically this one and we're storing right over here into this
Berlin.
Now we have the uh having that component.
And now we can access the parameter.
So what we're going to do now we're going to do this Berlin Dot.
And we want to access the amplitude gain.
Basically we want to access this amplitude gain, this amplitude gain.
As you can notice in the inspector.
And we're going to set this amplitude gain equal to our intensity or with how much intensity we want
to shake our camera.
So that's why we're going to access Perlin dot Perlin.
And we're going to access the amplitude gain.
And we're going to set equal to our intensity.
And now we're going to close that off with semicolon.
So firstly, what are we doing?
Firstly going over to this cinemachine camera, then getting this cinemachine basic multi-channel Berlin
component, and we storing this one into this cinemachine basic multi-channel pardon or into this Berlin,
as you can see right over here.
And after that we just go accessing this amplitude gain.
As you can notice right over here.
Firstly going over to this cinemachine basic multi-channel Berlin and then accessing this amplitude
gain.
And we setting this amplitude gain equal to our intensity as you can notice.
So firstly we're going to Berlin and then we access the amplitude gain.
And we setting equal to intensity.
As you can notice right over here.
So I hope you get the idea.
And let's suppose as an intensity you pass two.
So this intensity right over here it's going to be two.
So this amplitude gain.
So Berlin dot amplitude gain just going to be two.
As you can notice because we directly setting it as you can see.
So this amplitude gain is just going to be true when we're going to set basically when we're going to
replace this intensity with our real numbers.
And now we have done we have done these things.
And we can simply press down control s to save our script.
And now we need to call this function.
So I'm going to press Ctrl S to save my script.
And let's move over to unity.
So it's going to be fine.
And we need to call this save function.
But when we need to call this function once our player game object or enemy game object just going to
take damage or just going to attack.
So uh, then we're going to call that.
So I'm going to call it once our player game object gonna take damage.
So we will select our player GameObject in the hierarchy.
And let's open up the player script by double clicking on it.
And let's move over to Takedamage function basically where we're taking damage.
So if we scroll down and move over to take damage function, and we made it in the bottom right over
here, as you can notice.
And we just subtracting our health basically with this damage amount.
So we also want to check our camera.
So basically what we need to do we basically need to go over to that camera shake script.
And then we need to call the shake function.
And we do know what that shake function is going to do.
It's gonna shake our camera with, uh, intensity.
So firstly we're going to use this find any object by type.
Uh.
So use this and then make this arrows.
And inside this arrows you need to pass the script name script name that you're looking for or what
you're looking for.
So we're looking for our camera shake script as we created.
So that's why in this arrows we're going to pass this camera shake.
Sorry.
Uh we're going to pass this camera shake camera shake.
And we need to make this brackets.
And after making brackets, we can just access anything that we like.
So we want to call the shake function.
So that's why we're going to type this dot and we're going to call the shake function.
So let's type this check and let's make first bracket and then close that up with semicolon.
And now this check function as a as a first argument it's going to take the duration uh with how much
duration.
We just want to check.
So I want to check my camera for a little moment or few seconds less than few seconds.
So that's why I'm going to pass something like 0.15.
And the second thing, it's going to take the intensity, uh, with how much intensity or amplitude
gain.
We just want to set width.
So I'm gonna set with something around two f.
So now we're going to press down ctrl s.
So what we're doing once our player game object takes damage then we're just going over to that camera
shake script and then calling the save function.
And as a first argument we just pass 0.15.
And as an intensity we just pass to F.
So the amplitude gain is just going to be two.
And let me explain one more time.
So just wait a moment.
And once our compilation is done we can just go ahead and check that out.
So now.
So now let me explain.
So if I select my cinemachine camera camera in the hierarchy then go over to camera script in Inspector.
So basically we just go going over to this class as you can notice right over here.
And then we're calling this check function.
And we do know what check function just gonna do as.
So as a duration we pass 0.15.
So this is going to be 0.15.
So so this much of time we want to check.
And the second thing we pass intensity.
And as you seen as in intensity we pass two.
So this intensity right over here it's going to be two.
So that's why amplitude gain just going to be two right away means our camera just gonna check with
two a unit of uh with two unit.
So now let's move over to script sorry unity editor.
And let's click on this play button in the top in order to play our game.
So as soon as our player just gonna take damage, it will.
The camera just gonna start shaking and you will see that.
Uh, so just wait a moment.
And if we get closer to this enemy, uh, one game object and enemy.
Just gonna try to shoot.
As you can see, indeed, the camera is shaking, and it's shaking, uh, for longer time.
Uh, so we also need to set back this amplitude gain to zero.
So as you can notice, inside this cinemachine basic multi-channel Perlin component in the inspector,
we can see amplitude gain is two.
So we also need to set back to zero.
So the camera will stop shaking.
So if we set back to zero as you can see camera stop shaking.
So we also need to set back to zero this amplitude gain uh amplitude gain.
So we're gonna get out of play mode.
But when we shoot once the duration is over.
So that's why we just created that parameter, which is duration, uh, inside our shake function.
So we will move over to script.
And we created this time as you can notice.
So.
So if we call this function it's going to take our camera.
And the reason we pass .158.
So this time right over here it's going to be .15 F.
So this is going to be a bigger than zero basically greater than zero number.
So what we would do what we going to do.
Now we're going to move over to this update function.
And update function runs every single frame.
And over here we're going to check.
So we're going to use this if condition.
Let's make first bracket and some curly brackets.
And what we want to check over here.
We basically want to check if our check time if our check time is greater than zero.
If if if it's greater than zero f then we need to subtract some amount from this check time.
So we do know that once we're going to call this check time check function, this check time just going
to be duration and duration.
We pass .11 f.
So this time over here, it's going to be 0.158.
And so that's why.
And we do know that .15.
So this time is say time is 0.15.
So and we do know that 0.15 is greater than zero.
Basically it's greater than zero.
So so in that moment what we just want to do, we just want to subtract from this time as some amount
every single frame until and unless this time it hits zero.
And when it hits when it hits zero, then we will stop shaking our camera.
So what we're going to do now, we're going to subtract from this time minus equal minus equal times
delta time and times delta time.
We do know basically this means change in frame rate.
So this time set time.
So once we're going to call this a shake function.
So this set time just going to be 0.15.
Or with this much of duration and duration we pass 0.15 as using, so this time becoming 1.5 sorry 0.15.
So and we do know that 0.15 is greater than 0F0F, then what we're doing, we just subtracting from
this check time every single frame because we're doing inside this update function as you can notice.
So somehow sometime this check time basically after some amount of time, this check time just gonna
be zero.
Because what we're doing we just subtracting every single frame.
So sometimes uh, at somewhere this time it's just going to hit zero.
And when it, when it's gonna hit zero then we will stop checking.
So we also want to check if so we're going to use this if condition.
Let's make first bracket and some curly brackets.
And what we want to check over here.
So we want to check if our check time is now already zero or lesser than zero.
And we do know that at some point this check time just going to hit zero because we every single frame,
as you can notice right over here we're just subtracting some amount from this time.
So this time I'm just going to hit zero after some period of time.
So we will check if our set time is now lesser than or equal to zero.
If that is the case means duration is over.
Basically that time period is over and now we need to stop checking.
Basically, we need to set that amplitude gain parameter to zero.
And as you saw, if we set back this parameter to zero it's going to stop checking.
So firstly we need we need to get this animation basic multi-channel Perlin component.
And after that we can access this amplitude gain.
And we're gonna set back to zero.
So now what we're going to do we're going to copy this line of code from here by Ctrl C.
And we're gonna paste it by Ctrl V, basically what we're doing.
We just taking that cinemachine basic multi-channel perlin.
Basically this one, this animation basic multi-channel Perlin component.
And we storing into this Perlin this variable or in this container as you can see, which is Cinemachine
basic multi-channel Perlin.
And we're storing inside this Perlin.
And then we accessing the amplitude gain.
Now we're gonna not gonna set equal to intensity.
Instead we're gonna set it to set it to zero.
If as you can see.
So firstly firstly we subtracting from this set time.
And once our set time hit zero and we do know it's somehow it's just going to hit zero.
And if it's hit zero as you can see, if it's less than or equal to zero, then we're just going over
to Cinemachine basic multi-channel Perlin component and then accessing the amplitude gain basically
this one and we setting it to zero.
As you can notice right over here.
And we now if we set back amplitude gain to zero, we do know that our camera just gonna stop shaking.
And now what we have to do.
We're going to press down Ctrl s on our keyboard in order to save our script.
But before I do I'm just gonna add in.
So this parameter that we created right over here.
Take time to just keep track of when we started shaking.
And you you can use the start function.
And in this start of the game you can just set the shake time something like zero.
And whenever we're going to call this check function we do know that a then at that moment this set
time not going to be zero.
It's going to be with duration or uh, or what amount we just pass as a first argument with this duration.
Basically that is the thing.
So I hope you get the idea.
And now we're going to press down Ctrl s to save our script.
So we're just subtracting from this set time minus equal time dot delta time as you can see.
So at some point this time just going to hit zero.
And once it hits zero then we just uh going over to Cinemachine basic multi-channel bonding component
and accessing the amplitude gain.
And we setting equal to zero.
And we do know once we're gonna set it back to zero if our camera will stop shaking.
So just wait a moment to compile our script.
And once the compilation is done, we can just go ahead and then play.
Let's click on this play button in the top in order to play our game.
And you will notice that our camera just gonna shake with that much of duration as we pass as first
argument.
And the second thing just gonna be the intensity and intensity we pass to.
So amplitude just just gonna be to.
And basically we do know that.
So after the the duration is over, our camera just going to stop shaking.
And indeed we can notice that camera is stop shaking once our player uh player uh once the duration
is over or that shake time is zero.
So if so, if I get out of play mode and then select the cinemachine camera so we're not able to notice
our private parameter.
And we do know that how we can access that.
For that we will go to Inspector in the top right.
We're going to click on this three dot and let's choose this debug.
And we can notice by now the set time is zero by default.
And so once we start our game and take damage from this enemy one game object, you will see this check
time just going to be 0.15.
Because as a depression we pass 0.15.
As you seen inside that script inside our player uh, C-sharp class.
So you will see.
So let's get closer to this enemy one game object and we can see indeed, uh, the same time just becoming
some numbers.
And we can see the check time is now -0.0.
Uh, basically it's zero.
So if it's zero, then what we're doing, we just say am accessing the amplitude gain, and then we
just setting equal to zero.
As you can notice right over here.
So I hope you get the idea.
And we're going to get out of play mode.
And we also want to call that function, uh, once our uh, this enemy one enemy, one game object takes
damage.
But before I do, I just want to go over to normal mode.
To do that, let's go over to Inspector Top right.
Let's click on it and let's click on this normal.
And we're going to open up our anime one script as well as anime two script in order to call the function
which is sick.
And we do know that check function is just going to check our camera as an image in camera.
So I'm going to select my anime one game object.
Let's go over to Inspector and let's open up this anime one script by double clicking on it.
And let's open that up.
And over here.
As you can notice, our anime is taking damage.
So we're going to call the function.
So let's call it here.
And let's call the function here.
And to call the function we're going to use find or find any object by type.
So we're going to use this find any object by type.
And then make this arrows.
And inside these arrows you need to pass the script name that you're looking for.
So we're looking for our camera check script.
So let's go there and let's make this first bracket.
And then we're going to call the save function.
So let's call it.
So let's type this dot and then check function.
And let's make first bracket.
And then close that up with semicolon.
And this function as you can notice as a first argument it's going to take the duration or the time
frame that we are with how much amount of time we want to check our camera.
So I'm going to give a pretty smaller number, something like .0.12, something like that.
And the second thing comma, it's going to take the intensity or the amplitude gain.
You can simply say so I want to set the intensity something around a little bit higher, something like
four F.
And now as you can see it's fine.
So whenever anyone just going to take damage, it will check our camera.
With this much of duration and with this much of intensity.
And now we're going to press down Ctrl S to save our script.
And now let's move over to unity.
And you will notice that.
So as soon as this anime one just going to take damage.
The camera will start shaking with that much of intensity, which we pass as our second argument and
first argument.
We pass our duration, so we just give arguments and we can.
Just later, if I move over to my camera shake script.
So we're just giving this argument and we can later on we can just place these arguments with real number.
Basically that is the thing.
So that's why it's taking argument.
And you can just pass the argument and later on you can just replace that with real numbers.
And now we're going to move over to Unity Editor.
And let's click on this play button in the top.
And let's attack this anime one game object.
And as soon as this anime one game object start taking damage, it will shake with that much of duration
as we passed as a first argument with that, with our intensity.
So let's uh, we see we can notice, our player is taking damage.
And now if we attack on this enemy one game object, we can see enemy just taking damage and camera
starts shaking.
And after shaking a little bit or basically that much of duration camera just stop shaking.
Why?
Because if I select my cinemachine camera in the hierarchy, then go over to Inspector, then Cinemachine
basic multi-channel Perlin component.
In the bottom we can see amplitude gain.
We just set back to zero, but as soon as we attack we just set to four.
And after setting four and once the duration is over, we can see it's became zero.
So I hope you get the idea that how you can do it, or how you can just shake your camera and you can
call the shake function wherever you need.
So I'm going to get out of play mode so you can call the function in inside that enemy to game object
as well, where enemy two GameObject just taking damage so you can call the function there as well.
So I hope you get the idea that how you can do this.