协程:顾名思义就是在主程序运行的同时,开启另外一段逻辑处理,来协助主程序的执行,大多数情况下使用协程是为了延时调用某个函数、某块逻辑要一直循环判断执行这两种请况
public class CoroutinesExample : MonoBehaviour
{
public float smoothing = 1f;
public Transform target;
void Start ()
{
StartCoroutine(MyCoroutine(target));
// StopCoroutine("MyCoroutine", target);
}
IEnumerator MyCoroutine (Transform target)
{
while(Vector3.Distance(transform.position, target.position) > 0.05f)
{
transform.position = Vector3.Lerp(transform.position, target.position, smoothing * Time.deltaTime);
yield return null;
}
print("Reached the target.");
yield return new WaitForSeconds(3f);
print("MyCoroutine is now finished.");
}
}