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 sign wave, which will be a float called bob..

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

0:19
down which will be a float called bob amplitude.

0:24
Let's type that out.

0:25
So just below our serialized fields we'll add a few more.

0:31
So this serialized field will be a private float called bob period.

0:37
And we'll set that to a default value of 2 float, then we'll add another

0:43
serialized field and we'll set that to a private float called Bob.

0:50
Amplitude also set to float.

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

0:59
that will represent the zero value from where the sign wave and

1:03
our camera movement beings.

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.

1:15
We don't need to see them in the inspector, so we'll just type private

1:19
float startY, 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, we need to record,

1:40
the staring y value of the camera, before it starts moving.

1:44
This should be the local position value, as opposed to the regular position value,

1:49
which is the world position.

1:51
Storing, the local position, for these values,

1:54
will make the script reuseable, if it's parented to other game objects.

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

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

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

2:13
We don't want the other positions.

2:16
And to store that, we'll type transform.localPosition,

2:21
and we want the y value of that.

2:24
Next, in the update method, we need to first calculate

2:29
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 want to interfere.

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

2:47
So I'll scroll down a bit here.

2:49
And here we want to update the vertical camera bob.

2:56
And to do that, we need to type out our sine wave equation.

2:59
So bobDistance = Mathf.Sin.

3:08
And then to calculate theta, we need time dot time sense level load

3:18
and divide that by the period, which is bob period.

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

3:28
In our sine wave equation we're using the sign method

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

3:39
Mathematics, trigonometric functions like sign cosine and

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

3:48
For the time value, we're using time dot

3:52
time sense level low which will give us a float value

3:56
representing the amount of time that's elapsed since the since loaded.

4:02
Now let's take the bobDistance resolved 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.

4:20
We'll adjust them in the next line of code.

4:23
So just after our sign wave equation, still updating the camera bob, and

4:29
we'll do that by setting transform.position = A new Vector3

4:39
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, or

5:04
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, and then

5:25
the result of the sine wave formula will add and 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 at 14 plus 2, or 16, and 14 minus 2, or 12.

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

5:49
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 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 it orbit slower.

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

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

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

6:53
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

7:06
very short and make the camera bob period 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:22
The bob amplitude, 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 six,

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

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

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