AbstractTargetFollower.cs 3.5 KB

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