I have a Prefab that gets instantiated everytime a ray (casted by a touch input) hit a gameobject that’s in the layer mask of the ray, that prefab has to move from the position (Screen.width / 2, 0) to (Screen.width / 2, Screen.Height / 2).
Originally I made it move by using a coroutine started at Start method, but after I read this https://unity3d.com/learn/tutorials/topics/performance-optimization/optimizing-scripts-unity-games?playlist=44069 I got that it would be better not to use too many coroutines to avoid GC issue.
And that is my case because I start a new coroutine for every prefab instantiated.
This is the code I used inside the coroutine to make prefab move:
var currentPos = transform.localPosition;
var t = 0f;
while (t < 1) {
t += Time.deltaTime / timeToMove;
transform.position = Vector3.Lerp (currentPos, endPos, t);
yield return null;
}
The question is: How can I use that code in Update function?