Elledan3101

TargetFollowerPosition

Jan 21st, 2021
116
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     public class TargetFollowerPosition : MonoBehaviour
  2.     {
  3.         [SerializeField]
  4.         private string targetTag = "Player";
  5.         [SerializeField]
  6.         private Transform targetTransform;
  7.         public Transform TargetTrasfrom
  8.         {
  9.             get => targetTransform;
  10.             set => targetTransform = value;
  11.         }
  12.  
  13.         [Header("Offset settings")]
  14.         [SerializeField]
  15.         private Vector3 offsetPosition;
  16.  
  17.         [Header("Following in world settings")]
  18.         [SerializeField]
  19.         private bool followWorldX = true;
  20.         [SerializeField]
  21.         private bool followWorldY = true;
  22.         [SerializeField]
  23.         private bool followWorldZ = true;
  24.  
  25.         [Header("Smooth settings")]
  26.         [SerializeField]
  27.         private bool smoothUpdate = true;
  28.         [SerializeField]
  29.         private float speedUpdate = 1f;
  30.  
  31.         private Vector3 initWorldPosition;
  32.  
  33.         private void Awake()
  34.         {
  35.             initWorldPosition = transform.position;
  36.         }
  37.  
  38.         private IEnumerator Start()
  39.         {
  40.             if (targetTransform)
  41.                 yield break;
  42.  
  43.             while (true)
  44.             {
  45.                 GameObject foundGO = GameObject.FindGameObjectWithTag(targetTag);
  46.  
  47.                 if (foundGO)
  48.                 {
  49.                     targetTransform = foundGO.transform;
  50.                     break;
  51.                 }
  52.  
  53.                 yield return null;
  54.             }
  55.         }
  56.  
  57.         private void LateUpdate()
  58.         {
  59.             if (!targetTransform)
  60.                 return;
  61.  
  62.             UpdatePosition();
  63.         }
  64.  
  65.         private void UpdatePosition()
  66.         {
  67.             Vector3 newPosition = targetTransform.position + offsetPosition;
  68.  
  69.             if (!followWorldX)
  70.                 newPosition.x = initWorldPosition.x;
  71.  
  72.             if (!followWorldY)
  73.                 newPosition.y = initWorldPosition.y;
  74.  
  75.             if (!followWorldZ)
  76.                 newPosition.z = initWorldPosition.z;
  77.  
  78.             if (smoothUpdate)
  79.                 newPosition = Vector3.Lerp(transform.position, newPosition, speedUpdate * Time.deltaTime);
  80.  
  81.             transform.position = newPosition;
  82.         }
  83.     }
  84.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×