Fireworks using the Unity Particle System

1 Introduction

This blog is a big homework of Sun Yat-sen University 3D game programming and design

2. Job Requirements

1. According to the requirements of reference resources, make a particle system

2. Using the introduction in section 3.3, use code control to make it have different effects in different scenarios.

3. Resources used

3.1 Basic resources , mainly used for the appearance of the fireworks and the smoke produced after the explosion of the fireworks.

3.2 Map resources , free resources downloaded from the assets store, make the final display of fireworks more beautiful.

4. Production process

4.1 Production of Firework Shells

To make a firework, you need to first craft a firework shell before the firework explodes.

First add a particle system to the scene, and then set the rotation of the particle system transform to (-90,0,0), so that the particle system will send particles upward.

Then set the basic settings of the particle system. First, set the Start Size and Start Speed ​​to appropriate values, and the Start Lifetime is the survival time of the particles. Here, it is set to Random Between Two Constants, which is a random number between 2 and 4, so that the fireworks will explode at different times after launch. At the same time, set the Gravity Modifier to 0.3, so that the fireworks will have a parabolic arc after launch.

Sets the particle system's particle emission rate to 1.

Set the emission radius of the particle system to 0.01, and the emission angle is specified within 35°, so that it looks like a funnel shape, which is more in line with the effect of firework projectiles.

                                           shape setting

                    Particle System Shape Effects

Check Size over Lifetime, and set the size of the particles to change with time, so that the fireworks will have a flickering effect, which is more realistic, as shown in the figure.

Then drag the flame material in the basic resource to the material in the Renderer of the particle system to make the fireworks more beautiful.

 

The effect of the fireworks bomb is as follows, you can see that the fireworks bombs are big and small.

4.2 Tail production of fireworks shells

In order to make the effect of the fireworks more realistic, add a trailing effect to the fireworks. Check Sub Emitters, and add a sub-emitter named tail.

Set the basic settings of tail. The survival time of tail particles should be relatively short, set to a random value between 1-2, and the size is relatively small, set to 0.5, and also need to add gravity effect, set to 0.2.

Set the shape of the tail particle to be spherical and set the radius to 0.003.

In order to make the trailing effect more cool, you need to check Color over Lifetime so that the particle color changes with time.

At the same time, in order to make the trailing particles disappear less abruptly, check Size over Lifetime so that the particle size becomes smaller over time and eventually disappears.

Elongate the particles, set the Speed ​​Scale to 0.15, and the Length Scale to 2, so that the particles will be longer and make the trailing effect more realistic.

In order to make the trailing effect more obvious, add a sub-emitter tails'tail to the tail. The settings of the sub-emitter are similar to those of the tail, and will not be described in detail here.

 The final effect of the fireworks tail is as follows

4.3 Add smoke effect

After the trailing effect is done, we need to add the effect of the fireworks explosion. Before that, we first add a smoke effect produced by the explosion. First of all, we need to add a sub-emitter smoke to the Sub Emitters of the fireworks, which will be triggered when death occurs.

Then set the properties of the smoke. First of all, set the basic settings. It is worth noting that the Start Size is set to a random value between 40-70, because the size of the smoke is not constant, and the Start Rotation is set to a random value between 0°-180°, which is to make the smoke look different every time.

 Then set the Emission. Since the smoke only needs to appear once for each explosion, it is set to only one round of explosion and the explosion will only produce one particle.

Then set Size over Lifetime, and set the size of the smoke to increase with time, so that there will be an effect of smoke diffusion.

  

 Finally, select a smoke material and drag it into the material of Renderer. Here, the smoke in the resource mentioned in 3.1 (the selected smoke) is used.

 The final smoke effect is as follows (in order to make the effect more obvious, the background is adjusted back to the default background)

4.4 Increase the effect of fireworks explosion

Add three sub-emitters to the smoke. When the smoke is generated, these three sub-emitters also emit particles at the same time. These three emitters emit red, blue, and green particles respectively, which makes the fireworks more beautiful.

Only the red sub-emitter is introduced in detail here, and the other color sub-emitters are just different in color, and everything else is the same.

First set the basic settings, the survival time, initial speed, and size of each firework particle are different, so they are all set to random values ​​between two constants. The color is set to red and the gravity is set to 0.2.

 

 Set Emission, because each explosion of fireworks will produce a large number of particles, and the number of particles is variable, so set the number of particles between 700-900.

 Set Size over Lifetime to make the particle size smaller with time, creating the effect of fireworks gradually extinguishing.

 Finally, it is the same as the trailing effect. In order to make the explosion more cool, add a child emitter redflame. The settings are similar to the parent emitter, but the particle size is reduced.

 After adding three sub-emitters of different colors, the effect is as follows (here, in order to make the effect more obvious, the firing rate of the firework shells is adjusted to 3 times the original)

 4.5 Control script

Write a script with two buttons that start and pause the firework launch. After writing, mount the script on the empty object, and then drag the particle system to the script to control the particle system.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class mono1 : MonoBehaviour
{
    public ParticleSystem particleObject1; 
    public GUIStyle buttonstyle;
    string str="";
    void OnGUI(){
        if (GUI.Button (new Rect (0,Screen.height-55,55,55), "放烟火")) {
            particleObject1.Play();   
        }
        if (GUI.Button (new Rect (55,Screen.height-55,80,55), "停止烟火")) {
            particleObject1.Stop();
        }
    }

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

The effect after adding is as follows (remember to uncheck the play on awake of the particle system, so that the fireworks will not start until the button is pressed)

5. Apply to the scene

Apply the finished fireworks to the resource scene downloaded in 3.2.

Because there are six particle systems here, the script code needs to be slightly modified, as follows.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class mono2 : MonoBehaviour
{
    public ParticleSystem particleObject1; 
    public ParticleSystem particleObject2;
    public ParticleSystem particleObject3;  
    public ParticleSystem particleObject4;  
    public ParticleSystem particleObject5;  
    public ParticleSystem particleObject6;  
    public GUIStyle buttonstyle;
    void OnGUI(){
        if (GUI.Button (new Rect (0,Screen.height-55,55,55), "放烟火")) {
            particleObject1.Play();
            particleObject2.Play();
            particleObject3.Play();
            particleObject4.Play();
            particleObject5.Play();
            particleObject6.Play();
        }
        if (GUI.Button (new Rect (55,Screen.height-55,80,55), "停止烟火")) {
            particleObject1.Stop();
            particleObject2.Stop();
            particleObject3.Stop();
            particleObject4.Stop();
            particleObject5.Stop();
            particleObject6.Stop();
        }
    }

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

The final result is as follows.

6. Code download 

The code warehouse  replaces the original assets folder after downloading the code, then open myscene->Scene->flamescene and run it.

7. Demonstration video

demo video

Guess you like

Origin blog.csdn.net/weixin_52801288/article/details/128541769