Madwomb





Cartoony Explosion Particles

Playing with particles Pixelnest 18 nov. 2013

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 the Shuriken 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:

1. Create a particle system of our explosion (as a prefab).

2. Instantiate and play it when needed.

An explosion is usually made of two things: fire and smoke.

|Smoke particles |[pic] |

|Create a new "Particle System" ("Game Object" -> "Create Other" -> | |

|"Particle System"). | |

| | |

|Tip: We recommend that you work on an empty part of the scene (or in | |

|an empty scene) so you can clearly see what is happening. | |

| | |

|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, |[pic] |

|or the "Inspector" pane. Yes, the latter is now filled with dozen of |(Right click to save the image) |

|fields related to the particle system. | |

|And that's where all the fun lie! | |

|Note: When you select a particle system in the "Hierarchy", it starts | |

|simulating the system. If you unselect it, it stops. It's really | |

|practical to see what the system does in real (and instantly) when you| |

|conceive it. | |

|We will use this sprite for the smoke particles: | |

|Tip: If you have an issue with the transparency when using your own |[pic] |

|asset, be sure that transparent pixels are black ones with 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: | |

Note: We are using a Unity 3D feature, not a "2D one". In fact, it doesn't matter. When you use the "2D" tools, you are only using a subset of Unity. But the full power of Unity is still there, ready to be used.

Assign the texture to the particle:

1. Drag the texture to 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").

2. Change the shader to "Particles" -> "Alpha Blended":

[pic]

To create a perfect smoke particles, we have to change many settings in the particle system "Inspector".

|For the tutorial, we recommend to use: |[pic] |

|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: | |

|[pic] | |

| | |

|N°2 — Size Over lifetime: Choose a decreasing curve: | |

|[pic] | |

| | |

|You should have: | |

|Feel free to tweak the system. Play with the editor to see how it works. It's |[pic] |

|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. |Category |

|Create a new particle system, just as you did above. |Parameter name |

|Use the default material for fire ("Renderer/Material" to |Value |

|"Default-Particle"). It's enough for our needs. | |

|We recommend to use: |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 |And you should get: |

|Create a nice gradient from yellow to orange, with a fade out in the |[pic] |

|end: |Save as a "FireEffect" prefab. Now, we are going to use these prefabs in |

|[pic] |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

public class SpecialEffectsHelper : MonoBehaviour

{

/// Summary: Singleton

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

///

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

///

///

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;

}}

Note: Because we can have multiple particles in the scene at the same time, we are forced to create a new prefab each time. If we were sure that only one system was used at a time, we would have kept the reference and use the same everytime.

We created a singleton that you can access from everywhere using the SpecialEffectsHelper.Instance member.

Singleton: A singleton is a design pattern that is used to guarantee that an object is only instanciated once. We have diverged a bit from the classic implementation in our script: the principle remains, however.

1. Assign the script to the "Scripts" game object in the "Hierarchy".

2. Inspect it, and fill the fields with the correct prefabs.

[pic]

Blow up that thing! It's 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 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related download
Related searches