Game Development Reference
In-Depth Information
06 public class HeadBob : MonoBehaviour
07 {
08 //Strength of head bob - amplitude of sine wave
09 public float Strength = 1.0f;
10
11 //Frequency of wave
12 public float BobAmount = 2.0f;
13
14 //Neutral head height position
15 public float HeadY = 1.0f;
16
17 //Cached transform
18 private Transform ThisTransform = null;
19
20 //Elapsed Time since movement
21 private float ElapsedTime = 0.0f;
22
23 //------------------------------------------------
24 void Start()
25 {
26 //Get transform
27 ThisTransform = transform;
28 }
29 //------------------------------------------------
30 // Update is called once per frame
31 void Update ()
32 {
33 //If input is not allowed, then exit
34 if(!GameManager.Instance.InputAllowed) return;
35
36 //Get player movement if input allowed
37 float horizontal = Mathf.Abs(Input.GetAxis("Horizontal"));
38 float vertical = Mathf.Abs(Input.GetAxis("Vertical"));
39
40 //Total movement
41 float TotalMovement = Mathf.Clamp(horizontal + vertical,0.0f,1.0f);
42
43 //Update elapsed time
44 ElapsedTime = (TotalMovement > 0.0f) ? ElapsedTime += Time.deltaTime : 0.0f;
45
46 //Y Offset for headbob
47 float YOffset = Mathf.Sin (ElapsedTime * BobAmount) * Strength;
48
49 //Create position
50 Vector3 PlayerPos = new Vector3(ThisTransform.position.x, HeadY + YOffset *
TotalMovement, ThisTransform.position.z);
51
52 //Update position
53 ThisTransform.position = PlayerPos;
54 }
55 //------------------------------------------------
56 }
Search WWH ::




Custom Search