Welcome back.
Now, if we start the game and we check one behavior, which is if we cut our oranges, they will also
stay in the game infinitely.
So we have taken care of our oranges.
They will get deleted after a certain amount of time.
But our cut oranges not so let's change that.
Let's go into our script and in this case, into our fruit script.
So here destroy insert dot game object after 5 seconds.
So this instantiated game object, which is our created slice sliced fruit, will be deleted after 5
seconds.
So let's have a look, if that's true now.
Now I'm gonna slice and let's see if those orange cut game objects get deleted after 5 seconds.
And as you can see, they are gone.
Those things are very important.
If you don't take care of those things, you might run into performance issues.
And that's not that big of an issue with a PC, which has plenty of powerful hardware, but it's a problem
with smartphones.
So really be careful with creation of game objects and deleting them.
All right.
Next, let's create the blade.
So let's create an empty game object and call that one blade.
Now let's reset the transform position and let's add a component, which is a trail renderer.
So there's the thing called trailer renderer.
Let's just use that.
Save the script and check it out.
And if we move around and let's play around with the X and Y value, and as you can see now, it creates
this magenta, which means we don't have the correct material for our blade yet, so let's create a
material for that.
Now let's go into our assets folder and create a new folder in order to create a material for it.
So material and actually materials, plural.
And in here, I want to create a new material and I'm going to call it Blade.
Now, it needs a color, of course, and I'm going to take a light blue, actually, maybe a bit more
powerful just like that.
You can take any color that you like, so I'm just going to take this blue.
Now let's check out our blade and it needs here material.
So click on materials and it says there is one material with none in here.
So there's no material in the element zero.
Let's drag that in there.
And if we test that now and move our blade back and forth, we will see that there is our blue material.
Now what we want is that our trailer renderer follows along our mouse and there is a functionality that
does that.
So it will check out the vector three values.
So a3d value.
In our world, of course, our game is a 2D game, but still the mouse is within a 3D world and we're
just going to use the 2D components that are relevant for us.
All right.
Now, if we look at our trade renderer, we see that it stays on the game for quite a while.
And that has to do with this time.
And let's just change that to 0.2.
So it will only be alive for 0.2 seconds, which means it will be a little trail, but it will not be
a very long trail here.
Let's test that.
Let's run it again and change the position of our X value.
As you can see, it doesn't stay alive for quite a while.
It only very only has a very short life.
And of course, it's also way too wide because we shouldn't have this huge cut or this huge blade look
in our game.
So let's go with 0.1 and let's see how that looks.
Let's play around with the X value.
And there you see, this is how it could look like.
So this is how our our trail will look like.
Now, in order to have a nicer look, so actually I'm going to make the game scene a little bit bigger
so we can see it a bit better.
So I want to change the looks of my trail because currently what it does, it is always 0.1 and we want
to change that.
So let's add a key now.
Let's drag the center here to the top and let's add another key here at the end and let's drag it to
the bottom.
So let's have a look what that means.
So if we move our X position, as you can see, it looks more like a cut with a knife.
You can only see it very quickly, but it will make much more sense as soon as we have our mouse functionality
implemented.
Great.
So next our blade will need a rigid body so that it can move and has a physical impact into the game.
Because what we want to have is of course, if we slice with the blade, which will follow along with
our mouse, if we slice through an object, we want that object to be impacted.
And in order to do that, let's go ahead and add a rigid body to DX and it should be kinematic.
What that does is it tells our blade that it shouldn't be influenced by other objects, but it will
still influence other objects.
So for example, it will still influence our fruits, but the fruits will not drag or push off our blade.
And the collision detection should be continuous here as well because we want to have a very fast collision
here.
All right.
Next, we want to have a script which, of course, takes care of this beautiful behavior that we've
just seen just with the mouse so that it doesn't follow along the X and Y position that we have set
here in our transform component, but it actually follows our mouse.
So let's call that script blade and let's open that script up.
And in here we will use the awake method instead of our start method.
And in here we will actually create a rigid body, private or B or rigid body, which I'm going to call
RB and let's initialize that RB with get component rigid body to DX.
What that will do is it will simply take the rigid body component from the blade itself because the
blade contains that script.
So it will take that rigid body here.
Next, we need a method which will take care of our blade following our mouse.
So let's create a new method, private void set, relate to mouse.
So this method will take care of setting the blade to the position of our mouse and we simply change
the rigid body.
So the RB position to camera dot main that screen to world.
Point transforms position from screen space into world space.
So that's exactly what we want here.
And now we simply say the input should be our mouse position.
So wherever our mouse is in comparison to the world, that's where we want the rigid body to be or the
rigid body of our blade to be.
And that will give us the slide.
Look, we will see that in a second.
And of course, in our update method, we will constantly call the set blade to mouse in order to keep
it up to date so that it always follows the follows along our mouse.
So it will change the position of the rigid body with every single frame based on where our mouse is.
All right, so let's go back to the Blade script.
And here there is a little thing that we need to solve.
So in some cases it might work for you.
So if you start the game, you will already see the mouse being followed.
But in other cases it will not work because the mouse position itself is a 3D point, but our world
is a 2D world, so now it doesn't always calculate it correctly.
So in order to fix that, what we will need to do is we create a new variable called mouse position
or mouse pose, and we set that to input mouse position, which is the one here.
It's the same position.
But now what we do is we set the Z value of our mouse position to the value of ten.
So that means that this is going to be the distance
of ten units from the camera.
And now what we can do here is we don't hand over the input mouse position, but we hand over the mouse
pose.
Now let's save that.
Let's go back to unity.
And by the way, and this ten here actually is this minus ten here.
So we go ten further in in order to get the correct position.
All right.
So let's try that again and now actually get a null pointer exception.
And here, of course, judge, that it should be a week.
Okay.
So now let's check that out.
Let's start the game again.
And let's see.
There you are.
Our plate is being followed.
So this is the blade effect that we have now.
So if we swipe through, we, of course, now want to delete or we want to cut the fruits.
So let's do that.
All right, so now let's cut the fruits.
Let's go into our fruit script.
And in here we are going to add an on trigger enter event on trigger enter to DX.
So we call this method and we say whenever we collide with the blade, we want to go ahead and create
sliced fruits.
So what that will do is go ahead and cut our fruit.
So let's create a blade object here.
Blade B we take the collision dot get component and we call it blade or or actually we get the blade
itself.
So if there is a component called blade with the object that we collided with, then create an object
called B, which is of type blade and then we check if there is no B, then just return.
And what that will do is, well, it will do nothing any more.
So it will just get out of the call of this on trigger enter event, but otherwise go ahead and create
a sliced fruit.
So now we won't do that in the update method anymore.
So let's get rid of that.
We will do it however in the on trigger enter.
So we will create sliced fruit whenever we collide with the blade.
And if we don't collide with the blade, then just return it to nothing.
So let's check that out.
Let's run our code.
Well, actually, before we try that, let's add a collider of more to our blade, because currently
it doesn't have a collider.
So let's create a collider going.
Going to add a.
Circle Collider here.
And before we test that we of course need to make that is trigger and let's see if our fruit is a trigger
as well.
So let's go to our orange or orange should be a trigger as well.
So let's go ahead and try that again.
Now, if we slice through our fruits, we can see that they get cut.
But as you might also see, it's not perfect yet.
So we will have to play around with the values of our radius here.
So let's go with a radius of let's go into the scene and see how big our radius is.
So let's go with 0.5.
Maybe that would be the radius if we compare it to an orange.
So let's drag an orange in here.
That would be too big.
You might guess so let's go with 0.3 maybe or oh point.
Well, actually, as soon as we hit the orange, we already cut it.
Right.
So all point three should be the right value.
So now blade is exactly the same size as the orange.
So as soon as we hit it, we should be good.
So let's try that.
And now let's cut some fruits.
Well, and as you see, we can slice our fruits.
Great.
All right, so see you in the next video where we are going to add the green to our game.