Game Development Reference
In-Depth Information
Lines 09 and 12. The public class members Strength and BobAmount define the
amplitude and frequency of the sine wave, respectively. They can be used to
control and customize the sine wave to affect head bobbing.
Line 15. The HeadY value will represent the camera Y position in the scene when
at rest . During movement, the camera Y position will be offset from this center
as a result of the sine wave.
Line 34. I've added a Boolean member to the GameManager InputAllowed ,
which can be set to false to disable user input. All input-reading classes should
verify this variable before processing input.
Lines 37 and 38. Together these lines will result in two variables, either 1 or 0,
indicating whether the user is moving the First Person Controller (1=Yes, 0=No).
If yes, then we'll need to apply head-bob movement; otherwise, the camera
should be at its default Y position.
Line 44. The ElapsedTime variable keeps track of the total elapsed time in
seconds since the Player started moving. This value will be used for calculating
the sine wave.
Line 47. Finally, the amount of Y offset for the camera on the current frame is
calculated using the Mathf.Sin function, along with the amplitude and frequency
parameters.
Now add the HeadBob.cs script to the First Person Controllers , and then take them for a test run
in-game. Your First Person Controller should now display a distinct head bob when walking. The final
details can be tweaked using the class public member variables from the Object Inspector
(see Figure 5-12 ).
Figure 5-12. Assigning the HeadBob script to First Person Controllers
Search WWH ::




Custom Search