Introduction To Unity: Particle Systems Animación 2D y 3D Particle Systems ● Modern games that don’t use particle effects in some manner feel can quite bland. ● Particles also add authenticity to scenes. They can be the foundation of a roaring campfire, or the luminous effects of a wizard’s spells. They can also be used to create smoke, dust, explosions, and even rainbow puke! ● In essence, particles are small, simple images or meshes that are emitted and then manipulated by a particle system during their lifetime. Particle Systems ● Back in the old days, you needed to know the black arts of graphics programming to make even a wisp of smoke. ● Thankfully, Unity makes creating particle systems quite simple. Unity uses a powerful, modular built-in particle system named Shuriken. ● It is quite easy to learn but also lets you create complex effects – once you get to know it. Particle Systems ● In this lesson you’ll learn the following: ● How to add a new particle system in Unity. ● What the most commonly used particle system modules do and how to use them. Particle Systems Particle Systems Particle Systems LINK VIEW Particle Systems ● When you select a GameObject with an attached particle system, you’ll notice a black dialog in the lower right hand corner of the scene view. ● This dialog gives you the ability to simulate or stop the particle system. ● Clicking Simulate activates your particle system and changes the button to a “Pause” button. ● To stop the simulation, click the “Stop” button. A Closer Look at a Particle System A Closer Look at a Particle System ● Each of these sub-sections is called a Module. ● These Modules contain the settings for the particle system. ● The Module expanded by default is known as the Main module: A Closer Look at a Particle System ● Duration: The length of time in seconds for the particle to run. Leave this at the default value of 5.00. ● Looping: Repeatedly emit particles until the particle system stops. The cycle restarts once the Duration time is reached. ● Left: on Right: off ● On & off ● The fire needs to burn continuously, so leave this enabled. A Closer Look at a Particle System ● Prewarm: Only used when Looping is enabled. The Particle System will act as if it’s already completed a full cycle on start-up. ● On & off ● For your fire effect, leave this disabled to make it look like the torch was just ignited. A Closer Look at a Particle System ● Start Delay: The delay in seconds before the particle system start emitting. Leave this at the default value of 0. ● Start Lifetime: The initial lifetime in seconds for the particles. The particle is destroyed after this elapsed time. ● Top: 8 seconds ● Bottom: 3 seconds ● Set the lifetime to 1.2 seconds; this ensures the flame won’t be too tall. A Closer Look at a Particle System ● Start Speed: The initial speed of the particles. The greater the speed of the particles, the more spread out they will be. ● Top: 2 ● Bottom: 10 ● Set the speed to 0.75; this will make the fire effect slower and more dense. A Closer Look at a Particle System ● Start Size: the initial size of the particles. ● Top: 0.2 ● Bottom: 1 ● Set the size to 0.25; this is a manageable size that lets you see the individual particles more clearly. A Closer Look at a Particle System ● Start Rotation: The initial rotation angle of the particles. ● Top: 0° ● Bottom: 45° ● Keep the rotation at 0°; the particles are round so you will hardly notice the difference. A Closer Look at a Particle System ● ● ● Start Color: The initial color of the particles. Keep the color at the default value of pure white (255,255,255); you’ll color the fire via a texture. Gravity Modifier: Scales the gravity value set in Unity’s Physics Manager window. If it’s set to 0, the gravity will be turned off. The image above is from a system with the gravity modifier set to 1, which makes the particles flow down like a waterfall. Leave the gravity in your system at 0; the particles will move upwards with the velocity you set in Start Speed. A Closer Look at a Particle System ● Inherit Velocity: Starts particles with the same velocity as the particle system. This only works if the particle system has a Rigidbody attached. ● Left: 1 ● Right: 0 ● Keep Inherit Velocity at 0; you don’t need this for the fire effect. A Closer Look at a Particle System ● Simulation Space: Moves particles in Local Space along with the particle system; while in World Space, particles move freely once emitted. ● Top: local space ● Bottom: world space ● Leave your simulation set to Local Space. The effect will only be visible once the particle system is moved. A Closer Look at a Particle System ● Play On Awake: Starts emitting immediately when enabled. If this is turned off, you have to manually start the particle system via script or an animation system. Leave this setting on, as you want the fire to start when the scene starts playing. ● Max Particles: The maximum amount of particles the particle system is allowed to have alive at any time. If you try to emit more particles than this once the system hits this limit, they won’t be emitted at all. This setting is used primarily for performance reasons; the default value of 1000 particles is more than enough in this case. A Closer Look at a Particle System Introducing the Emission Module ● The Emission module is one of the most important modules in Unity particle systems; it handles the number and timing of emitted particles in the system to create a continuous flow or a sudden burst of particles depending on your needs. While still in the particle system’s Inspector, click on the Emission module title: ● This opens up the Emission module: ● The Rate is the number of particles emitted per second (Time), or alternatively, per unit (Distance). You can switch between the two modes by clicking the combo box and selecting the desired mode: ● ● Leave the settings at Time and change the Rate to 50. Run your scene again; it looks a lot more lively now: ● Adding a Custom Texture ● ● All particles have a particle material and a texture that defines how they look. There’s only so much you can do with the default texture. By changing the texture you can create effects like magic stars, smoke and of course, fire Changing the particle texture is quite easy. Up to now, the particles were drawn on the screen using the Default-Particle material, which is a particle material with a circular gradient texture: ● To change the material, select the TorchFireParticles GameObject inside the Hierarchy. Then, find the the particle system component in the Inspector, and open up the particle system’s Renderer module. ● ● Open the Materials folder in the Project View, and drag the FireMaterial Material to the Material property: Finally, run the scene to see your custom texture in use: Changing the Particle System’s Shape ● ● The Shape module, as the name implies, controls the shape and the behavior of particles in that shape. You can choose from several different shapes; each has their own particular settings. This lets you create particles in a box, a sphere, or even in your own custom mesh! Expand the Shape module in the Inspector: ● The shape of your particle system is set to cone, which means particles emit from the base and are moved outwards at an angle: Changing the Particle System’s Shape ● In the example above the base is colored blue, the angle is green and the particles are red. Also notice that while you have the Shape Module expanded, you’re presented with a handy preview of the cone in the Scene view: ● Changing the Angle changes the size of the cone to make it wider or narrower. Set this to 10; you’ll get a nice tight flame that widens slightly as the particles rise. Changing the Radius changes the size of the base; the larger this value is, the more the particles will scatter as they emit. Set this value to 0.2; this ensures the flames will start inside the fuel holder of the torch. Run your scene and see how the shape of the flame has changed: ● ● Changing Size Over Lifetime ● ● With the Size over Lifetime module, you can create particles that grow or shrink during their lifetime, or even pulsate like fireflies in a forest. In your particle system’s module list, look for “Size over Lifetime“. Contrary to the previous modules, Size over Lifetime isn’t enabled by default. Click on the checkbox next to the module name to enable it: ● Expand the Size over Lifetime module by clicking on its name. This will display a dark gray background with a flat Curve running along the top: ● Click on the dark gray background to open the curves editor at the bottom of the Inspector. The horizontal axis depicts the lifetime of the particle while the vertical axis depicts its size: Changing Size Over Lifetime ● You can move the keys at either end of the red line to edit the curve; you can also add additional keys by double-clicking anywhere on the curve. To delete keys, right-click on the key to remove and select Delete Key: ● You can also select one of the preset curves at the bottom: ● If you think about flames in the real world, they tend to shrink as the particles rise, so select the third preset from the right to create a downwards slope: ● Run your scene to see your effect in all its fiery glory! Changing Size Over Lifetime Exercise: How to add flickering shadows Exercise: Solution ● ● ● ● ● ● ● ● ● ● ● ● ● Disable the Directional Light in the Hierarchy. Add a Point Light to the center of the fire particles effect. (0, 0, 4) Make the Color slightly orange (HEX value FFC68AFF). Set the Range to 50. Set the Intensity to 3. Add an Animation and save it somewhere in your Assets folder. Assign the Animation you just made to the Point Light. Open an Animation Window. Select the Point Light. Set the Shadow Type to Hard Shadows. Set the Samples to 50. Every few frames, add a keyframe and slightly move the Point Light on the X and Y axes. There’s no golden rule for this, if it doesn’t look right when you play the animation, tweak the movement some more. Play your scene. Building a Bomb (Effect) ● Making an exploding bomb is quite simple using Unity. Once you know how to instantiate particles at will, you can use this effect for things such as car wheels sparking when they scrape the ground, or balloons that pop and shower confetti. ● Open up the Bomb scene from the Project Window and play the scene: Building a Bomb (Effect) ● There’s a floor at the bottom of the scene, but apart from that, there’s not much going on at the moment ● ● ● To spawn bombs, drag the Bomb Prefab to the Bomb Emitter: Play the scene again to see your bombs appear: The emitter creates a new bomb every 2 seconds. Building a Bomb (Effect) ● ● ● To put a neat spin on things, you’ll add some rotational force to the bomb when it spawns. Open up the Bomb script inside the Scripts folder in your Project Window. Add the following code to Start(): ● The first three lines generate random float values between 10 and 100 for the x, y and z axes. Next, you get a reference to the bomb’s Rigidbody component and apply torque to it. This causes the bomb to rotate in a random direction: Building a Bomb (Effect) ● ● In the Hierarchy, press the Create button and select Create Empty. Click on the newly created GameObject and name it ExplosionParticles. Next, add a new particle system to the GameObject. With your particle system in place, drag the ExplosionParticles GameObject from the Hierarchy to the Prefabs folder in the Project Browser. Next, select the Bomb Prefab inside the Prefabs folder. Finally, drag the ExplosionParticles Prefab to the Bomb‘s Explosion Particles Prefab slot like so: ● ● Now, when a bomb touches the ground a new Explosion Particles GameObject will spawn. Play your scene to see how the explosion looks: ● ● ● ● Building a Bomb (Effect) ● ● ● Delete the ExplosionParticles GameObject in the Hierarchy; from now on you’ll be editing the prefab. As with the torch, you’ll be using the fire material for the particle system. Select the ExplosionParticles Prefab in the Project Window, then expand the Renderer Module in the Inspector. Drag the FireMaterial from the Materials folder in the Project Window to the Material slot as shown below: ● To complete the effect, you’ll have to modify the following settings in the Main module: Building a Bomb (Effect) ● To complete the effect, you’ll have to modify the following settings in the Main module: 1. Set the Duration to 0.70. 2. Looping should be disabled. The particles should emit just once. 3. Set the Start Lifetime to 0.7. 4. Set the Start Speed to 10. 5. Set Start Size to 2. 6. Set the Gravity Modifier to 1. This will make the particles drop slightly at the end. ● Run your bomb scene to see what you’ve built: Building the Explosion ● ● To improve the explosion, you’ll alter the properties of one particle system’s modules. Expand the Emission Module; as you saw earlier, the Rate is the number of particles spawned per second. For this explosion, you won’t want a steady flow of particles, but rather a sudden burst. ● Set the Rate to 0. Now take a look beneath the Rate; there’s a list of Bursts that’s empty by default: ● ● A Burst is a collection of particles emitted all at once at a particular point in time. Click on the + button at the bottom right to add a new Burst. You’ll see two fields: Time & Particles: Building the Explosion ● ● ● Leave the Time at 0, and set Particles to 150. These settings will make the particle system emit 150 particles all at once at the start of the system. Play your scene Building the Explosion ● ● ● Leave the Time at 0, and set Particles to 150. These settings will make the particle system emit 150 particles all at once at the start of the system. Play your scene ● Now THAT looks more like an explosion! While this explosion looks better, the shape is still an awkward cone and the particles don’t fade out — they just disappear. You’ll need to mold your explosion to give it that final touch. To get started, expand the Shape Module: ● Building the Explosion ● You’ve already used this module for the torch’s fire shape, but there are several more shapes to choose from. Click on the dropdown box that says Cone to see all options available to you: ● Each shape affects the emitter in a different way. Each animation below shows the same emitter, with only the shape changed: Building the Explosion ● Sphere ● HemiSphere ● Cone ● Box Building the Explosion ● Mesh (Cube) ● Circle ● Edge Building the Explosion ● ● To create a realistic explosion, set the shape to Sphere. Run the scene and prepare to be blown away: Changing Color ● With the particle system open in the Inspector, click the checkbox next the Color over Lifetime module to enable it and expand it. You’ll be greeted by the word Color and what looks like a white block next to it. Click on the white block: ● This opens up the gradient editor: ● The color change over the lifetime of the particles is represented as a gradient bar. The starting color is on the far left, and the particles will transition to the color on the right side: Changing Color ● The four white arrows at the edges are known as markers; click between two existing markers to add a new one. To remove a marker, drag it off the bar: ● The top markers handle the Alpha or opacity of the color, while the bottom markers manage the RGB (Red, Green, Blue) color values. Changing Color ● Click on the rightmost alpha marker. The bottom of the Gradient editor now shows the current alpha value: ● ● Drag the slider all the way to 0. Now the particles will gradually fade away over the course of their lifetime. Run your scene once again to see the effect of your changes: Exercise ● Return to the torch scene and configure the flame to use Size Over Lifetime Module to achieve a similar effect.