In this lecture are we going to create some particle effects so that when the ball hits block it triggers
this neat little particle effect in the process we will learn about instead of through code.
Somebody gets started.
We've already learnt that we can instantiate more game objects from a prefab for example our block here
is a prefab Flem we find the prefect folders in C Block.
If I drag that into my scene then we instantiate or create an instance of a second block.
I can also do that by creating into my hierarchy.
If I just go and put these blocks where this post to be so there's a look underneath blocks and one
more block underneath a block there.
I can also click in here and control D or komenda from a hierarchy that will create another block in
exactly the same place.
And then I can move it to a different position and I can grab all of these go comen or control de angry
create a whole bunch then move them up.
Now I've got four of them.
So now we're going to do the same thing that we did just there but we're going to do it in code so that
we can instantiate something at a very specific point in time.
What we do is create a particle effect when we Clower block or destroy our block.
The particle effect is instantiated and then after a short amount of time we destroy it so that it's
not hanging around in a hierarchy.
So what I've created for you is a little simple particle effect called Impact VFX and it's as a package
if you can download that now it's against this lecture unzipped that and then drag it into your prefab
area.
And you'll get the dialogue that says you want it import this.
Yes we do.
We click on the import button that might be right there on the bottom for you.
We import it for that important then you should see impact the effects in your assets folder down the
bottom.
Now if I drag this into the scene just any old place you can see that it's going to play straight away
in a fire.
I now have this little particle effect dialogue box that pops on and click play and plays it again a
little bit odd because there's some orange outlines around it if you're finding that got to gizmo's
and then let's see what that is.
It is the selection outline I believe.
Let's see if that works for play.
OK.
Now we don't have that selection outline around it so it's just a really simple particle effect that
I've created for you.
Just that all stars that are going sparkle and will do a whole bunch more particle conversation later
on and we've created this particle system there's a whole bunch of knobs and dials and levers to twiddle
here if you're excited about that you can jump in and start playing around with it.
Otherwise in later lectures will spend a lot more time explaining what particle effects are and how
you can create your own finalities using the one that I've created that looks like blob like that will
trigger that when a block is destroyed.
Now if we just drop that into our scene we don't need it in the scene so I'm going to delete that effect
that I just dropped in my scene the way that from the scene where we only create this as part of our
code but we do need to have it in our prefab area right here.
So let's go ahead and open up our script we need to open up the blocks block script and said blocked
script.
Now it's just one it's just block lock script.
Let's start at the top here we need to say what is the particle effects or what serialise field and
there's B of type game object because our particle prefab is a game object and we will call it block
what we call block Sparkle's can like Sparkle's the f x visual effects it stands for right sort of serialise
that will hook that up when we go back into unity back in the inspector and under destroy.
BLOCK This is what's happening when things get destroyed or create a new method under here that's going
to be private void.
What we want to be doing here is saying it's triggering this is when we treat our particle effects so
we'll call this trigger Sparkle's.
F X.
Right now in trigga particles VFX What do we want to do.
I've got some unity docs for us to have a quick look at.
I've given you the link against this lecture as always.
And this is our instantiates function satiate method that we can call with a little description down
here that says the function makes a copy of an object in a similar way to duplicate in the edit.
This happens at runtime runtime is when we click play the game is running and that's at run time.
So these things happen at run time and there's a few things that we can specify about what we want our
instantiated object to do.
If we scroll way at the top you can see there's one two three four five or so lines.
Sign that we have a few different options for how we call this method or call this function the very
first one to say instantiate and then object original.
Basically they're saying instantiate or create something and then what is the name of the object of
the game object or the prefab in our case that we want to create and that's it.
You can do that if you want to but we have some other options if we want to say where thing where it's
going to be are instantiated and these are called overloads these having multiple different options
or multiple different signatures.
This is a signature here.
This is a signature signature means what parameters are we going to put into our function.
So it's the same function essentially.
But it has a few different ways that you can do it so well it just once again these are overloads and
these overloads are having different signatures because we have different signatures they do slightly
different things.
So we start off by typing game object and this is a variable of type game object we'll call Sparkle's
and this will be instantiate.
Okay this is our our method here.
Instantiate.
When I do my open parentheses you can see all this stuff coming up on our screen here.
This is showing us that there's 10 different overloads 10 different signatures that we could use for
instance shape.
The most simple is just to say what's the name of our particle we created.
BLOCK Sparkle's of the f x.
Sorry say block Sparkle's VFX and that's it.
Semicolon.
So that's the simplest of our insentient you can just instantiate something and just say make this.
That's all we're doing.
So that's one of our signatures we can do.
The problem with this is that if we instantiate this into our scene now it will the position it will
insentient it will be wherever our particle effects think that it should be.
So our prefab just going to very quickly dropped this in though.
Undo it.
It looks like it wants to be roughly there on the screen.
And that's based upon wherever it was created or wherever the prefabs thinks that it's prefabbed position
should be.
Now the way that we get control is let's just go back briefly to the unity doxey we look at saying the
instantiate but also feeding in passing in the position of the object and the position the way we're
going to do this here is to say comma transform dot position.
And now also wants transform Dort rotation to a saying transformed composition position is basically
saying here where you are at the moment and here where you're rotated at the moment.
Nice simple straightforward.
If I save that I now have your first challenge.
That challenge is to connect the particle prefabbed the block prefabbed so to make sure that the block
knows when it gets destroyed.
This is the particle that we should be spawning and they call our method which instantiates the particle
effects whenever the block is being destroyed.
That is when we want to call our particle effect.
So jump in and take on that challenge.
Now this is challenge one of two for this lecture.
So take that on and be back with you in a moment.
First of all ever into unity.
Let's connect up now because where we have a block in our block E is spawning a particle that is a prefab.
Then we can go straight to our block prefab whereas our block pretty heavy block prefab and we can say
the block sparkles VFX is going to be our impact VFX and drag that on over.
So that means this prefab is spawning that prefab.
So wherever we place this block It knows it's pointing to this particular prefab particle effect down
here.
Excellent.
Now the other step we need to do is actually call owl trigga Sparkle's VFX and do that just in destroy
block here so where shall we do this.
So are going to say trigger Sparkle's the effect not passing anything in semicolon.
Now as I mentioned this is getting a little bit unwieldy so let's see what we can group together here
we've got something to do with audio something to audio and then destroying so I will highlight all
of these click extract method.
This is the p.c way I showed in a previous video how to do the Mac way it's very very similar.
What are we doing here.
We're finding audio.
So this is playing playing brake sound so play a block destroy.
SFX sound effects.
Okay that is nice and clean now just getting rid of all of this heavier looking code.
Now we can see we can read through.
We can do our human readings to see what's going on there.
Let's save.
Make sure this all works.
Come back over.
Ok.
Click on Play.
Make sure that wherever on our game screen I just spent the last minute or two wondering why am I seen
view wouldn't play the game properly.
Okay there we go.
Pretty quick.
OK one more time let's make sure that the particles are spawning right on top of the blocks will see
if I can actually accomplish.
They're right on top of the blocks.
Now I'm going to play for a moment.
As you can see over here in the hierarchy we've got all of these impact VFX starting to be spawned something
to be instantiated but we're not ever getting rid of them.
It's okay in our game I don't think it's going to become a huge issue but in a bigger game we don't
have all of these things filling up our how our hierarchy becomes a bit of a mess.
We can't see what's going on and it's a little bit less efficient.
So what we'll do.
E is your second challenge and that is to destroy our effect so destroy the panicle instance after two
seconds.
We've done the story a couple times we've done destroy with a delay a couple times so that should be
fairly straightforward for you.
So jump in and destroy our effect after two seconds.
OK.
So underneath trigger Sparkle's VFX where instantiating we also want to destroy down here so we type
destroy put in the name of the variable of the game object which is Sparkle's in this instance colmar
an arm you're say one if so two are destroyed after one second can save back over into unity.
So look to see if they're created over on the right here and also destroyed after an amount of time
like they are.
Okay call me having a hard time playing in the screen at the same time but it looks all good.
Okay so we've now got ourselves in a situation where we can instantiate and also destroy objects that
we've created in our hierarchy through our code.
Very excellent good work.
I'll see you in the next video.