Explode Effect With Particle System
Our shooter is starting to have a good shape. It's time to enhance it a bit visually with particles.
Particles are (basically) simple sprites that will be repeated and displayed for a very short timespan.
Think about explosions, lasers, smokes, etc. Those are done with particles — most of the time (an explosion can be a simple animated sprite).
Unity provides a powerful built-in editor for particles with theShuriken Engine. Let's see what we can do with it.
Explosion prefab
We will make an explosion that is going to be used when an enemy or a player is destroyed. This involves to:
- Create a particle system of our explosion (as a prefab).
- Instantiate and play it when needed.
An explosion is usually made of two things: fire and smoke.
Smoke particles
Create a new "Particle System" ("Game Object" -> "Create Other" -> "Particle System").
If you want to focus on an object in the "Scene" view, you can
double-click
on it in the "Hierarchy", or press F
inside the "Scene" view.
By zooming on your particle system, you will see a continuous flow of sparks emitted by the particles game object:
Observe the new window (with "Pause" and "Stop") in the "Scene" view, or the "Inspector" pane. Yes, the latter is now filled with dozen of fields related to the particle system.
And that's where all the fun lie!
We will use this sprite for the smoke particles:
(Right click to save the image)
0
alpha. Indeed, even if a pixel is not visible, it still has a value. Which is used by the computer.
Copy the image in the "Textures" folder. Change the "Texture Type" to "Texture", and set "Alpha Is Transparent". You should have:
Assign the texture to the particle:
- Drag the texture to the your particle system in the "Inspector" (or onto the particles object in the "Hierarchy", which will assign the texture to the right property inside the "Inspector").
- Change the shader to "Particles" -> "Alpha Blended":
To create a perfect smoke particles, we have to change many settings in the particle system "Inspector".
For the tutorial, we recommend to use:
Category | Parameter name | Value |
---|---|---|
General | Duration | 1 |
General | Max Particles | 15 |
General | Start Lifetime | 1 |
General | Start Color | Gray |
General | Start Speed | 3 |
General | Start Size | 2 |
Emission | Bursts | 0 : 15 |
Shape | Shape | Sphere |
Color Over Lifetime | Color | See below (N°1) |
Size Over Lifetime | Size | See below (N°2) |
N°1 — Color Over lifetime
Set an alpha in the end to create a fade out:
N°2 — Size Over lifetime
Choose a decreasing curve:
You should have:
Feel free to tweak the system. Play with the editor to see how it works. It's your game after all. :)
When you are satisfied, uncheck "Looping".
Observe the result:
It's clearly not perfect, but notice how simple it was to create. Adding particles can make the difference between a dull game and one that is fun to watch.
Save as a prefab. You can organize a bit by creating a folder "Prefabs/Particles" and calling the prefab "SmokeEffect".
Fire particles
This one is no different.
- Create a new particle system, just as you did above.
- Use the default material for fire ("Renderer/Material" to "Default-Particle"). It's enough for our needs.
We recommend to use:
Category | Parameter name | Value |
---|---|---|
General | Looping | false |
General | Duration | 1 |
General | Max Particles | 10 |
General | Start Lifetime | 1 |
General | Start Speed | 0.5 |
General | Start Size | 2 |
Emission | Bursts | 0 : 10 |
Shape | Shape | Box |
Color Over Lifetime | Color | See below (N°1) |
N°1 — Color Over Lifetime
Create a nice gradient from yellow to orange, with a fade out in the end:
And you should get:
Save as a "FireEffect" prefab.
Now, we are going to use these prefabs in a script.
Helper script
Instantiating those particles prefabs is identical to instantiating a player or a shot.
However, you must remember that they should get deleted when they are no longer needed.
Also, we will do a combination of a fire particle system and a smoke particle system in the script for our explosion.
Let's create a "SpecialEffectsHelper" script:
using UnityEngine;
/// <summary>
/// Creating instance of particles from code with no effort
/// </summary>
public class SpecialEffectsHelper : MonoBehaviour
{
/// <summary>
/// Singleton
/// </summary>
public static SpecialEffectsHelper Instance;
public ParticleSystem smokeEffect;
public ParticleSystem fireEffect;
void Awake()
{
// Register the singleton
if (Instance != null)
{
Debug.LogError("Multiple instances of SpecialEffectsHelper!");
}
Instance = this;
}
/// <summary>
/// Create an explosion at the given location
/// </summary>
/// <param name="position"></param>
public void Explosion(Vector3 position)
{
// Smoke on the water
instantiate(smokeEffect, position);
// Tu tu tu, tu tu tudu
// Fire in the sky
instantiate(fireEffect, position);
}
/// <summary>
/// Instantiate a Particle system from prefab
/// </summary>
/// <param name="prefab"></param>
/// <returns></returns>
private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position)
{
ParticleSystem newParticleSystem = Instantiate(
prefab,
position,
Quaternion.identity
) as ParticleSystem;
// Make sure it will be destroyed
Destroy(
newParticleSystem.gameObject,
newParticleSystem.startLifetime
);
return newParticleSystem;
}
}
We created a singleton that you can access from everywhere using the
SpecialEffectsHelper.Instance
member.- Assign the script to the "Scripts" game object in the "Hierarchy".
- Inspect it, and fill the fields with the correct prefabs.
Blow up that thing !
It's (finally) time to call the script.
Open "HealthScript". We will check when the game object is destroyed and, then, we will display our sweet explosion.
We just have to add one line:
SpecialEffectsHelper.Instance.Explosion(transform.position);
Into the
OnTriggerEnter()
method of the "HealthScript": public Damage(int damageCount)
{
// ...
if (hp <= 0)
{
// 'Splosion!
SpecialEffectsHelper.Instance.Explosion(transform.position);
// Dead!
Destroy(gameObject);
}
// ...
}
Start the game. Try to shoot enemies. Then, let the player dies.
(Click to see the result)
Not bad, right? Could be way better, though.
But hey, it's up to you to make beautiful explosions using particles, now that you have an idea on how it works. ;)
Next Step
We have learned how to create particles using the built-in engine of Unity. And as you have seen, it's that easy. Moreover, they add delight and feedbacks to your game.
Be careful however! It can slowly become time-consuming as you will probably prefer to tweak effects rather than code boring features like high-scores.
As James Silva wrote in Building XNA 2.0 games, you should work on particles at the end of the development or the other tasks will quickly loose interest.
Like using particles, there is another way to add feedbacks to your game: Sounds! And that's just what we are going to learn in the next chapter.
Source : Source Web
Explode Effect With Particle System
Reviewed by Kado Lampu
on
21.18
Rating:
Tidak ada komentar: