You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.3 KiB

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