Welcome back my fellow game developers and this video,
when we were as the game,
when we maximize that we go,
we run the game.
When we walk around,
when we hit one of the parts,
you can see that it gets destroyed and they slowly,
slowly fade out,
and then they disappear from the hierarchy.
So let's not waste any more time.
And let's get started.
Oh, okay.
So now it's time to finally
finish off this and
destroy the broken pieces on the ground.
How are we going to do this?
Well, we are going to combine a couple of things that we
already know and maybe one method that we don't.
So let's get started.
The first thing I'm going to do is
I'm going to go into my scripts.
And in here, in the broken part,
I'm going to first of all create a timer here,
which is going to be a serialized field.
And it's going to be a float with
a lifetime called lifetime.
And let's just set it at around three seconds.
Okay?
Maybe even more, maybe five,
Let's set it as five.
And then after we instantiate,
after we start moving and the position
and be wild or after all,
show while we are learning the direction,
we want to go ahead and
go to the lifetime and make sure that every
single frame we are removing the time, that delta time.
Okay, great.
Now, what happens when the lifetime, where is it?
When the life time is less than or equal to 0,
we are going to destroy the game object.
Let's check this out and see if it works.
So I'm going to go into again, let me save that.
Yes, we did run the game.
Had the broken pieces, 1, 2, 3.
Okay. So it's around five.
I guess.
I think that was five anyways.
But obviously we don't want to destroy this.
We want to slowly tear it apart,
so we want to make sure that it doesn't just explode.
We want to slowly deteriorate with time.
So to do that,
we are going to use something called the move towards.
But before we delve into that,
I want to show you something.
We are going to be accessing the colors,
so let's use the pot.
Okay, So on the broken pieces we
have the same breakable pot.
Actually know what, let me just add one of
those broken pieces in here and demonstrate on them.
So you can see we have the sprite renderer,
we have the lifetime and everything.
If I access the color,
I have these three colors.
So the R, You can see that I
can change the color if I zoom in,
you can clearly see how ugly
this pixel art is. No, I'm kidding.
It's amazing.
And then we have the red.
Finally we have the yellow,
and we have also the fate,
and this is what we want to access.
Now, as I always say,
there are easier ways of doing this,
but I want to test everything.
I want to explore with you as much as possible.
So I'm going to delete this.
I'm going to go back into Unity,
and I'm going to access the sprite renderer.
So I'm going to,
first of all create a reference in here.
And it's going to be a serialized on, you know what?
It doesn't need to be a serialized field.
I'm just going to get access to the right, a renderer.
And I'm going to call the part sprite.
And it's going to OK.
It's going to be private.
I forgot to make it private
because we don't need to see it and start.
I'm going to make sure that the part Sprite is equal
to get component sprite render.
Okay, there we go.
So now we have a reference to the part Sprite.
And in here I'm going to access
the part Sprite dot color.
Okay, So you notice that on
the sprite renderer we have the color.
So we're accessing the color in here.
And we're saying that it's a new color.
And what shall this color B. I'm going to open up
the bracket right here and
I'm going to hit Enter and start typing in.
So we can see right here, where is it?
Where is the new color?
Okay, but anyways, we have to give it the
red, blue, and green.
And as you saw, the red,
blue and green are not going to change.
So the first three will be extremely easy.
It will be the parts bright color dot r,
comma duplicate, duplicate,
red, green, red, green, and blue.
And there we go. If we hover over right here,
we can see that these are three colors that we want.
And finally, we want to change the a.
So the way we access this is through
the part dot color dot a.
But obviously this time we want to change this.
We want this to move from what it currently is to 0.
And this is the math out dot moved towards.
So as you can see, it moves a value
from the current towards the target.
And you can see right here,
move towards using the math library.
We can add the current value,
the target value that we want to get to, and the maximum.
So it's the maximum change
that should be applied to the value.
So it's just like we have any alert,
the slowdown factor or the Halting factor.
This is also some kind of factor that you can use.
Okay, so let's go ahead and implement this move towards.
So in here I'm going to write method dot move towards.
And I'm going to open this up.
So the current target will be the current alpha.
The target, okay, So the current,
the current value, I meant to say,
and the target will be obviously 0 F.
And we are going to be moving at the fade speeds.
So I'm going to just write faith speed
in here, times Time.deltaTime.
And I'm going to go ahead and
create some kind of fade speed variable.
Copy this, and let's go ahead.
Where should we added?
Okay, so there we go.
This will be the fate speed and set it to five.
Let's set it to one and see how fast that is.
I'm not sure how fast will be.
I really have no idea.
So we're going to save this.
So what happens in here is when the lifetime,
there is an error,
which is that the lifetime is less than 0.
Okay? So we need to add a point and come up.
So what happens in here when the lifetime is less than 0?
So we've finished spreading out,
the lifetime is done.
We are going to go through this every single time.
So what happens in here?
The red, the green, the blue stay the
same on the part Sprite.
But the color, the Alpha moves from
the current state that it is
n to the 0 using some kind of fade speed.
So let's go and test this out.
So I'm going to run the game.
I'm going to try to make this bigger, to see everything.
I'm going to destroy this.
And now we'll see that
the broken pieces after a couple of seconds.
Whoa, how cool is that?
So they slowly disappeared.
And if you noticed,
we had the three parts being
part 3 and they all look different.
So we didn't have any thing that looks similar.
But as you noticed,
even though they fade it out,
we still have them right here.
We need to get rid of this and it's going to be
a challenge for you to destroy the pieces.
Finally, so create an if statement
that checks if the piece alphas down to 0.
And if it is, you'll need to go ahead and
destroy the game object.
So pause the video right now and go do the challenge.
Okay, welcome back.
So in here, after we've moved towards,
we are going to keep checking if
the part Sprite dot color dot alpha,
which is the a, is equal to 0 f. Then
we are simply going to destroy the game object.
Save that back in our game.
Let's maximize on play.
Our game is looking fantastic.
I mean, except for the blue lines that you just saw.
I mean, we have the enemies shooting at us, following us.
We have weapons shooting properly.
We have different effects
for different weapons hitting things.
You can see maybe
the salt hitting could use a bit of help.
We have blood everywhere.
We have blood when we interact.
Well, what did you see that?
So when we should wait,
when we shoot one of the enemies,
you can see the blood coming out of them,
which is really nice. There you go.
You can see the blood. We have random blood we have
now also the structural objects if you want.
We can also implement
their destruction when we shoot them.
But anyways, when we crush them,
these are the pieces.
And they disappear slowly,
which is a very nice addition.
And I always forget too,
I wanted to, okay?
Okay, so remove the maximization. There you go.
We can see that we no longer have any parts,
but we still have the blood,
which you know what,
I'm going to give it to you as
a challenge from I don't know.
I'm not going to deal with this right now.
Obviously at some point I will,
but I pretty sure you know what to do.
You can use whatever method we've learned.
There are a lot of things,
so I hope you enjoyed this section.
I think it really adds to
the whole atmosphere of the game,
the moving, slowly destroying.
We learned a lot in this section also.
So make sure that you are always committing your changes.
And if you haven't already,
I know I bored you,
but please leave me a review helps me out a lot.
Of course, if you're enjoying the course,
if not, not, until you begin enjoying the course.
Anyways, I'll see you in the next one.