When we built the beginnings of fireworks in class, I found myself endlessly tweaking them to make them “pretty.” Although I’m currently into game design, I knew the firework aesthetic struck a chord in me more than the pong game so I decided to build on it. I have also been longing to play with particle systems in Unity for a while and this gave me the excuse.
The first thing I added were the sparks shooting off of the cube/rockets. This required very little coding because I simply attached the particle system to our prefab MasterBlaster we built in class. For anyone reading this not in my class, we built the code for the cubes/rockets and the ribbons trailing in class. The code is attached below:
Since I attached the particle system to the game object, I knew already it wouldn’t fire without hitting the space bar. I must admit, my first thought was to add smoke, not sparks, BUT when I made the particle system a component of the MasterBlaster, I was essentially tying it to the cube. SO my first attempt at smoke looked awful because the particles followed the front of the rocket instead of being where in my head it “exploded”. I decided to first try playing with the particle system component controls before dealing with my smoke issue. It turned out that after making the particles very small and changing their color, they made lovely sparks so I decided to keep that. In fact, the sparks are my favorite touch and it was completely stumbled upon.
To the left are the settings for the spark particle system. I didn’t really get too in depth on this one. The only thing not pictured is the renderer which I changed nothing in the settings from what would be set in the default. I was shocked at how little I had to change to make it quite pretty.
Then I moved onto the smoke. I glanced through enough tutorials to know that the particle system can be used for a lot! Flames, smoke, explosions — really all sorts of things. I found this one tutorial by Gabriel Martino on youtube. Here’s the link if you’re curious: https://www.youtube.com/watch?v=BHdbBtGgfb8. I actually never made it through the full tutorial because it was more for creating real explosions, but I wanted to get a better feel for changing the images of the particles and what can be done with them. I built my smoke using one of the material assets Gabriel supplied a link to so that was a huge help. And my manipulation of the smoke would not have been possible without his tutorial. The only reason I didn’t finish it was because the explosions were a bit overkill for fireworks, but it is definitely something I will go back to in the future.
There were a ton of settings I changed to make the smoke. This was vastly different than the sparks. I played with things shifting on curves. A color gradient instead of simply changing the color. I changed the material asset as well. It was actually amazing how many different ways you could change it. And the tutorials I watched on flames before were pretty interesting even if I didn’t use them.
I added rotation and played with different speeds at different times. And I learned to work with curves over the lifetime of the systems.
Now I had smoke that looked quite good — although I must admit, I could be tweaking this forever. It is quite fun once I started to muck about with the controls. BUT the particle system is also very hard to control with code — at least for me. I still had the initial problem I had before I switched to sparks. How do I make the smoke only fire when the spacebar is hit — aka when the firework explodes? My first attempts were to borrow the code from the cube and just make it for a particle system. That didn’t work.
I went online and googled it endlessly. I came up with a ton of different options that all remarkably did not work. A lot of it was because the language/libraries had changed with the recent updates to Unity. The logic made perfect sense. I made the particle system a Boolean with an initial value of false. Then tried to switch it open hitting the spacebar and then make it delay before turning it back to false. No version I initially came up with worked. I ran my issues by my TA for my Narrative Game Studio class, Wen Shi. We are not working in Unity for that class, but she’s an MFA gaming student and a programmer so figured I’d ask if she had ever run into this problem. After trying four different code options online that all failed, I’d take any insight I could get.
Wen’s response was she never worked with the particle system before and could not understand why my code wouldn’t work. Sigh. Oh well… Then she came back with another option. Wen gave me the advice of making a public GameObject and attaching the particle system to that. If there’s some weird thing with Unity that makes it hard to code directly that might work. She also warned me that this is a terrible way to code for Unity, but if it works, it works and I should go for it. With her help, it finally worked! Here’s the final inelegant piece of code:
Here’s a little video of the fireworks: