Advertisement
Guest User

Untitled

a guest
Apr 11th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3.  
  4. public class CoroutineExample : MonoBehaviour
  5. {
  6.     private Transform _transform;
  7.     private readonly float _limit = 5f;
  8.    
  9.     private IEnumerator _coroutine;
  10.  
  11.     private void Awake() => _transform = GetComponent<Transform>();
  12.  
  13.     private void Start()
  14.     {
  15.         _coroutine = CoroutineWithCondition(_transform, _limit);
  16.  
  17.         if (_transform != null) StartCoroutine(_coroutine);
  18.     }
  19.  
  20.     private IEnumerator CoroutineWithCondition(Transform t, float limit)
  21.     {
  22.         while (t.position.x < limit)
  23.         {
  24.             t.Translate(Vector3.right * Time.deltaTime, Space.World);
  25.             yield return null;
  26.         }
  27.  
  28.         Debug.Log("Transform has reached the limit");
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement