Oh cool.
Gameplay is now pretty much there, but our game still feels a little bit dull because our enemies and
our player ship just disappear and blink out of existence whenever they get destroyed.
So in this lecture, let's add some explosions with some particle effects.
Now, whenever I'm working with the particle system, I do like to have some kind of reference to work
from be that from looking at other games or past projects or even just drawing something down on paper.
Now, you don't have to follow exactly what I'm doing in this lecture, so feel free to make this explosion
entirely your own.
And rather than setting a challenge for this lecture, I encourage you to pause the video frequently
to play around with this effect rather than just copying the numbers that I'm going to be using.
Now, the first step to creating our particle system is to add it to the hierarchy.
So in our hierarchy, let's right click, go down to effect, and then add a new particle system.
And this currently looks nothing like our end result.
And the first biggest thing that I'm noticing is that we're currently spitting out these fuzzy particle
balls rather than our nice star shaped particles and the changes we need to look at changing the particles
material.
We haven't created this material yet, so let's go ahead and do that first.
If we head into our asset packs and then space shooter, I've imported this explosion styles sprite
sheet.
If you want to follow along and use the same material as me, then this is attached as a resource to
this lecture.
So go ahead and download and import that.
Now once you've got a spreadsheet you want to use, the next step is going to be to make it into a material
that we can apply to our particles.
And we haven't really dealt with materials too much so far.
So to create one, let's right click in our assets folder, go up to create and then down to material.
Let's call this material M underscore explosion.
And whenever we create a material, it will have a shader which tells the unity how to render it.
If we look up in the inspector, we can see under the shader dropdown that these come in as standard
by default.
But since this is a particle system, we're going to want to change this.
So if we click on our dropdown, head down to particles and we're going to use the standard unlit shader
next, we're going to want to change the render mode from opaque down to additive, and that will just
make the particles appear correctly when we start using them.
And now we need to attach our sprite sheet to this material.
So let's grab our sprite sheet and drag it into the little box next to Albedo.
Under the maps, our material is now set up for what we need.
So let's now go into our political system itself.
Scroll right the way to the bottom under renderer.
And we can drag our new material into the material slot.
We can see that this has now changed our particles to be our sprite sheet, but we don't want the entire
sprite sheet here.
We just want to loop through each individual cell within that sprite sheet and we can do that with some
texture sheet animations.
So let's enable our texture sheet animation.
And if we open this up, we're going to keep the mode as grid.
And for the tiles, we're going to set this to three by one because our sprite sheet is three wide and
one tall.
The changing is quite slow though, so let's bump up our cycles to maybe two.
So this one I'll loop through the sprite sheet twice and rather than always starting on exactly the
same particle, what I'm going to do is I'm going to select a random one to start with in the dropdown,
let's pick a random between two constants and start to anywhere from the first to the third.
So between zero and two.
And now we can see we're getting a little bit more variation with that set.
The next thing I want to do is to amend the colors because they're all white for their entire lifetime
and it's not very fiery or exclusionary.
So let's go up to color over lifetime and then open up our color picker.
And I've actually already got a nice explosion type effect that I like to use, but you can go ahead
and create whatever kind of explosion you like.
You might have purple explosions or green explosions or really anything that you want to show you how
this one is set up.
Along the top, we have various levels of alpha and this will decrease the alpha over time as it goes
through its fire and smoke cycles until eventually petering out entirely along the colors.
We start off with a nice hot yellow color before we go into oranges, orange reds, and then we go into
a reddish gray color for our smoke.
This is now starting to look a bit more like fire, but it's very much the wrong shape and going in
the wrong direction.
So let's look at our shape next.
It's currently set up to be a cone, but we want it to explode out in a circle.
So for our shape, let's click on circle.
And this doesn't really do what we want.
It's now spreading out on a line, but this is a circle.
It's just being rendered in three D and the rotation is incorrect.
So at the very top of our political system, let's zero out the rotation and now we have it generating
in a circle.
While we're here, let's also zero out the transform because that really should be zeroed if we're going
to prefab this later on.
Next, let's try and constrain this explosion so it doesn't spread out quite so far for this.
The first thing I want to do is change the start lifetime and explosions are quite quick, so let's
do a random between two constants and have our particles die out between 0.1 and 0.5.
And with that small change, we can now get a better sense of that hot fire from the explosion and then
the smoke spreading out.
Our particles are all oriented in exactly the same way, though.
So now let's look at giving it some rotation.
So under our start rotation, let's again go with a random between two constants and go from zero degrees
to 359 degrees.
So a full circle.
And we're also going to come down to rotation over lifetime and give it an angular velocity of around
180 degrees.
This allows our particles not only to be instantiated at a random angle, but also it gives it some
rotation as they travel.
So this is looking okay, but it is very sporadic and not very explosion yet.
So now let's try and amp up the number of particles we've got and really condense them into a quick
explosion.
Up in our emissions, let's increase the rate over lifetime to say about 100, which is definitely giving
us a more sparkly effect and to tighten up the radius a little bit.
Let's go down to our shape and change the radius of our circle to maybe around 0.5.
This has brought us a good step closer, and the next thing I'm noticing compared to our reference is
that our particles are always exactly the same size.
So down in size over a lifetime, let's enable that.
And in our curve, let's flip this line so we start off big and eventually peter out to zero.
This has drastically changed the look of our political system.
So we now have a hot fire in the middle and these wispy smoke pieces around the edge.
But to get it looking a little bit better, let's change the start speed so all of our particles aren't
moving at exactly the same rate.
Doing this will allow for a little bit of overlap in our speed and we'll have some bigger pieces of
smoke fly out to start with.
Let's try a random between two constants and maybe try somewhere between zero and two.
And again, this has made quite a big difference, but it isn't quite what we want.
And this is because when we do a random between two constants, the actual random numbers that get generated
are evenly distributed between the upper and lower bound.
But we really want more of our particles to be on the quicker end of things.
To do this we can use a curve.
So let's select that.
And for this curve I'm going to want a nice ramp between zero and two.
This means that we'll have more particles being created with a speed closer to two, and as we head
down the curve, we'll end up with less particles at the slower speed.
The final step now is going to be to change the duration of our particle system, because if we turn
off looping and then restart our particle, it's existing for a very long time and explosion should
be very short and sharp.
So with looping turned off, let's set our duration down something nice and snappy, maybe around 0.2.
And if we restart our explosion now, we get a nice short, sharp explosion.
And with that final change, I'm pretty happy with the end result for this particle system.
For now, it's pretty close to our initial reference and it looks quite nice.
So with that set, let's go ahead and prefab our political system.
And rather than calling it the particle system, let's call this the explosion effect.
And we can delete that out of our hierarchy.
Next, we want to instantiate this explosion whenever one of the ships is hit.
So if we take a look at our script and think about where we could do this, the damage is being taken
in our health script.
So it'd be a good idea to have our health and our particle system linked in some way.
Now, for this game, we're only going to have the one explosion effect.
So I'm actually going to instantiate it directly into this health script.
But if you decide to expand on this game later on and add more effect in other parts of your game,
then it may be worth creating a brand new script that holds all of these special effects and then link
that to the various scripts that need to call them.
So in our Health script, let's create a serialized field for our particle system, and we're going
to call this the hit effect.
And we want to instantiate this particle effect whenever an object takes damage.
Now, we could place this in our take damage method, but rather than muddying that up, let's create
a new method called play hit effect.
In here, we're going to say that if our hit effect does not equal null, so if we've attached a hit
effect that we want to play, then we want to instantiate that effect so that we can destroy it later.
Let's store this in a temporary variable.
So for this one, we're going to need a particle system called instance, and then we can go ahead and
instantiate our hit effect at the transform dot position.
So at the position of the thing being hit and because we've included a position, we need to include
a rotation of Quaternions identity for no rotation.
Then to destroy the particles, we can say destroy the instance game object because the instance is
currently just the particle system attached to a game object and for the duration we can get the duration
of the particle system by saying instance dot main dot duration, and that will be the duration that
the particles are being created.
But we also want to include the lifetime of the particles so we can add the instance dot main dot start,
lifetime dot constant max and you'll see a few different options here.
And the one you select depends on how you set up the particle system.
So we've got a minimum and a maximum value, but if you've set up a curve, you'll have to worry about
animation curves and things like that.
For this one, though, constant Max will do the job.
Finally, we need to call this method from somewhere, so let's take our hit effect.
And if we've dealt damage, we're going to take damage.
We're going to play our hit effect and then we're going to tell our damage data to processes hit method.
So let's save this up and test our immunity.
Let's first of all, link our explosion to both our enemy and our player.
So let's click both of those in our asset folder.
Scroll down to our health script and then drag over our explosion effect.
And then we can go ahead and hit play.
And our particle systems are now being created.
But if we notice, the particles are currently behind our ships.
So have a quick think about how we might go and fix this.
Okay.
I'm not going to give you too long.
If we jump into our explosion effect, scroll right down to the bottom where we have our renderer,
because this is a problem with the sorting order.
If you recall, we're currently working on a rough system of having our projectiles on layers zero,
our ships on layer one.
So for our particles, let's change our sort order in layer two to.
If we save our prefab and then test again.
We can see that the particles are now appearing on top of our ships, and when ships get destroyed,
they go out in a bit more of a blaze of glory.
So congratulations on creating your explosion particles.
And in the next lecture, we're going to juice our game even more by adding some camera shake when the
player gets hit.
So I'll see you there.