Our focus is creating games that are fun and engaging. We strive to make game-play the highest priority in each of our titles.

Post news Report RSS Unity Calming the Coroutine

Tired of re-writing the same transition coroutine over and over again? Try this!

Posted by on

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.

Ability Transition

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;

});

Let me know what you think. Cheers!


Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: