Reader small image

You're reading from  Unity Cookbook - Fifth Edition

Product typeBook
Published inNov 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781805123026
Edition5th Edition
Languages
Tools
Right arrow
Authors (3):
Shaun Ferns
Shaun Ferns
author image
Shaun Ferns

Shaun is a lecturer at Technological University Dublin. He is currently teaching on the BA (Hons) in Creative Digital Media where he is lead in the delivery of the Multimedia Stream. He is currently exploring serious games for construction-related training as well as the opportunities transmedia provides in improving user experience and engagement in cultural archive artifacts. His educational research is currently driven by his interest in self-determined learning (heutagogy), rhizomatic learning theory, micro-credentialing /digital badging, and curriculum development.
Read more about Shaun Ferns

Sinéad Murphy
Sinéad Murphy
author image
Sinéad Murphy

Sinead Murphy is currently Data Analytics Manager for the Irish NGO Trocaire. She has over 25 years of computing experience, including freelance IT training and database consulting, university lecturing in mathematics, IT skills and programming at TU Dublin (Ireland) and Middlesex University (London). She is a published academic, with undergraduate and postgraduate degrees in mathematics, computing and data science. She is passionate about the use of IT for understanding and visualising data, and using that understanding to make meaningful differences in the world. She is currently exploring the use of Python and Unity for data analytics and interactive visualisations.
Read more about Sinéad Murphy

View More author details
Right arrow

Particle Systems and Other Visual Effects

While a lot of content in scenes is from terrain and 3D meshes, and other effects from light sources, some fantastic effects can be created from Particle Systems. Particle systems are basically a collection of points whose creation changes over time, and whose lifetime can be controlled through parameters and rules. Also, how each particle is rendered can be controlled over time, sometimes using colors and other times using sprite images. Effects such as gravity and collisions with surfaces can be applied to make it more convincing to the player that the particles are part of the scene and interacting with other scene objects.

Some possible effects of Particle systems include:

  • Fire, explosions, smoke, and sparks
  • Water drops, moving water, and waterfalls
  • Other effects such as dust, fish, glowing effects from magic potions, and so on

Figure 15.1: Particle systems used to great effect in the Witcher computer...

Exploring Unity’s Particle Pack and reusing samples for your own games

Unity has published a great demonstration of Particle System effects as a package in the Asset Store. While it’s fine to explore the samples, by looking at the GameObjects in the scene and the asset files in the Project window, we can exploit these free resources and adapt them for our own projects.

In this recipe, we’ll explore the sample scene and create two versions of a new scene. One will give the effect of fire on the ground, while the second will look like a flaming torch attached to a concrete wall. In both cases, we’ll make use of fire Prefabs from the Unity examples.

Figure 15.2: Part of the Unity Particle System demo scene

How to do it...

To reuse Unity particle examples for your own games, follow these steps:

  1. Start a new Unity 3D project and ensure you are logged in to your Unity account in the Unity Editor.
  2. Open the Unity Asset Store...

Creating a simple Particle Systems from scratch

Making use of Prefabs from Unity, as we did in the previous recipe, is fine if we’re happy to use such Prefabs without any tweaking. However, we often want to adjust the look and feel of assets and Particle Systems so that they fit in with the style of a particular game or scene. Therefore, it is useful to learn how to create and customize Particle Systems from scratch in order to learn about the different parameters and modules that make up Unity Particle Systems. Knowing how to adjust these values means we are able to either create what we need or customize a Particle Systems prefab from a third-party source, such as the Unity examples.

In this recipe, we’ll create a Particle Systems from scratch and customize it in several ways to learn about some of the most important modules and parameters:

Figure 15.6: The scene we’ll create in this recipe with white, red, and yellow particles shimmering around...

Using Texture Sheets to simulate fire with a Particle System

Much of the work of creating visual effects can be achieved if we have a multiple-image sprite sheet. Then, the visual form of each particle, as it changes over its lifetime, can be driven as a 2D animation, looping through the sequence of images from the sprite sheet.

In this recipe, we’ll create a flickering blue flame effect using a Particle System that animates its particles from a multiple-image sprite sheet from the Open Game Art website:

Figure 15.14: The flickering blue flames Particle System we’ll create in this recipe

Getting ready

For this recipe, we need a sprite sheet with multiple images on a black background. Either use one of your own or use the fire2_64.png blue flame sprite sheet from Open Game Art. Thanks to Ben Hickling for sharing this asset for game developers to use.

We’ve prepared the file you need in the 14_03 folder. Alternatively, you can download the...

Making particles collide with scene objects

A great way to enhance the visual effects of Particle Systems is for the particles to behave as if they are a real part of the 3D world. We can easily give this impression by enabling the Collision module, which makes particles change their direction of movement when they “hit” the colliders of 3D objects in the scene.

In this recipe, we’ll create a scene containing some 3D objects and create two Particle Systems – one like a fountain of ping-pong balls and another like a gun firing many small balls in a line. Both sets of particles will bounce and change direction when they collide with the objects in the scene:

Figure 15.19: The two bouncing Particle Systems in the collision scene we’ll create in this recipe

How to do it...

To create a Particle System from scratch, follow these steps:

  1. Create a new Unity 3D project.
  2. Move MainCamera to Position (0, 1, -20) so that we...

Simulating an explosion

For many games, there will be events where we want to display an explosion in the scene, such as when the player’s character dies or when they collide with an object that deals damage. We can quite easily create a visual explosion effect using an appropriate 2D image and a Particle System that fires one with a burst of many fast-moving, short-lived particles, in all directions. That’s what we’ll create in this recipe.

Figure 15.24: The explosion effect we’ll create

Getting ready

For this recipe, we need an image of an explosion, so we’ll use the firecloud.png image that’s been published on the PNG-EGG website. Thanks to GAMEKILLER48 for sharing this asset for game developers to use.

We’ve prepared the file you need in the 15_05 folder. Alternatively, you can download the image directly from the PNG-EGG website: https://www.pngegg.com/en/png-ffmwo.

How to do it...

To create a Particle...

Using Invoke to delay the execution of an explosion

In the previous recipe, we wanted to immediately instantiate and play a Particle System as soon as the player’s character collided with a crystal GameObject. However, there are times when we want to delay for a few moments when we want a prefab to be instantiated. This recipe customizes the previous one, in that we’ll allow our player to move around the scene while dropping bombs. Three seconds after being dropped, that bomb will be replaced by an explosion – an instantiation of our explosion Particle System from the previous recipe. This recipe is inspired, although in a much-simplified form, by games such as BomberMan.

Figure 15.30: Our Unity BomberMan

Getting ready

This recipe follows on from the previous one, so make a copy of that and work on the copy.

How to do it...

To delay an explosion using the Invoke(...) method, follow these steps:

  1. Open a copy of the previous recipe...

Using a Line Renderer to create a spinning laser trap

Interesting visual effects can be achieved through a scripted approach by creating Line Renderers. Unity Line Renderers can draw lines in the scene using assigned materials and their colors and Alpha Transparency. Lines can be drawn for many different reasons in games, such as to simulate laser beams, provide floating arrows to give the player hints on the direction to follow, and so on.

Figure 15.35: The rotating cube with the laser being emitted

In this recipe, we’ll create the effect of a rotating 3D Cube emitting a laser beam for a short distance. We’ll also learn how to create a scaled invisible cube in the same place as the laser, whose Box Collider can be used for collisions (as if they were with the laser).

Finally, in the There’s more... section, we’ll learn how to add these effects to the scene from the previous recipe to then create explosions when the player’s character...

Further reading

You can learn more about Particle Systems at the following links:

Learn more about Line Renderers in the Unity manual pages:

An exciting package now out of preview is Visual Effects Graph. This offers powerful ways to create stunning visual effects in Unity projects. Learn more at these links:

Learn more on Discord

To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below:

https://packt.link/unitydev

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity Cookbook - Fifth Edition
Published in: Nov 2023Publisher: PacktISBN-13: 9781805123026
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (3)

author image
Shaun Ferns

Shaun is a lecturer at Technological University Dublin. He is currently teaching on the BA (Hons) in Creative Digital Media where he is lead in the delivery of the Multimedia Stream. He is currently exploring serious games for construction-related training as well as the opportunities transmedia provides in improving user experience and engagement in cultural archive artifacts. His educational research is currently driven by his interest in self-determined learning (heutagogy), rhizomatic learning theory, micro-credentialing /digital badging, and curriculum development.
Read more about Shaun Ferns

author image
Sinéad Murphy

Sinead Murphy is currently Data Analytics Manager for the Irish NGO Trocaire. She has over 25 years of computing experience, including freelance IT training and database consulting, university lecturing in mathematics, IT skills and programming at TU Dublin (Ireland) and Middlesex University (London). She is a published academic, with undergraduate and postgraduate degrees in mathematics, computing and data science. She is passionate about the use of IT for understanding and visualising data, and using that understanding to make meaningful differences in the world. She is currently exploring the use of Python and Unity for data analytics and interactive visualisations.
Read more about Sinéad Murphy