- Home /
Coroutines and Lerp - how to make them friends?
Hi there, so I've spent a bit of my time getting my head around coroutines which is a fantastic tool to have in your armoury. However, I'm confused by how to use a Vector3.Lerp within a coroutine - separately, I understand how they work, but I'm a little fuzzy as to how to make them work together. I've been pointed to a javascript piece of code here but this doesn't make things much clearer for me. As always, any help is truly appreciated.
Answer by syclamoth · Dec 06, 2011 at 09:57 AM
The two functions are completely orthogonal- there's no reason whatsoever why they can't work together!
In fact, they are particularly useful in conjunction. Here's a simple bit of code for making an object move to a new position over a period of n seconds.
function MoveToPosition(newPosition : Vector3, time : float)
{
var elapsedTime : float = 0;
var startingPos : Vector3 = transform.position;
while (elapsedTime < time)
{
transform.position = Vector3.Lerp(startingPos, newPosition, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield;
}
}
Simply call that function with StartCoroutine, and it'll all work!
StartCoroutine(MoveToPosition(new Vector3(0, 10, 0), 5));
I do like to point out that by using this code, the position where the object ends up may be inconsistent. For example, I needed my Gameobject to stop at exactly 1.12345 in the yPos. This code does not guarantee that since one possible reason is that elapsedTime < duration doesn't necessarily always break on exactly the same elapsedTime value. Just a heads up for future devs who run into this thread searching for answers as well :))
Did you try: At the end of path, just sync position?
Answer by ilezhnin · Nov 14, 2014 at 05:46 PM
And for C#
private IEnumerator FadeOut(float alphaStart, float alphaFinish, float time)
{
if (bgTexture == null)
yield return null;
float elapsedTime = 0;
bgTexture.alpha = alphaStart;
while (elapsedTime < time)
{
bgTexture.alpha = Mathf.Lerp(bgTexture.alpha, alphaFinish, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield return new WaitForEndOfFrame();
}
}
I noticed the WaitForEndOfFrame() as well. Is there an advantage of that over yield return null?
@Electric$$anonymous$$onk
They simply return at different points during a frame. WaitForEndOfFrame()
obviously returns at the end of the frame. Returning null will return during main coroutine updates (which I believe occurs between Update()
and LateUpdate()
, which also means it occurs before rendering.)
Also note that WaitForEndOfFrame()
is a class object, so caching it in a variable and reusing it will create slightly less overhead in performance-heavy operations.
I was placing the yeld return OUTSIDE the while, and It was behaving very weirdly. Now yous aved my life cause I burnt half afternoon trying to figure out the hell was going on. Can you explain why it needs to be inside the while, even further if it returns nothing?
Your answer
Follow this Question
Related Questions
Lerping inside Coroutine occurring too quickly 1 Answer
lerp in coroutine doesn't work correctly 0 Answers
restrict Lerp to only one axis? 1 Answer
Lerping multiple materials of a gameobject within a coroutine 0 Answers
Lerping blinks 1 Answer