Welcome back, my fellow game developers.
In this video, we've created the broken pieces
and we make sure that when we hit these broken pieces,
they fly a bit apart.
So now when I hit it, there you go.
You can see that these spots moved a
little bit and you can see that,
okay, So for this wasn't the best example.
Let's try to see when we
have two parts that are the same.
So there you go.
Can you tell which one is part
2 and the which one is also part two?
Nope, you can't because we've added a random rotation.
I thought it would have been
a very nice idea when I told you
about it in the previous video,
I thought why not implemented right now into this game.
So let's not waste any more time.
And let's get started.
All the case.
So now when we destroy our broken bought,
we have the broken pieces and speciated.
But first of all,
they are sometimes the same
even though we choose random numbers.
And the second thing is that they don't move apart.
So the first thing that I want to handle is the rotation.
So instead of just having the transform rotation,
but I'm going to do is I'm going to create
another integer, random rotation.
And it's going to be equal to a random range between 04,
Y4 because I'm going to be rotating
them around the z-axis in four directions.
So the way I'm going to do that is by
multiplying in here by 90 degrees.
And we will have four options.
So either we rotate ninety one hundred and twenty two
seventy or we just brought it all the way around.
Maybe this one should be 0.
We start off at 0123.
Okay, so instead of the transform dot rotation,
I'm going to use something called
the quaternion, that ruler.
There we go, That's ruler.
I was looking for it and see it.
So you can see that this translates
the three axis and to the rotation.
And we are going to be obviously
rotating on the, around the z-axis.
So 90 half times the random rotation.
There we go.
Save that. You can see the instantiation right here.
If you think this line is a bit too long,
what you can do is hit Enter on every single line.
And now it's much more obvious what
this instantiation does and what the elements of it are.
Okay, great. Now with this none,
the next thing is we want to add
behavior to our broken pots.
So we are going to go into the scripts in here and
the breakable right-click and create
a C sharp script which will be called the broken pot.
So broken part, hit enter.
Okay, I don't know why this keeps happening
and I'm going to go into the broken parts,
select all three of them,
and go ahead and add
the broken part script on each one of these prefab.
Open the script up and what do we want in here?
So first of all, we want a variable too,
after the movement speed of these parts.
So it's a serialized field which is going to be a float.
And we'll go to the move speed.
And if you want to add a bit more variety,
you can even make each one of them
has a different speed and baby,
depending on how fast dashes or whatever you want to do.
Then we are going to create a private vector 3.
And this will represent the movement direction.
So movement direction.
So in what direction will each piece be moving in?
And finally, we will have something
called a serialized field,
which is going to be a float.
And this is the halting factor.
And it's going to be 50 of these numbers are
arbitrary and I've already tested a couple of outsold.
That's why I know why five-act.
But anyways, what is this halting factor?
Obviously, when our pieces break up,
they will spread apart,
but then they will stop at some point.
So our goal is to make the salting factor,
which will affect the pieces as
they move away from each other and finally stopped them.
So we will slowly be slowing down.
And I'll show you how we'll be
slowing down using something called lobe.
But let's not get ahead of ourselves.
First thing we want to do is we want to have
a random direction in which each piece moves.
So first of all, I'm going to
make a movement direction of x.
So why am I doing this and start?
Because the broken part is attached
to the broken piece, which instantiates.
And as soon as it instantiates,
the start is being called.
And then start will determine what the direction is.
So the movement direction dot x2 will be equal to
the random dot range and what values you,
you can choose any value you want in here.
But just to make it a bit of fun.
For example, you have
the broken pieces flying off very fast.
I think the direction,
the randomness of the direction will
be a bit more wacky, let's call it.
So what we'll do is we'll use
the minus movement speed as
the minimum and the movement speed as the maximum.
So now as the movement speed increases,
if you wish to increase it,
the randomness will also increase.
And having a minus will make
sure that some pieces just go.
I don't know where some random place.
So now with the movement acts downwards,
that's have the movement y and
it's going to be exactly the same.
With that.
Let's continue on. We want to
start actually moving the pieces and
we're going to use the transform dot position.
And we're going to be increasing it by
the movement direction times time dot delta time.
Obviously we know plus, plus equals,
so it moves slowly and the movement direction times
Time.deltaTime to detach from the frame rate.
So this would be more stable.
Anyways.
Now we need to make sure that we
are halting the movement of
each piece and not just having them move
into infinity and going through everything.
So the way we are going to do
that is using something called the Learn.
So I'm going to show you what is,
make sure that you find it on your
own in Unity's documentation.
What does it do? It takes in three variables.
The first one is the start value of
the speed of and with its the start value.
B is the end value and t is
the value to interpolate between a and b.
What does interpolation mean?
So, linear interpolation between the two points.
So what happens is we want to take the value a or any
value from a to B using a factor of t Every single time.
So this will be
the Halting factor that
we talked about that we are going to
be applying and make sure that
you understand what is happening in here.
We are trying to move the pieces slower and slower.
So we are going to start off with some kind of direction.
And then we are going to be taking it
to 0 to stop the pieces from moving.
So I'm going to issue
this challenge to use her to stop the movement.
So first of all, we want to decrease
the momentum of the broken pieces,
of the moving pieces.
We want to set the movement direction to learn.
We're going to start value.
We're going to have the start value
be the actual movement direction,
and the end value should be the 0.
And finally, the speed of which we are going to
be halting the movement is going
to be the Halting factor.
So basically this challenge as you
applying, stop the movement.
So pause the video right now and go do the challenge.
Okay, welcome back.
So in here, after our
while we are moving in certain directions.
So we have the movement direction as
the only factor that's affecting this.
So in here are going to set the move direction to
be equal to a vector three bottlers.
And in here, the initial value
will be the movement direction.
The final value will be vector 3 0, and the factor,
the T will be
the Halting factor times
Time.deltaTime because we are
learning every single frame,
so we don't want it to be very fast.
Okay, so let's go back in here and
let's test this out and see if it works. For we do that.
I'm going to go into the
prefab and see what we have broken parts.
Maybe we can add them here.
So breakable pot.
There we go.
It's in the broken parts,
maybe whatever it's called.
Okay, broken pots, run the game.
Let's see what happens.
Maybe this doesn't work at all then.
Oh, nice.
So as you can see,
each part went our separate ways and you can see we have
the part one which was exactly on
top of the part one or the other part 1.
So they had the same rotation.
Let's test this out again.
There we go, we destroy it.
What do we have? Part 2.
2. And so can you figure out which part the two was?
So if I move this right,
also still having the same rotation for some reason,
did we do something not completely right,
so I'm going to break the pot and let's see.
Okay, so I feel that
the rotation thing isn't working if I
go back to the breakable random rotation.
Okay, welcome back.
So I was worried that there was something
wrong with the code, but no, apparently,
we are either the luckiest people on
the world or the unlucky asked people on the world,
depending on how you look at it.
But every single time the rotations were the same.
So you can see right here,
part two is minus 180 and the other part 2 is minus 90.
So again, let's test this out.
And let's see, there we go.
We break it.
And you can see we have Part 1 and Part one.
But can you tell the difference between these two parts?
So you can see that this one is 90 degrees
rotated and this one is the same.
Wrote it. So there is a big difference between them.
So that done, I hope you can
see and appreciate how random rotations work.
I hope you can see and appreciate how
the small detail of adding a bit of velocity to
the broken pieces makes everything look
more and let's say natural and better.
So let's make sure that we stay
all of our changes and I hope you're
enjoying and I'll see you in the next video
where we actually will be getting rid of
these part that fell apart
using something similar to learn.
But we'll be applying it to the broken parts to make
them slowly disappear and not just an instant go away.
So I'll see you.