While working on Cows Control I've written a good number of coroutines specifically to smoothly transition a number or vector from one to another.
Seeing as there is almost always a better way when such situation occur, I came up with this neat class. It works really well for progress bars and the like and could easily be extended to include vectors.
The Code:
public static class CX_Utility
{
private static IEnumerator TransitionFloatOverTime(float from, float to, float overTime, System.Action floatUpdate)
{
float time = overTime;
float alpha = 0.0f;
while(time > 0.0f)
{
yield return new WaitForEndOfFrame();
time -= Time.deltaTime;
alpha = time / overTime;
floatUpdate(Mathf.SmoothStep(from, to, 1.0f - alpha));
}
floatUpdate(to);
}
public static void Transition(this MonoBehaviour monoBehaviour, float from, float to, float overTime, System.Action floatUpdate)
{
monoBehaviour.StartCoroutine(TransitionFloatOverTime(from, to, overTime, floatUpdate));
}}
Its Use:
this.Transition(startFloat, endFloat, transitionTime, delegate (float updatedFloat)
{
valueToUpdate = updatedFloat;
});