So we want to add a bit more life to our broken pieces so as to do that.
We're going to go to one of our pieces here.
I'm going to go to my scripts folder.
I'm going to create a new C sharp scripts that will call broken pieces and I'm going to add that to
just one the scripts for the moment and we'll sort out the rest of them.
Then after that so we have our broken pieces scripts let's go ahead and open this up and we're going
to add a few variables so first of all we need to know how fast do we want to move.
So public flows move speed
and by default I know set out to be tree we always need to know what direction should this little piece
move in.
So we're gonna say private's vector tree move direction.
Now we're making this private because we obviously want to assign this when our game is running we don't
want to beat this to be so we don't want it to be that every piece moves in the same direction or anything
like that.
So we wanna make sure that we are randomly assigning this the next we're going to need is a race which
we're going to slow down because our move speed we obviously don't want us to be tree forever or else
those pieces will just keep going and never stop moving.
So we don't want that.
So what we're going to have is a public float D celebration
I set to be five by default and then one a couple of other variables but we'll deal with them in a little
bit.
Let's just sort out our movement and our deceleration.
First of all.
So first thing we're gonna do is as soon as this piece starts we're going to assign our move direction.
So we're just going to say move direction does X equals random dot range not random range random the
range between negative move speed and positive moves speed.
So between minus tree entry.
So somewhere in that range our x value is going to be and we'll do the same for our y value move direction
Y equals random dot range between minus move speed and move speed.
Very simple.
That's how we're going to control that.
And then first thing we'll do is obviously control our movements.
So we're going to say transform well not transparency alone trance form dot position plus equals move
direction multiplied by timed up delta time.
So let's just see that in action.
First of all where we're going to see our pieces keep moving forever and ever.
So that's not really Grace but we're going to go with that for the time being.
Let's go ahead and I think these are pieces and also add the broken pieces scripts to all of them and
I can multiple have multiple objects selected and I to overrides.
And here you can see you get this little warning saying you've got multiple like this so we can't show
the overrides but just cause can't show us doesn't mean that we can't apply all those changes so we
can apply all and apply the overrides that were changed on all of these objects.
So now when I press play.
Well first of all you'll see these pieces randomly go in different directions so you can see up there
goes all our pieces.
And if I go here and break these they also fly off in a lot of different directions okay.
So that's fine.
Obviously though we don't want them flying off in different directions like that.
So what we do want to do is over here on our transponder transformed our position.
We're going to make sure to move direction slowly goes towards zero on both the x and y axis.
We're basically going to make them go towards being 0 0 0 instead of say minus 23 and plus 1 on the
x and y axis.
So to do that all we have to do is say move direction is equal to.
I'm going to use a special function of vectors here.
We're gonna use vector tree does LARP slurp if I just open this bracket here you can see the tooltip
says linearly interpolate between two vectors.
So that's what LARP stands for is linear interpolation.
And basically what happens is if I open up paint here very quickly you've got two points in space you've
got a start point here and we've got an end point here so let's say for example in what we are doing
we want our characters move speed to go to 0 but it starts off at minus 23 so what Lerch does is it
basically gives it will move your value at a certain percentage of the distance towards zero.
So say for example we've got minus three here and say we I laugh is going to move 50 percent we don't
need to worry about the specific number and things like that.
So let's say this right here is our current value and that's where we started.
And this is where we're going to.
So when we move in our first frame it'll go 50 percent and it'll go to roughly about here and then that'll
be one point five.
Then in the next frame it'll move another 50 percent but it won't move all the way to zero it'll move
50 percent of the remaining distance so it'll go from here to here.
So that will be zero point seven five.
Then I'll go halfway again.
So it'll be point 3 7 5 and so on.
It'll keep going half of each distance so halfway and halfway and halfway and halfway in like that.
So what you end up with is that it looks like it slows down.
So it goes really fast at the start but then goes really slow until it gets to zero and one of the things
will learn as well is it'll never actually get to exactly zero.
It'll get down to a really tiny tiny small number that's almost impossible to represent but it will
look for all intents and purposes like zero.
So it'll effectively look like a piece of stop moving.
So with that in mind we can say let's fill in this loop.
So LARP requires a couple of variables.
So the variable first of all that we want to change is our move direction.
So that's going to be the variable that will be constantly changing.
Then we tell us what value we wanted to go towards.
So we want our mood direction to go to zero on the x y and z axis so we can say vector tree zero because
we know that's just a vector tree with 0 0 0 4 x y and z and then we need to tell the lab how fast it
should work and how fast we want our love to work is our deceleration speed.
So we're going to say deceleration we're going to multiply that by timed up delta time like so OK so
we'll say this and let's see this in action.
Compile and when we press play and see.
There we go to pieces.
Saturday fast but then slow down.
So when we break an objects now it looks like these pieces are flying out of the broken box so perfect
that looks good.
But we also notice that hey if we keep breaking pieces in our game we're going to end up with our hierarchy
being filled up with these objects.
So we really don't want them being broken up and staying here forever.
So what we're going to do is go into back into our script we're going to add a new variable up here.
We're going to add a public sloth lifetime so that be 23 seconds by default.
And how we're going to do another thing with these breakups as well but we'll just leave that for the
moment.
So what we're going to do is say in our updates we're going to make our lifetime countdown so lifetime
minus equals timed out delta time.
Now we don't have to use a camera for this because this is gonna be a variable that's only used once.
We're not gonna reuse this.
So lifetime will count down and it will check and say hey is our lifetime less than zero.
Well yes it is.
So destroy that destroy object destroy brackets game object.
Save that popped back into unity and let's see this in action.
So we have our broken objects gives that tree seconds and boom they disappear.
So again that's all fine that works for us but it doesn't look great that they're just disappearing.
Wouldn't it look a lot nicer if we took this broken piece for example and we just made us feel X overtime
so we'll make this a good and zero
so let's do that.
Let's with that in mind then we need a couple of variables to use and obviously the most important variable
is we need a reference to the Sprite Renderer attached to our broken piece of script.
So we're going to go here and we're going to say we need public Sprite Renderer.
There we go Sprite Renderer and would call this the SLR for these byte renderer.
And then when these know how fast we want it to fade out.
So public flows
fade.
Speed will cause and we'll set out to be 2.5 by default.
Okay.
So the down here when we make our lifetime count and 0 we're not going to just destroy the object straight
away before we destroy the object we're going to as soon as our lifetime can stand we're gonna start
making our sprite render color fade.
So we're gonna say the sprite render dark color is equal to a new color.
We don't want to change the or G and B values we don't want to leave them wherever they sat at on the
spray render.
So we're gonna say the S R does color the or the s or does color does G and the Assad does color does
B.
And then the next thing we want to do is make our house of value so our a value count down to zero.
Now we could use a loop again here Boss if we do a LARP it's going to very quickly fade out first but
then slow down and then it'll almost disappear.
But when you see something like that visually if it doesn't completely disappear and then the object
gets destroyed it actually looks kind of weird.
So we wanted to actually fade out completely.
So instead of using a loop function we're going to use something else a little bit differently a little
bit different but it functions mostly the same.
So we're going to use Matt s does move toward.
So we could use my desktop LARP but we're going to use my desktop move towards and move towards works
very very similarly to how our loop works.
So if we go back to our little image that we had here I'm going to get a green color when we use my
left move towards instead.
So let's imagine we start again at the same point instead of going a percentage of what's rate remaining.
So we went here 50 percent 50 percent of that and so on so on.
This will go a certain amount then it'll go the same certain amount then the same certain amount the
same certain amount and I'll keep going keep going keep going.
And then when it reaches the end points so say it has gotten to about here and then it reaches the end
points.
It won't go past the end point so it won't go.
It won't continue on.
Taken away past that point it will stop exactly at that point.
So it'll go the same equal amount.
Although these don't look equal at all.
Imagine that they're equal if they've got the same equal amount all the way.
And then when it gets to the end it'll just cut off at the exact target value that you have set.
So back in here as I said it's very similar to lurk in that we use the same kind of variables so the
first thing we say is the value we want to change.
So that's going to be the s or that color dot a we said the value that we wanted to go towards and that's
gonna be 0 F and then we say how fast we wanted to happen.
Well that's gonna be fade speed multiplied by timed up delta time okay.
So that's gonna help.
That's how we're gonna make our color fade away and then we can do a check and see if the Sprite Renderer
that color does A is equal to zero.
S
So if house value is zero well then an object is invisible.
So destroy the game object from the sea.
So let's see that in action.
We'll save this.
We'll go back in and then just on those pieces.
I'm going to I'm going to select the prefabs and select here and then each individual one I'm gonna
drag itself into there actually to be honest I could select them contract to sprite render into that's
lost and make a bit more sense but we can also select it and drag it in kind of up to you Whichever
method you find a little bit quicker to do with all them set up as dragging their own pieces onto themselves
we can hit play and I will see those ones fade outs do we go to disappear.
I know these ones will also appear and then fade out into the distance.
So having them fade out makes little sense because obviously they're items that don't need to last forever
and they were freeing up our hierarchy to not be clogged up with a whole bunch of useless elements.