Game Development Reference
In-Depth Information
the Cash Power-Up moves up and down at a constant speed and in a straight line. Head motion,
however, isn't really like this: its movement upward is slightly slower than its movement downward.
There's a smooth ease-in and ease-out motion that unravels with our heads whenever we walk
around. In fact, if this motion could be plotted on a line graph, in terms of the camera Y position
over time, it would probably create a curve and not a straight line. This curve, when repeated in
sequence, would look like a wave . In real life, our head motion may not really form a completely
smooth and repeating curve, but it could be approximated believably in a video game by using such
a curve. One type of curve that creates repeated oscillation is a sine wave (see Figure 5-11 ).
Figure 5-11. Sine waves are useful for creating smooth oscillations in motion
Mathematically, there are two main parts to a sine wave—at least, the two parts of interest to us in
creating head bobbing; these are the frequency and the amplitude of the wave (see Figure 5-11 ).
The amplitude refers to the tallness of the wave—it represents how strong the head bob should
be. Higher amplitude values will produce higher and lower head bobs—more extreme offsets from
a default center. The frequency refers to the size or horizontal length of one complete wave cycle
(known as a period ). The higher the frequency, the more wibbly-wobbly the wave is! In other words,
higher frequencies will make the head bob happen more often and quickly. The general formula for
producing a sine wave is sin(angle × frequency) × amplitude . If frequency is 1, then a range of 0-360
degrees for angle will produce one complete period for the wave. Values outside this range (higher or
lower) will simply produce repetitions of the same wave—so you never need to worry about clamping
between 0-360 for sine waves.
Now let's code a C# class that can be attached to the First Person Controller to add head-bob
motion. I'll call this class HeadBob.cs (see Listing 5-2; comments follow).
Listing 5-2. HeadBob.cs: Script to Add Head-Bob Motion to Camera Through Sine Waves
01 //------------------------------------------------
02 //Class to make first person camera bob gently up and down while walking
03 using UnityEngine;
04 using System.Collections;
05 //------------------------------------------------
 
Search WWH ::




Custom Search