| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- using System;
- using UnityEngine;
- #pragma warning disable 649
- namespace UnityStandardAssets.Cameras
- {
- public abstract class AbstractTargetFollower : MonoBehaviour
- {
- public enum UpdateType // The available methods of updating are:
- {
- FixedUpdate, // Update in FixedUpdate (for tracking rigidbodies).
- LateUpdate, // Update in LateUpdate. (for tracking objects that are moved in Update)
- ManualUpdate, // user must call to update camera
- }
- [SerializeField] protected Transform m_Target; // The target object to follow
- [SerializeField] private bool m_AutoTargetPlayer = true; // Whether the rig should automatically target the player.
- [SerializeField] private UpdateType m_UpdateType; // stores the selected update type
- protected Rigidbody targetRigidbody;
- protected virtual void Start()
- {
- // if auto targeting is used, find the object tagged "Player"
- // any class inheriting from this should call base.Start() to perform this action!
- if (m_AutoTargetPlayer)
- {
- FindAndTargetPlayer();
- }
- if (m_Target == null) return;
- targetRigidbody = m_Target.GetComponent<Rigidbody>();
- }
- private void FixedUpdate()
- {
- // we update from here if updatetype is set to Fixed, or in auto mode,
- // if the target has a rigidbody, and isn't kinematic.
- if (m_AutoTargetPlayer && (m_Target == null || !m_Target.gameObject.activeSelf))
- {
- FindAndTargetPlayer();
- }
- if (m_UpdateType == UpdateType.FixedUpdate)
- {
- FollowTarget(Time.deltaTime);
- }
- }
- private void LateUpdate()
- {
- // we update from here if updatetype is set to Late, or in auto mode,
- // if the target does not have a rigidbody, or - does have a rigidbody but is set to kinematic.
- if (m_AutoTargetPlayer && (m_Target == null || !m_Target.gameObject.activeSelf))
- {
- FindAndTargetPlayer();
- }
- if (m_UpdateType == UpdateType.LateUpdate)
- {
- FollowTarget(Time.deltaTime);
- }
- }
- public void ManualUpdate()
- {
- // we update from here if updatetype is set to Late, or in auto mode,
- // if the target does not have a rigidbody, or - does have a rigidbody but is set to kinematic.
- if (m_AutoTargetPlayer && (m_Target == null || !m_Target.gameObject.activeSelf))
- {
- FindAndTargetPlayer();
- }
- if (m_UpdateType == UpdateType.ManualUpdate)
- {
- FollowTarget(Time.deltaTime);
- }
- }
- protected abstract void FollowTarget(float deltaTime);
- public void FindAndTargetPlayer()
- {
- // auto target an object tagged player, if no target has been assigned
- var targetObj = GameObject.FindGameObjectWithTag("Player");
- if (targetObj)
- {
- SetTarget(targetObj.transform);
- }
- }
- public virtual void SetTarget(Transform newTransform)
- {
- m_Target = newTransform;
- }
- public Transform Target
- {
- get { return m_Target; }
- }
- }
- }
|