So we want to show broken pieces when a box gets smashed.
So in our art file folder even in our objects we have a whole bunch of broken pieces here.
I'm just gonna do one for the moment so bring one in and we can just see it there.
I'm gonna call this broken piece one nice and simple and we're just gonna leave that as it is for the
time being.
But here with box actually what we're gonna do I'm gonna make this a prefab so the prefabs uh create
a new folder for breakable is breakable is and
area and create a folder for the box.
So in here we're gonna put the broken piece of the box there.
I'm also gonna put a prefab of the box itself.
Okay so we got a broken piece on our box then on the box we're obviously going to want to instantiate
this broken piece.
So let's go into our breakable script.
We're gonna either reference to that piece so public game object broken piece
and whenever our object gets our object gets destroyed we're gonna say instantiate broken piece s transform
position and transform dust rotation.
Okay.
Save that.
Let's go back in now I've got a slot for the broken piece so I'm going to add that prefab in there.
So let's go back to our previous folder.
Well at that broken piece in there and if we pray play we get the piece appearing so that's OK if we
add multiple boxes
like this
if we go ahead and roll to them all they all leave the exact same piece in the exact same position.
So straight away you think that's not ready Grace.
That's that's more variety to that.
So let's add some more pieces.
So I'm going to take this broken piece here and we'll select where the images like this and I'm going
to then have a prequel two three four five and six.
So then I'm going to copy just the name of this copy of property.
There we go.
So then we have broken these two probably story for five and six with prefabs at all these is important
to note when you're making prefabs you have to drag them in one of the time you cannot multi select
them and drag them in.
I do one prefab by the time case we've got a good few pieces here now.
Let's.
With all these sections it's a pop in there that's randomly picked between them.
So we know how to do that.
We know that we can make an array of broken pieces.
We can randomly select it random piece equals random that range between zero and broken pieces does
length and actually just looking at that.
We're going to change this to broken pieces so I'm going to his control are and then at the end of this
I'm going to add an s and then hit enter and that replaces broken pieces down here as well for us.
Nice and handy.
So random that range between zero and the length of our broken pieces and then we're going to instantiate
broken pieces from the broken pieces array the random piece number like this will save.
And then if we jump back into our game got our broken pieces here and look to our box and other broken
pieces and I'm going to add all six of these actually the easiest way to when we have multiple pieces
to add in like this is I can lock the Inspector here.
So now this is locked so that no matter what other objects I select we still have the box being shown
here.
And that's really handy because then I can select all of these prefabs and just drag them over and drop
them in here.
So now we have broken piece.
One two three four five six.
So that's fine.
I need to remember to unlock that after you do that as well.
We'll apply those changes now all these boxes will randomly pick from those.
So now we can go over here and if we smash them.
OK.
So we're starting to add a bit of Eurasian that's looking a little bit nicer.
But we could make it better and that we can make it better and just wanted one random piece been broken
from this crate.
What if we randomly selected a few objects so we could say hey for every box randomly break a few elements.
So let's go back into our scripts let's say we'll add a variable up the top here for public int Max
pieces and I said that by default be a tree.
So you know one set of three five so to be a maximum of five pieces that will make out.
So what we can then do is obviously we want to make we want to randomly side how many pieces are we
gonna drop.
So let's say here
it pieces to drop equals random that range between zero and maximum pieces.
So somewhere between as you know we've set this to be 1.
So there always be at least one piece falling out.
So somewhere between one and five pieces will pop out every time we break that little little box.
So then how do we know here how many pieces the drop.
Well for that we're gonna need a new element that we haven't encountered just yet.
Which is a for loop.
So a for loop is something that will check if something is true for a certain amount of turns basically
and then keep carrying out the same bit of code over and over again until at a certain target is reached.
So how we set up a for loop is there's three simple elements to a for loop.
So we say four and then in a bracket here the first element is we set a number to start and add our
tracking that we're gonna do in our fall.
So for example what we're going to track is is this number that we create here.
Less than however many pieces we want to create.
So say for example it randomly selects three pieces are gonna drop.
So we're creating a number here.
So we'll say for int i equals zero.
So we're creating a new int called I started out at zero.
Then the next thing we do is do a check.
So we're basically doing an if statement in here.
So we're checking if i is less than pieces to drop and if that is true then we'll do a bunch of code
inside these curly brackets here.
So a bunch of code will run in here and I'm one step in the code is finish running it'll go back up
to the top here it holds it will do this next bit of code which is I plus plus.
So add one onto I.
So at that point with all this in place here basically what's happening is we started zero.
We check is I less than peace to drop which is tree for example is I less than tree which is is zero
less than tree.
Well yes it is so run a bit of cold in here which will right out the moment to do and then go back off
the top add one onto I.
So now I becomes one then it checks again.
Hey is one less than tree.
Well yes it is.
So a bit of code again.
And one is two less than tree.
Well yes it is so do a bit of code again and add one on is tree less than three.
Oh well no it's not obviously.
So stop doing this loop.
So at that point it goes.
Okay well then I'll continue onto the next bit of code that I see.
So obviously what we want to do inside our loop is select a random piece and sparseness below our at
our box.
So I want to delete that.
Like that's And there we go.
That's it.
That's all we have to do.
So now we can make some a few different objects appear when we break our box so I can press play and
if I go over here and roll to them you can see that these elements are now getting a few overlapping
here but that doesn't look great.
We don't want just our pieces to overlap.
We want to add a little bit of movement.
So when we break our box for example we want our pieces to move a little bit and come a little bit more
alive.
So we're going to have to add a little bit of functionality to the broken pieces themselves.