TargetFieldOfView.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.Cameras
  4. {
  5. public class TargetFieldOfView : AbstractTargetFollower
  6. {
  7. // This script is primarily designed to be used with the "LookAtTarget" script to enable a
  8. // CCTV style camera looking at a target to also adjust its field of view (zoom) to fit the
  9. // target (so that it zooms in as the target becomes further away).
  10. // When used with a follow cam, it will automatically use the same target.
  11. [SerializeField] private float m_FovAdjustTime = 1; // the time taken to adjust the current FOV to the desired target FOV amount.
  12. [SerializeField] private float m_ZoomAmountMultiplier = 2; // a multiplier for the FOV amount. The default of 2 makes the field of view twice as wide as required to fit the target.
  13. [SerializeField] private bool m_IncludeEffectsInSize = false; // changing this only takes effect on startup, or when new target is assigned.
  14. private float m_BoundSize;
  15. private float m_FovAdjustVelocity;
  16. private Camera m_Cam;
  17. private Transform m_LastTarget;
  18. // Use this for initialization
  19. protected override void Start()
  20. {
  21. base.Start();
  22. m_BoundSize = MaxBoundsExtent(m_Target, m_IncludeEffectsInSize);
  23. // get a reference to the actual camera component:
  24. m_Cam = GetComponentInChildren<Camera>();
  25. }
  26. protected override void FollowTarget(float deltaTime)
  27. {
  28. // calculate the correct field of view to fit the bounds size at the current distance
  29. float dist = (m_Target.position - transform.position).magnitude;
  30. float requiredFOV = Mathf.Atan2(m_BoundSize, dist)*Mathf.Rad2Deg*m_ZoomAmountMultiplier;
  31. m_Cam.fieldOfView = Mathf.SmoothDamp(m_Cam.fieldOfView, requiredFOV, ref m_FovAdjustVelocity, m_FovAdjustTime);
  32. }
  33. public override void SetTarget(Transform newTransform)
  34. {
  35. base.SetTarget(newTransform);
  36. m_BoundSize = MaxBoundsExtent(newTransform, m_IncludeEffectsInSize);
  37. }
  38. public static float MaxBoundsExtent(Transform obj, bool includeEffects)
  39. {
  40. // get the maximum bounds extent of object, including all child renderers,
  41. // but excluding particles and trails, for FOV zooming effect.
  42. var renderers = obj.GetComponentsInChildren<Renderer>();
  43. Bounds bounds = new Bounds();
  44. bool initBounds = false;
  45. foreach (Renderer r in renderers)
  46. {
  47. if (!((r is TrailRenderer) || (r is ParticleSystemRenderer)))
  48. {
  49. if (!initBounds)
  50. {
  51. initBounds = true;
  52. bounds = r.bounds;
  53. }
  54. else
  55. {
  56. bounds.Encapsulate(r.bounds);
  57. }
  58. }
  59. }
  60. float max = Mathf.Max(bounds.extents.x, bounds.extents.y, bounds.extents.z);
  61. return max;
  62. }
  63. }
  64. }