Lerp vs Coroutine?

Trouble lerping values during a coroutine, I’m able to do so within the update, where have I gone wrong?

IEnumerator colorSplashTog()
{
	//Fade From Black
		colorRef_A = Mathf.Lerp (colorRef_A, 0f, Time.smoothDeltaTime);
		colorSplash.color = new Color (0, 0, 0, colorRef_A);
		yield return new WaitForSeconds(3f);
}

I feel I’ve followed the same code structure as the example provided here:

CoroutinesExample : line 20
http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines

The above code prints out time values at request as expected, it just seems to skip by line 4 and continue on normally?

You didn’t tell it to loop, so next frame it will start where you yielded and just finish the function.

The example contains a loop. Yours doesn’t.

Perfect, thanks…
That was a stupid question I’ll admit, not sure how I overlooked that…

Working now, on to the next

You also shouldn’t use a time delta in the third argument of lerp, it’s meant to be an interpolation factor.

As angrypenguin pointed out, you shouldn’t be using time delta directly if you’re looking for a linear interp:

IEnumerator colorSplashTog(float duration)
{
    //Fade From Black
        float t = 0;
        Color fr = Color.Black;
        Color to = Color.Clear;
        while(t < 1) {
                t += Time.smoothDeltaTime / duration;
                colorSplash.color = Color.Lerp (fr, to, t);
                yield return null;
        }
}

As a bonus, you could also stick the fr and to parameters into the method signature itself, so it’s more reusable.

IEnumerator colorSplashTog(Color fr, Color to, float duration)
1 Like

Thank you guys for the feedback, I am looking for a non-linear progression with this though. I’ve since taken the time to build a cut off point so it doesn’t continuously interpolate :slight_smile: This has been helpful, as I didn’t fully understand this before and now have a much better grasp on it.

I am currently trying to slim the code down though,

/*
	satCamPivot.localEulerAngles = new Vector3
		(
			Mathf.LerpAngle(satCamPivot.localEulerAngles.x, 294f, waitTimeCur),
			Mathf.LerpAngle(satCamPivot.localEulerAngles.y, 90f, waitTimeCur),
			Mathf.LerpAngle(satCamPivot.localEulerAngles.z, 165f, waitTimeCur)
		);
*/
	satCamPivot.localEulerAngles = Mathf.LerpAngle (satCamPivot.localEulerAngles, new Vector3 (294f, 90f, 165f), waitTimeCur);

The commented code seems to be working, although it doesn’t always rotate properly…Not sure why this is yet.
The uncommented code obviously doesn’t work because the vector3 isn’t able to be converted to a float…this makes sense to me but I’m wondering if there is another way to write the commented code in a shorter way? The game allows several different rotations controlled by buttons (EX: You click a button and it rotates to the position). If I were able to get the broken code working it would decrease this script by a significant amount…

Any thoughts on this?

Mathf.LerpAngle

should be

Vector3.Lerp

Only got one error this time, so we’re getting closer :slight_smile:

satCamPivot.localEulerAngles = new Vector3
	(Vector3.Lerp (satCamPivot.localEulerAngles, new Vector3 (270f, 85f, 50f), waitTimeCur));

ERROR:
CS1729: The type UnityEngine.Vector3' does not contain a constructor that takes 1’ arguments

nvm got it,
removed 1st new vector 3

thanks a lot LoTekk