Learning Unity — Making Your In-Game Effects Temporary

Joshua Nielsen
3 min readJun 18, 2021

--

In the last article, we learned how to make use of the animation features in Unity to take our games to the next level. In this article, we’re going to examine how to make our in-game effects temporary.

Think about all the times in games where you have an effect on your character that changes the game, but is temporary. For example, think about when you catch the star in Super Mario Brothers, or the berserk powerup in Doom, or when you endure a poison effect in any number of games. You have an effect in play, but it goes away after a certain amount of time. How do we replicate that in Unity?

This turns out to be actually quite easy if we once again make use of the power of coroutines. Let’s try a simple example of doing nothing but changing the color of the object in question.

Create a primitive 3D object and accompanying script, then open the script in your editor. Because this example aims to change the color of the object, it would be a good idea to create a variable up front that holds a reference to the object’s Renderer.

We will then create the coroutine. Remember that all coroutines are of type IEnumerator. Within the coroutine method, we are just going to have three lines:

A simple coroutine that changes the color, then changes it again after 3 seconds

This is supposed to be a simple example, so don’t get too hung up on the details. The important aspect of this is to recognize what these three lines represent.

The first line (changing the color to blue) represents the temporary effect. For most temporary effects, this would undoubtedly involve a great deal more logic than we see here.

For example, consider the earlier example of Mario getting the star. If we were to code that ourselves, we would probably have a boolean flag on Mario to tell us if he is currently invincible. When Mario “collides” with the star, we set that flag to true. Then when Mario “collides” with an enemy, that flag being set to true would direct the code to kill the enemy instead of killing Mario.

Multiple aspects of the game could be affected at once. With Mario, on top of being invincible, we also hear a change in the music and see a graphical change in Mario himself.

The next line (waiting for three seconds) is the duration. This one is pretty straight forward and probably wouldn’t change much outside of this example. I will note however that the duration obviously doesn’t have to be three seconds, but it also could even be a random value.

The final line (changing the color back to red) represents the return to the normal condition. Removing the power/condition that the player momentarily had and returning to the before state.

In our example with Mario, this would mean he stops being invincible, the level music returns to normal, and Mario stops flashing. Like with the first line, this could involve complex game logic and many lines of code.

The color change coroutine in action

Of course, none of this will occur unless we call our StartCoroutine method somewhere in code that will be accessed. For this example, I started the coroutine in the Start method of the object, but it could be put anywhere.

Returning to our example of Mario and the invincibility star, we would probably start our coroutine in the code that handles the “collision” between Mario and the star.

And that wraps up our exploration of making in-game effects temporary. In the next article, we’re going to learn a method for making our object’s more modular and reusable. Until then, good luck and happy coding!

--

--

No responses yet