1 using System;
2 using
System.Collections;
3 using
UnityEngine;
4
5 namespace
UnityStandardAssets.Utility
6 {
7     
[Serializable]
8     
public class FOVKick
9     {
10         
public Camera Camera; // optional camera setup, if null the main camera will be used
11         [HideInInspector]
public float originalFov; // the original fov
12         
public float FOVIncrease = 3f; // the amount the field of view increases when going into a run
13         
public float TimeToIncrease = 1f; // the amount of time the field of view will increase over
14         
public float TimeToDecrease = 1f; // the amount of time the field of view will take to return to its original size
15         
public AnimationCurve IncreaseCurve;
16
17
18         
public void Setup(Camera camera)
19         {
20             CheckStatus(camera);
21
22             Camera = camera;
23             originalFov = camera.fieldOfView;
24         }
25
26
27         
private void CheckStatus(Camera camera)
28         {
29             
if (camera == null)
30             {
31                 
throw new Exception("FOVKick camera is null, please supply the camera to the constructor");
32             }
33
34             
if (IncreaseCurve == null)
35             {
36                 
throw new Exception(
37                     
"FOVKick Increase curve is null, please define the curve for the field of view kicks");
38             }
39         }
40
41
42         
public void ChangeCamera(Camera camera)
43         {
44             Camera = camera;
45         }
46
47
48         
public IEnumerator FOVKickUp()
49         {
50             
float t = Mathf.Abs((Camera.fieldOfView - originalFov)/FOVIncrease);
51             
while (t < TimeToIncrease)
52             {
53                 Camera.fieldOfView = originalFov + (IncreaseCurve.Evaluate(t/TimeToIncrease)*FOVIncrease);
54                 t += Time.deltaTime;
55                 
yield return new WaitForEndOfFrame();
56             }
57         }
58
59
60         
public IEnumerator FOVKickDown()
61         {
62             
float t = Mathf.Abs((Camera.fieldOfView - originalFov)/FOVIncrease);
63             
while (t > 0)
64             {
65                 Camera.fieldOfView = originalFov + (IncreaseCurve.Evaluate(t/TimeToDecrease)*FOVIncrease);
66                 t -= Time.deltaTime;
67                 
yield return new WaitForEndOfFrame();
68             }
69             
//make sure that fov returns to the original size
70             Camera.fieldOfView = originalFov;
71         }
72     }
73 }