Flickering Color Effect in Unity
2D Space Shooter — Phase II: Core Programming Challenges
In the previous article, we took a look at how to make an Aggressive Enemy Type that rams the player. In this article, we will be focusing on adding visual representation to the attack.
Objective: Create a flickering or blinking color effect using sprites in Unity.
- We will need a variable that allows us to access the sprite renderer, and a bool variable to keep track of when the flicker is enabled. The bool variable will be false by default and only switched to true whenever the ramming action is about to execute.
2. Drag the spirit to the newly created slot in the Inspector.
3. Create a coroutine to manage the flickering effect.
- Here, we will access the Sprite Renderer’s color and change it every 0.5 seconds. This coroutine will run as long as the bool variable has been set to true, in my case, the variable is set to true when the Enemy Spawns. However, know that this alone will not make the coroutine execute.
4.The flicker bool variable is set to true when the enemy spawns. You may use this variable to manipulate when to trigger the coroutine’s behavior, it doesn’t necessarily have to be the same as my code.
Note: As a best practice, the bool variable change was placed in a method, this method is called whenever we need it to change value.
5. Execute/ Start your coroutine. Whenever the bool is set to true, the coroutine will become active.
The Result:
This article has been part of a series of Core Programming Challenges by GameDevHQ. In the next article, I will be taking a look at how to make it so enemies can pick up Power-Ups!