Unity3D: Intro to Coroutines while creating a Dash Ability

Have you ever wondered how to create a flexible sequence of events in Unity?

If we want to control the flow of an object spawning in a certain amount of time, we can do so with Coroutines. We can also control the speed of a player that can act as a Dash ability. There are many possible ways you can use Coroutines for a specific mechanic. It is all up to you as a programmer to have a specific use for it.

Description of a Coroutine

In Unity’s description of a coroutine:

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

Let me try to explain similarly:

A coroutine is a function that allows us to stop the execution of an event until a certain time frame has been met, then continue where it had left off.

Creating a Coroutine

When creating a coroutine, we must understand a couple of things first.

  • IEnumarator: We must use this data type when creating a function for the coroutine. It is a common practice to name your coroutine function with what it does and adding “Routine” in the end to state that it is a coroutine. An example is “private IEnumerator SpawnEnemyRoutine();
  • Yield: The yield keyword tells Unity that we want to wait an “x” amount of time, then continue with the execution of the code. Without it, the compiler will give us an error. Yield means to “submit” so we want to submit a time to pause the execution in a number of seconds. So we use “WaitForSeconds(the_amount_of_time_to_wait_here).”
  • StartCoroutine: The StartCoroutine() function is then called in the Start() method to initialize the coroutine. As you can see below, we have to call the StartCoroutine("coroutine function here") function and pass in the coroutine we created ass the argument. Notice that we pass in the function with “().”

Creating a Dash Ability using a Coroutine

Let us get started!

The objective is to create a Dash ability for the player. We hit the space key to dash, and it will increase the “speed” of the player, yield an amount of time for the dashing, then decrease the “speed” back to its default value.

I have the Player movement setup where he can move around with its animations working.

As you can see, I am pressing the space key to dash, but there is no logic for the ability yet. We want to Player to speed up when dashing. So this is when we use Coroutines.

Let’s set up our Input.

We need to have a variable to check and see if we are dashing. This will let us use the coroutine in the Update() function when we press the space key. I have the Input for dashing encapsulated in the Input script. We also check to see if the Player has enough fuel to dash. We then check to see if isDashing is false. If it is, then we can start the coroutine to dash.

Let’s set up our Coroutine.

Within the body of the coroutine, we need to first set isDashing to true. It will restrict us from spamming the space key and not dash infinitely.

isDashing = true;

We can then set other logic for the fuel and animations of the dash ability. In this example, I have them commented out.

Before we execute a yield, we want to increase the Player's speed to a certain amount. In this case, I doubled the default player speed of 4. So before the yield is executed, the speed will change to 8.

player.movement.Speed += _dashSpeed;

Whenyield is executed, we wait for a certain amount of time by using WaitforSeconds and passing in a time. So for 0.4 seconds, the Player’s speed is doubled until the time runs out.

yield return new WaitForSeconds(0.4f);

We then set the Player’s speed back to default.

player.movement.Speed -= _dashSpeed;

We also want to set isDashing to false so that we can dash again.

isDashing = false;

Calling the StartCoroutine

Remember, we need to call the coroutine using the StartCoroutine() function Unity provides us.

Let’s check where do we want to initialize the coroutine.

So, in this case, we want to call the coroutine in a condition within the Update() function. It makes sense. If we hit the space key, which is encapsulated in this example, we dash.

With all of this setup, we now have a simple dashing mechanic!

You can use coroutines for all kinds of mechanics, from having a UI element fade in and out, to flickering. It’s a very useful concept for your arsenal as a Unity Developer.

Thank you for reading!

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store