In this video, I'm going to run through the camera shake code in the robot scene.
Going back to the robot script, I added a temporary function
just for this video that will increase the camera shake intensity property
when pressing the spacebar or the enter key.
If I run the scene now and I press space, the camera shakes.
If I press a couple of times, it shakes more.
Now, we want to open the shaking camera 2D script.
I'm going to scroll back to the top to run you through it.
You will find more information in the comments,
but I'm going to give you the general flow of the code.
We control the shake through that shake intensity property.
You can increase it to make the camera start to shake.
As you can see,
it uses a setter function, which is at the bottom of the script.
I'm going to scroll down to set shake intensity.
This one limits the value between zero and one using the clamp function.
You first give it the value to limit,
and then the lower limit and the upper limit
that will prevent the intensity from going beyond one but also below zero.
Then when we change the shake intensity,
we check if the camera should be shaking.
To do that,
we just check if the shake intensity is not equal to zero.
Notice how we use a function called 'is equal approx'.
This one is a safe way to compare
two decimal values like our shake intensity.
The reason is that decimal numbers are not exactly precise on the computer.
When you compare them…
Well, you can use double equal, and it will often work.
Sometimes, it will fail unexpectedly.
You can use that function for safety.
Then we use that 'is shaking' value, it's going to be true or false,
and pass it to 'set physics process'.
This will turn off physics process
when the shake intensity value is equal to zero,
but it will keep it turned on while the value is greater than zero.
And so then, the shake animation happens in physics process.
We use physics process here although the camera is not a physics body.
We use it to synchronize the camera motion with the robot's motion.
The robot uses physics process to move,
so we use physics process to move the camera.
Otherwise, they will be desynchronized, and you will have some jitters.
Every physics frame,
we call 'set shake intensity' and we lower it.
We lower it by delta.
That will go through the setter and check if it has to turn off physics process.
Then we use that shake intensity value at the bottom here.
We'll start there.
Every frame, we offset the camera in a random direction,
and we multiply that by the shake intensity.
If the shake intensity is one, it's going to have the maximum amplitude.
As the value gets lower and lower, you will have less and less shake.
Now, we square the shake intensity so that when the value
of shake intensity gets close to one, you get a lot of shake.
But as it goes down, it goes down faster and faster.
This gives the shake a nicer feel.
That offset here,
you can see in the Inspector when you select the camera.
It's a property of the camera that's around the top,
and it's an offset from the camera's position in pixels.
You use it to offset the view.
It's really nice to make the camera
look around the character or to make it shake.
Let's head back to the shaking camera script
to see how we calculate the random direction.
We use something called a noise generator to get random values to offset the camera.
You learned about noise in the Random Rock series.
You coded a blue noise that would offset rocks inside grid cells.
Now, Godot has a built-in noise algorithm
that's going to generate random values, but we can get continuous series
that give us that nice smooth camera shake animation.
We use that here.
To calculate a random direction, we first calculate a random position
by calling the noise 'get noise 2D function'.
We try to sample a value that is random somewhere in the noise generator.
It's a position on the noise.
To do that, we use a time value every frame.
Why?
Because we can get continuous samples.
We can sample along a line as we'll see in a second.
Actually, let's look at this now.
I prepared a little visualization here to show you how we sample the noise.
Your noise generator, you can visualize it as an image like so
where the black parts represent values that are negative or close to zero,
and the bright parts represent values that are positive and closer to one.
Your noise is going to generate values between minus one and plus one.
By sampling a position somewhere on the noise,
we can get the value at that point,
and it's going to be a value between minus one and plus one.
If we get two of these, we almost have a direction vector.
We just need to normalize it so we don't get larger values in diagonals.
The way we sample with the time is like this.
We use time values to continuously move along a line,
and that line is going to wrap around the noise.
Where you see the yellow ball or the pink ball is going to be
the values that we get from the noise generator.
You can see that there are lots of bumps in the texture,
and these bumps are going to cause the shake
because they are going to make the camera move forward and backward,
but in different ways with different intensities.
Right now, you have lots of bumps
that would make the camera shake very, very fast,
but you can actually change that directly on the noise.
I have my noise as a texture here, and inside of that texture,
I have the actual noise that we use for the camera shake.
The thing we use to shake the camera is this noise resource.
You can see that it has some properties in there,
so you can change the period to remove some bumps
and make the camera shake more slowly.
You can change the persistence similarly to remove
some visual noise inside the texture and remove the high-frequency motions.
Finally, the lacunarity is going to change the scale of the high-frequency detail.
You control the persistence.
This is how we make the camera shake
and why we use time to sample the noise texture.
This is a lot to take in right now,
but you can find some more information in the comments
and you can change the values that you see here
to try to get a more intuitive sense for how all of this work.
The last thing I want to point out is how we get the time.
This function, OS.get ticks millisecond,
is going to return the number of milliseconds elapsed
since the start of the game, since the time you pressed the F6 key.
This gives you a value that continuously goes up.
This is very handy for anything that's time-dependent in your game.
We divide the value because those are milliseconds,
meaning that the number increases by 1,000 per second,
and dividing the value makes it go a bit more slowly,
which works a bit better with our noise sampling.
Once we calculated our random direction,
we multiply it by some maximum amplitude in pixel,
the maximum distance the camera can shake and by our shake intensity,
That moves the camera.
That's how the camera shake works.