After getting my enemies to explode like juicy grapes, it’s time to work on the Player’s visuals. During the chaos of fast-paced action, the user might not be aware of having taken damage, so I want to provide them with some immediate visual cues to help them.
Create visualizations for player damage.
It could be interesting to create animations for this purpose. However, one of the aspects I value most is randomization. Creating fixed animations might make the game visually monotonous. I, therefore, will be using particle systems combined with sprite animations to generate this effect.
I will begin by creating an empty GameObject and naming it PlayerDamageEffect. I will then add a ParticleSystem Component to this GameObject.
This new GameObject will be the emitter of the player damage visuals. As soon as I add the ParticleSystem, the effect begins to play in my Scene view.
Not exactly what I am after. Not even close. It’s time to start editing the ParticleSystem to obtain my desired effect.
Step 1: The Emitter’s Shape is set to Cone by default. Since I am working in 2D, I can change the shape of the emitter to Edge.
Instantly the particles begin to emit differently. Their direction and position now stem from a line that I can resize and reposition as needed. For now, I will set the Radius to 0.1.
Step 2: I will change the sorting order of my particles in the Rendering tab of the ParticleSystem.
Doing so will display the particles above my ground plane and make it easier for me to preview my changes.
Step 3: I want my particles to emit backward relative to my player. I can change this by editing the velocity of the particles.
I have chosen this value based on the speed of my background element.
Step 4: After having imported and sliced my particle animation sprite sheet, I will add each slice to the Texture Sheet Animation module.
I add each slice one at a time. I then apply a material in the Rendering module. The material is required for the particles to display correctly. And they are starting to look much better. They still need some work.
Step 5: I will resize the emitter and the particles to fit the scale of my Player. I will also lower their Life Time value of each particle to around 1.5.
Step 6: Currently, the animation looks very consistent. I will change the Initial Rotation of each particle to a random value for additional variety.
With this initial rotation, the particles begin to act a little more chaotically.
Step 7: I will change the value of each particle’s Color Over Lifetime. I want to create the effect of dark, fiery smoke.
I have prepared a gradient that spans the range of colors I want each particle to have throughout its lifetime.
At this point, I can use what I have to set up my code and get the effect working in-game.
I will create a prefab of this GameObject by dragging it into my Assets folder. Then I will use the prefabs as children of my Player, positioning them where I want the smoke to appear.
Each prefab gets renamed to describe its position on my player’s motor.
To get the new effects to work in my game, I will begin by creating an Array of GameObject type variables in my Player script.
I can populate this Array in the Inspector by dragging in my prefabs.
The next step is to make sure these GameObjects are not active when the game starts.
foreach() loop will help us iterate through each item in a collection. In my case, it sets the value of the GameObjects status to inactive.
The last step is to activate these effects as the player’s health decreases. I can use a Switch statement for this.
I can check for the current value of Health and activate the damage effects accordingly.
The code works just as intended.
After spending more time tweaking the ParticleSystem and adding additional ones as children, this is the current state of the effect.
Unity’s ParticleSystem is a complex and powerful tool. I could spend days and days going over every detail. I hope, however, that this guide has inspired you to play around with particles and that it has shed some light on how different modules work.
For now, I feel that my users will have an easier time keeping track of the player’s health.