SHARE
TWEET

Untitled

1max1 Feb 14th, 2020 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Obj : MonoBehaviour
  2. {
  3.     CamMove camMove = null;
  4.     void Start() => camMove = FindObjectOfType<CamMove>();
  5.     void OnMouseDown() => camMove.target = transform;
  6. }
  7.  
  8. class CamMove : MonoBehaviour
  9. {
  10.     // Сфера
  11.     [SerializeField]
  12.     Transform mainObj;
  13.  
  14.     // Дистанция
  15.     [SerializeField]
  16.     float distance = 10.0f;
  17.  
  18.     Coroutine coroutine;
  19.  
  20.     public Transform target
  21.     {
  22.         set
  23.         {
  24.             if (coroutine != null)
  25.             {
  26.                 StopCoroutine(coroutine);
  27.             }
  28.  
  29.             coroutine = StartCoroutine(c_Move(value));
  30.         }
  31.     }
  32.  
  33.     IEnumerator c_Move(Transform target)
  34.     {
  35.         var p = mainObj.position + (target.position - mainObj.position).normalized * distance;
  36.  
  37.         while (true)
  38.         {
  39.             var q = Quaternion.LookRotation(target.position - transform.position);
  40.             transform.rotation = Quaternion.Lerp(transform.rotation, q, 5.0f * Time.deltaTime);
  41.             transform.position = Vector3.Lerp(transform.position, p, 3.0f * Time.deltaTime);
  42.  
  43.             if ((transform.position - p).sqrMagnitude < 1.0f)
  44.             {
  45.                 coroutine = null;
  46.  
  47.                 yield break;
  48.             }
  49.  
  50.             yield return null;
  51.         }
  52.     }
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top