AbstractTargetFollower.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using UnityEngine;
  3. #pragma warning disable 649
  4. namespace UnityStandardAssets.Cameras
  5. {
  6. public abstract class AbstractTargetFollower : MonoBehaviour
  7. {
  8. public enum UpdateType // The available methods of updating are:
  9. {
  10. FixedUpdate, // Update in FixedUpdate (for tracking rigidbodies).
  11. LateUpdate, // Update in LateUpdate. (for tracking objects that are moved in Update)
  12. ManualUpdate, // user must call to update camera
  13. }
  14. [SerializeField] protected Transform m_Target; // The target object to follow
  15. [SerializeField] private bool m_AutoTargetPlayer = true; // Whether the rig should automatically target the player.
  16. [SerializeField] private UpdateType m_UpdateType; // stores the selected update type
  17. protected Rigidbody targetRigidbody;
  18. protected virtual void Start()
  19. {
  20. // if auto targeting is used, find the object tagged "Player"
  21. // any class inheriting from this should call base.Start() to perform this action!
  22. if (m_AutoTargetPlayer)
  23. {
  24. FindAndTargetPlayer();
  25. }
  26. if (m_Target == null) return;
  27. targetRigidbody = m_Target.GetComponent<Rigidbody>();
  28. }
  29. private void FixedUpdate()
  30. {
  31. // we update from here if updatetype is set to Fixed, or in auto mode,
  32. // if the target has a rigidbody, and isn't kinematic.
  33. if (m_AutoTargetPlayer && (m_Target == null || !m_Target.gameObject.activeSelf))
  34. {
  35. FindAndTargetPlayer();
  36. }
  37. if (m_UpdateType == UpdateType.FixedUpdate)
  38. {
  39. FollowTarget(Time.deltaTime);
  40. }
  41. }
  42. private void LateUpdate()
  43. {
  44. // we update from here if updatetype is set to Late, or in auto mode,
  45. // if the target does not have a rigidbody, or - does have a rigidbody but is set to kinematic.
  46. if (m_AutoTargetPlayer && (m_Target == null || !m_Target.gameObject.activeSelf))
  47. {
  48. FindAndTargetPlayer();
  49. }
  50. if (m_UpdateType == UpdateType.LateUpdate)
  51. {
  52. FollowTarget(Time.deltaTime);
  53. }
  54. }
  55. public void ManualUpdate()
  56. {
  57. // we update from here if updatetype is set to Late, or in auto mode,
  58. // if the target does not have a rigidbody, or - does have a rigidbody but is set to kinematic.
  59. if (m_AutoTargetPlayer && (m_Target == null || !m_Target.gameObject.activeSelf))
  60. {
  61. FindAndTargetPlayer();
  62. }
  63. if (m_UpdateType == UpdateType.ManualUpdate)
  64. {
  65. FollowTarget(Time.deltaTime);
  66. }
  67. }
  68. protected abstract void FollowTarget(float deltaTime);
  69. public void FindAndTargetPlayer()
  70. {
  71. // auto target an object tagged player, if no target has been assigned
  72. var targetObj = GameObject.FindGameObjectWithTag("Player");
  73. if (targetObj)
  74. {
  75. SetTarget(targetObj.transform);
  76. }
  77. }
  78. public virtual void SetTarget(Transform newTransform)
  79. {
  80. m_Target = newTransform;
  81. }
  82. public Transform Target
  83. {
  84. get { return m_Target; }
  85. }
  86. }
  87. }