Coroutines and Power Ups

In this post, I’ll briefly talk about the use of coroutines and it’s application in power up mechanics.

Unity defines coroutines as follows;

A coroutine is like a function that has the ability to pause execution and return control to Unity but then to continue where it left off on the following frame.

This comes very handy when using a constant timed event such as Power Ups.Power Ups are temporary gameplay enhancements that should change the “world” or the character in a certain time interval. That said, we first need to store the initial world settings before altering the variables. Then, we invoke the coroutine which waits about the duration of the power up and then reset the initial variables.

void ActivateTimeWarp(){
gameObject.GetComponent<PlatformerCharacter2D> ().m_MaxSpeed /= 2;
platformSpawner.GetComponent<spawnScript> ().spawnMin *= 2;
gameObject.GetComponent<PlatformerCharacter2D> ().m_JumpForce *=1.3f;
ParticleSystem tp = (ParticleSystem)Instantiate(TimeWarpParticle, transform.GetChild(0).position, Quaternion.identity);
tp.transform.parent = transform;
Snow.playbackSpeed /= 3;
StartCoroutine (DeactivateTimeWarp (tp));


IEnumerator DeactivateTimeWarp(ParticleSystem tp){
yield return new WaitForSeconds(15);
Destroy (tp);
gameObject.GetComponent<PlatformerCharacter2D> ().m_MaxSpeed *= 2;
platformSpawner.GetComponent<spawnScript> ().spawnMin /= 2;
gameObject.GetComponent<PlatformerCharacter2D> ().m_JumpForce /=1.3f;
Snow.playbackSpeed *= 3;

It should be noted that, coroutines are still operating in the main thread and treating them as they were multi-thread solutions can be problematic. In game development, anything can happen at any time and there isn’t really a way to estimate the wide array of possibilities that the user can do. Therefore, using many coroutines are “dangerous”. In addition to that, irresponsible usages of coroutines(obvious examples such as having a constant loop, update etc.) have a direct negative impact of the devices battery (especially if you are developing for mobile devices), so use it should be used with caution.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s