Improving Enemies with Particle Effects

Yet again I am back on Project 1, making improvements. This time I decided to add some more visual features to the enemies. I wanted to make the enemies stand out because they can be hard to see, since both the enemies and the background have similar cream colours. Also, since the enemy sprites just disappear when they have lost all their health, I wanted to have a fade or a transition just so it looks better.

What I added is a particle effect that sits behind an enemy and fades after the enemy dies. The effect is intended to make the enemy appear as if they have an aura.

To do this I created a Particle System in Unity, set its Shape to Mesh and its Mesh to Cylinder.

ShapeMesh.png

The reason I selected Mesh > Cylinder instead of Circle is because particles spawn in the centre and around the edge of the Cylinder, whereas Circle has particles spawning inside the Circle. You can see the difference below.

ShapeCompare.png

ShapeCompareSettings.png

The Particle System on the left is the Cylinder and the Particle System on the right is the Circle. Both have the same settings, which you can see in the image above.

The Cylinder Shape suits my needs more because, when speed is set to a negative, all the particles centre in a single spot. This gives me the kind of fading effect that I am looking for. Below is an image of the Cylinder Particle System with Start Speed set to -0.5 and Emission > Rate Over Time set to 500.

Cylinder.png

For the actual Particle System I used the following settings and got the following result:

CylinderSettings.png

CylinderDone.png

Now to put the Particle System underneath an enemy sprite.

CylinderSprite.png

In order to make the Particle System linger and fade after the the Enemy sprite has been destroy, we need to write some code. We can’t make the Particle System a child of the the Enemy sprite because when the Enemy sprite is destroyed the Particle System will be destroyed as well.

The first that we have to do is reference the Particle System.StopLoopCodeStart.png

Also, set up a bool that we will use later.

StopLoopCodeBool.png

Then, in the function that controls when the enemy is destroyed, line 41 of the code below retrieves the Main section of our Particle System and line 42 retrieves the Loop setting of our Particle System and changes its value to the bool made earlier.

StopLoopCode.png

Be sure to put the code above the Destroy code, otherwise the Enemy will be destroyed and will not run anymore code.

Now we have a Particle System that fades when the Enemy sprite is destroyed, but what happens when the Enemy sprite moves? Well the Particle System, at the moment, doesn’t follow the Enemy sprite. Let’s change that.FollowCode.png

The above code has a public reference to a GameObject, meaning that we can drag and drop the specific Enemy sprite that we want to be associated with this code. Then the Follow() function finds the Enemy’s position, if available, and makes the Particle System’s position equal to it. Finally, this function is called in Update().

Now the Particle System follows the Enemy sprite and fades away shortly after the Enemy sprite is destroyed.

Advertisements

Creating a Particle Effect

Particle Effects:

For Project 1, I created a particle effect that changes based on whether the player has finished the level or not. The purpose of this particle effect is to have a more visually appealing indicator that the level is complete and that the player can move to the next one. I also wanted the particle effect to match the art style of the game, which is pixel graphics. Note that this project was done in 2D.

I started by creating a Particle System in Unity by right-clicking inside the Hierarchy and selecting the Particle System option.

HierarchyParticle.png

Doing this will make a GameObject called Particle System appear, similar to the Particle System that, as you can see, is a child of the ExitLevel GameObject.

The default Particle System shoots white circles in a cone shape, as can be seen below. However, this doesn’t fit well with the current theme of the game.

SceneParticle.png

So, to fix this, the first thing I did was to change the Material in the Renderer settings. I want this particle effect to mimic a section of the border so I created a Material that is the same colour as the border and applied it to the Particle System, the result of which you can see below. Note, however, that in order to get this result I had to change the Shader that is on the Material from Standard to Unlit > Color.

SceneParticleMaterial.png

The next thing that I changed was the Shape. The default shape is a cone but, in order to make a believable-looking wall, that will have to change. After experimenting with a few different shapes, I settled on the Edge shape.

The first thing to adjust is the rotation. The default X rotation of a Particle System is -90 degrees which means that, with the Edge Shape applied, the particles are now shooting towards the camera, if in the 2D camera setting. Set the X rotation to 0 to stop that. So now the Particle System should look the the one pictured below.

SceneParticleEdge.png

Now, while that seems all well and good, it doesn’t mimic a wall very well. The first step to fixing this is to position the Particle System where I want it and adjust its value from there. As you can see in the picture below it doesn’t fit quite, since it is supposed to fit in the small opening in the border below it.

SceneParticlePlaced.png

The first setting I change was the Start Size, to 0.1, since those particles are far too large for what I need them to do. Then I went to Shape > Radius and changed it to 1.8 in order stop the particles from extending into the border. After that I did some tinkering with both Start Lifetime and Start Speed, finally settling on 1 and 0.66 respectively.

The Particle System now looks like the image below. There is a fairly obvious problem here, that problem being that there aren’t nearly enough particles to make a convincing mimic of a wall.

SceneParticleAdjusted.png

In order to add more particles, I went to Emission > Rate Over Time and changed it to 10,000. This, however, isn’t the only setting that must change. If you don’t change Max Particles to 10,000 (Rate over Time multiplied by Start Lifetime) as well, you will get a solid line of particles once every second instead of a block of particles.

The image below is a big block of 10,000 particles. Now, all that remains is adding some functionality through coding.

SceneParticleDesignDone.png

Here is what I have written for the Particle System:

ParticleCode.png

The first thing I do is reference the EndLevel script because there is a function in there that detects if all the enemies have been defeated. Then I wrote a public reference to the Particle System. This allows me to assign the Particle System in the Inspector. Finally, the float partiChange determines the rate at which the Particle System decreases emission.

The function LevelComplete() check if all the enemies are defeated and, if so, retrieves the emission and then the emission.rate of the Particle System. I then change the constantMax of the emission.rate so that it decreases according partiChange. Then, last of all, is an if statement that checks if the constantMax goes below a certain point. If it does go below that amount the rate that constantMax decreases at is set to 0, meaning that it remains at the rate that it dropped to.

This script is then attached to the ExitLevel object that was in the Hierarchy from earlier. Now the Particle System is done and this is the final result.