using System.Collections; using System.Threading; using UnityEngine; namespace Animations { public class ThreadTween : MonoBehaviour { private void Start() { StartCoroutine(StartThread()); } private IEnumerator StartThread() { yield return new WaitForSeconds(2.0f); // CAUTION: you can't access Unity Objects, e.g. Transforms, from another thread // than the main thread // CAUTION: never put the main thread to sleep, e.g. Thread.Sleep() or Thread.Join() Vector3 position = transform.position; Vector3 forward = transform.forward; Vector3 localScale = transform.localScale; Thread thread = new Thread(() => { float time = 0.0f; while (time < 1.0f) { position = Vector3.Lerp(Vector3.zero, forward * 5.0f, time); localScale = Vector3.Lerp(Vector3.one, Vector3.one * 2.0f, time); Thread.Sleep(16); // time += Time.deltaTime; // Time is not available in a thread time += 0.016f; } }); thread.Start(); thread.Join(); transform.position = position; transform.localScale = localScale; } } }