using System.Collections; using System.Collections.Generic; using UnityEngine; public class FollowTarget : MonoBehaviour { public Transform target; public Vector3 offset; public float smoothTime = 0.1f; private float velocity; private Transform myTransform; // Use this for initialization void Awake () { myTransform = GetComponent(); } // Update is called once per frame void Update () { if (target == null) return; Vector3 pos = target.position + offset; Vector3 now = myTransform.position; if (pos.Equals(now)) return; float distance = Vector3.Distance(pos, now); float v = Mathf.SmoothDamp(0, distance, ref velocity, smoothTime); myTransform.position = Vector3.MoveTowards(now, pos, v); } }