Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
The orbit camera would be a little better if the camera moved up and down. In this video, we'll add a subtle camera "bob" using a sine wave.
Unity Documentation

Transform.localPosition  The
localPosition
variable on theTransform
class is the position of a transform relative to the parent transform. 
Mathf.Sin  The
Sin()
method on theMathf
returns the sine of an angle in radians.
Resources
 Cameras in Unity  Project Files  This is a zip file that contains the Unity project for this course. Download the file, unzip it, and then in your file browser (Finder on OS X and Exporer on Windows) navigate to Cameras in Unity > Assets > _Scenes. Finally, choose the scene you'd like to open.

0:00
We need several more variables for the camera bob to work.

0:04
Let's jump back into our script.

0:06
If you're still in Unity, switch over to the script.

0:10
First, we'll need the period of the sine wave,

0:13
which will be a float called bob period.

0:17
Then we'll need an amplitude for the camera to bob up and

0:19
down, which will be a float, called bobAmplitude.

0:24
Set that out.

0:25
So, just below are serialized fields, we'll add a few more, so

0:31
this serialized field will be a private float called, bobPeriod.

0:37
And we'll set that to a default value of two float.

0:41
Then we'll add another serialized field, and

0:46
we'll set that to a private float called, bobAmplitude, also set two float.

0:54
We also need to store the starting Y position of the camera as a float,

0:58
because that will represent the zero value from where the sine wave and

1:03
our camera movement begins.

1:05
We'll add the distance of the sine wave equation to this value,

1:09
which we'll store in a float, called bobDistance.

1:12
So these variables don't need to be serialized, we don't need to see

1:18
them in the inspector, so we'll just type, private float startY.

1:23
And that will be our starting Y position of the camera.

1:26
And then private float bobDistance,

1:31
which will store the result of our sine wave equation.

1:35
That covers our variables.

1:36
Now, in the Start method,

1:39
we need to record the starting line value of the camera before it starts moving.

1:44
This should be the local position value,

1:46
as oppose to the regular position value, which is the world position.

1:51
Storing the local position for

1:53
these values will make the script reusable if it's parented to other game objects.

1:59
So, inside of the Start method, we'll type a comment.

2:04
I'll say, store the start position of the camera,

2:09
and we want to actually store the startY.

2:13
We don't need the other positions, and to store that we'll type,

2:18
transform.localPosition, and we want the y value of that.

2:24
Next, in the Update method,

2:27
we need to first calculate the camera bob value using the sine wave formula.

2:33
This should be done before the rotation is performed, because the rotation

2:39
will literally add it's own spin to the camera, and we don't wanna interfere.

2:44
We'll type this out, and then review the code.

2:47
So I'll scroll down a bit here, and here we want to update the vertical camera bob,

2:54
and to do that we need to type out our sine wave equation.

2:59
So bobDistance =

3:03
Mathf.Sin And

3:09
then to calculate data, we need Time.TimeSinceLevelLoad,

3:16
and divide that by the period, which is bobPeriod.

3:23
And then we need to multiply theta by the amplitude, which is bobAmplitude.

3:29
In our sine wave equation, we're using the Sin method,

3:34
which is part of the math functions, or Mathf class.

3:39
In Mathematics, trigonometric functions like sine, cosine, and tangent,

3:43
are typically abbreviated to three letters as is the case here.

3:48
For the time value we're using, Time.timeSinceLevelLoad.

3:54
Which will give us a float value representing the amount of

3:58
time that's elapsed since the scene loaded.

4:02
Now, let's take the bobDistance result and

4:06
add it to the local y position of the transform.

4:11
The x and z values should use their same value from within the current frame,

4:17
because the camera rotation will adjust them in the next line of code.

4:23
So just after our sine wave equation, we're still updating the camera bob.

4:29
And we'll do that by setting,

4:32
transform.position equal to a new Vector3,

4:37
and in the Vector3 constructor method we'll type,

4:43
transform.localPosition.x, and that will be our x value.

4:51
And then we want to add the starting Y value to the bobDistance.

4:59
And then we want transform.localPosition.z,

5:06
for the z value.

5:08
Now, save the script and return to Unity.

5:13
Before I hit the play button, notice that the camera Y position is a value of 14.

5:20
If we wrote the script correctly it should start at this value.

5:24
And then the result of the sine wave formula, will add and

5:28
subtract the amplitude over time.

5:31
We've set the amplitude to a value of 2, so it should start at 14 and

5:37
then oscillate between 14 plus 2, or 16, and 14 minus 2 or 12.

5:43
When I hit the play button, watch how the camera bobs up and

5:48
down, and then also watch this y value.

5:54
In the scene view there's a nice camera bob, so let's watch that for a moment.

5:59
And then in the inspector, watch how the y value goes between 12 and 16.

6:08
It will get infinitely close to 16 without ever actually hitting

6:13
the number 16.0 before it turns back around.

6:16
Then it will also get infinitely close to 12, without actually hitting 12.0.

6:23
In the inspector, there's several values you can use to adjust the behavior.

6:30
Increasing the rotation speed will make the camera orbit faster.

6:37
Decreasing it will make the orbit slower.

6:43
And if it's a negative value you

6:45
can make the camera orbit in the opposite direction.

6:50
I'll leave it a value of 10.

6:52
The bob period will adjust how long it takes for

6:57
the camera to go between the two amplitude values.

7:01
Very low values, like 0.1, will make the camera very short.

7:08
And make the camera bobPeriod quick.

7:11
Longer periods, like three, will make the camera bob take longer.

7:18
I'll set this to a value of one for the moment.

7:23
The bobAmplitude will adjust how far up and

7:26
down the camera will move from the starting y position.

7:31
If we set this to a value of 6,

7:34
it will move further upwards and further downwards.

7:40
Now, stop the game, and save the scene.

7:46
That's it for the orbit camera.

7:48
In the next video, we'll use sine waves again, to write a tracking camera script.
You need to sign up for Treehouse in order to download course files.
Sign up