Welcome back, My favorite game developers.
And in this video, when we run the game,
we are still breaking the part,
but you'll notice that after disappears,
we have three parts.
Part 1 on the ground being destroyed.
We have a part 2 and part 3 and another part 3.
So we are instantiating and broken parts when
an object or a breakable bond
breakable object is destroyed.
So let's not waste any more time.
And let's get started.
Oh, case.
So time to make some broken pieces.
The first thing that we're going to do is I'm
going to drag this broken piece right here.
And where is it?
Double-click of k.
There we are.
And let's make sure that it's on the player layer.
And I think we should change this to
have the level objects and not the player lives.
But for now I'm just going to
keep it as it is not worth about that.
Obviously, we should change it,
but we're not going to do that right now.
I'm going to add a 23.
So now we have three pieces.
I'm going to select all of them and
make them all on the player.
There you go, the PS2 and the bees.
Three, I'm going to save that.
Before I move on.
Let's change this into the Baroque and part one.
Copy that broken part
2 and the broken part 3. There we go.
Now, I'm going to go into
the Prefabs folder and I'm going to right-click
and create a new folder for the broken parts.
Open it up. And I'm going to add them one by one.
If we select all of them and drag we, it doesn't work.
So I'm going to drag one after the other.
So now we have the three broken pieces and if you want,
you can make something even better.
So we can add, for example, the third piece.
We can rotate it around.
So now we have this piece right here,
we rotate it around.
And this can be called,
for example, the piece for, if you want,
I recommend you use your imagination when
in doubt with the pieces that we have.
I'm going to select these three and make
sure that their position is at 0.
Because for example, if we have,
let's say this as a six, Okay,
We add them at six or have them all at six
if I add them so I instantiate them.
You can see that it starts off at
the position 6 not on 0.
This will cause problems.
So I always like to keep it at 0,
even though when we instantiate
it at a certain transform position,
it will take the position that we tell it.
But anyways, let's continue on.
I think you know what I meant?
I'm going to delete this broken pieces.
I'm going to open up the breakable script if I find it.
And in here we are going to first of all,
obviously get a reference to the broken part.
So I'm going to create a serialized field of
type game object and call this the broken part.
Save that.
And then before we destroy the game object,
I'm going to go ahead and instantiate
broken part at transform.py
position and transform the rotation.
Save that.
Let's go back in our game.
Right, for this, compile any day now.
Okay, So I'm going to lock this down.
I'm going to go into
prefabs and select the broken pieces.
And let's just add the first one,
save that the game collide with the object
there go at breaks and we should be
left with this piece right here.
Okay, Very nice.
Now it's not disappearing.
We'll handle that in just a bit.
But for now we have the pieces breaking.
But obviously, obviously we want to have
multiple pieces breaking and
appearing one after the other.
So this is going to be your challenge,
your challenge to instantiate all the pieces.
So first of all,
get all the pieces from
the sprites we've already done that.
Create an array of game objects for
the broken parts looped
through the pieces you using the for loop.
So we've already learned how a for loop works.
You'll need to generate
a random number every single time.
And we'll need to use the number to
instantiate a broken piece.
Now you might be thinking, well,
this means that some broken pieces will be done twice.
And that's okay.
Honestly, I think it's not something
very bad to worry about.
If you want, you can go even
deeper into this and instantiate
a random angle for the pieces to have so
that they may be appear
different from one another and don't.
Set on the same place.
Now, of course there will be a small hindrance here.
We'll talk about it once you do the challenge.
So pause the video right now and go do the challenge.
Oh, come back.
So before we destroy the game object,
what we're going to do is we're going to,
and I think that this is not
the perfect place to do this.
So instead of instantiating in here,
I'm going to go ahead and move this up in here.
So after we start the animation,
or you know what?
This is also a problem.
So I'm going to make this a bit bigger.
And after we start the animation,
I'm going to instantiate the pieces if you want.
If you want, you can also create
a separate method and call it using the events.
But for now, let's go ahead
and do the things that we wanted to do.
So first of all, we are going to turn
the game object into an array,
and the broken part double-click in here.
And this is going to be the broken parts apply that.
I'm going to comment this out in order
to see everything in the inspector.
So breakable pots scroll down,
close these off because they are annoying.
And what do we have here?
The broken parts.
Lock this down, select all
of these and drag them in here.
So now we have the three broken parts ready for use.
I'll go back in here.
And what are we going to do?
So first of all, we are going
to loop through all the parts.
So for I'm going to initiate at r equals 0.
So I hope this wasn't too hard.
Let's start again.
We start off by creating the variable.
We started with a 0.
Then what we're going to check,
or we will keep looping.
While I is less than the broken parts dot,
length and length because we are using arrays.
If we're using lists,
we use count and every single time we increase i by one.
So we'll need to generate three pieces,
and we want these pieces to be random.
So we have a variety when breaking up pots.
So int, it's an integer.
We're going to call it the random broken part.
And it's going to be equal to random dot range.
And what's the range?
The range is from 0 into the broken parts dot length.
There we go.
Now after we do that,
we are going to move the instantiation up.
And inside of here,
instead of simply the parts,
we are going to use the part with the random broken part.
So I'm going to save all of that.
Okay, so let's go ahead and test this code out.
So I'm going to go back in here.
I'm going to see what else do we need to do?
I think everything is good.
Run the game. Now we walk up to the bot, does not break.
We tried to destroy it.
There we go, it gets destroyed and there we go.
We have some pieces are no,
maybe I should not have maximize on play.
Again, stop, maximize on play.
Let's make this bigger.
We want to see the hierarchy.
We want to know what's happening.
Okay, so I run into
the part that gets broken up. So there we go.
We have broken part two, broken part 1,
and broken part two.
So I can move this just a little
bit and the broken parts are on top of each other.
So this is why I recommended
that we create a bit of rotation.
So with that done,
let's make sure that we stage everything in here.
We stage of that.
And I'm going to call this, now,
we instantiate broken parts.
When we instantiate broken parts,
one pots are the stroke.
When bots are destroyed,
save that, commit the changes.
And I hope you are enjoying,
I hope you are understanding everything.
And in the next video we are going to make sure
that the broken pieces don't just stay in place.
They spread out as if this
has been destroyed and we have some kind of impact.
So I'll see you then.