Skip to content

Instantly share code, notes, and snippets.

@partiallyblind
Created February 18, 2022 11:27
Show Gist options
  • Save partiallyblind/a11edd6ed879d562220e6d6fac5c0702 to your computer and use it in GitHub Desktop.
Save partiallyblind/a11edd6ed879d562220e6d6fac5c0702 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class CoroutinesExample : MonoBehaviour
{
public float smoothing = 1f;
public Transform target;
void Start ()
{
StartCoroutine(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.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment