Welcome back.
In this chapter, we are going to build a fruit ninja clone and you might know the game fruit ninja
if you don't know it.
And check out some YouTube videos.
It's a very basic game in which you have to slice some fruit and you simply do that with a swipe and
then you slice them, they fall off and you get some points and your goal is to get as many points as
possible.
Of course, there is something that holds you back from making points and that are bombs.
So as soon as you cut a bomb, you'll lose the game.
All right.
So that's pretty much it.
There's not much more than that.
And we will build these basic functionalities in this chapter.
So let's go ahead and create a new project, and let's call that Fruit Ninja clone and let's create
the project.
In my case, I call it V one.
You don't have to do that.
And that's just because I already have a clone somewhere under my project.
All right, so there we are.
Let's go ahead and first of all, change the background of our camera.
So I don't want to have a skybox.
I'm just going to use a solid color and I'm going to change that color to.
Let's go ahead and pick a color.
And let's go with a brownish color.
Where light brown a bash almost great.
So that's our starting point.
And now let's import the assets that we have because there are some assets which you can download which
are attached to this lecture and let's go ahead and drag all of them into our game.
So there is banana cut, banana blend bomb, orange cut, orange blend.
So all these blend files which are blender files and let's drag them into our game.
All right.
So now, as you can see, the assets were created for us directly and the materials as well.
Let's start off by creating a folder for them, which I'm going to call models, and I'm going to drag
all of them in there.
Even materials.
So let's drag all of it in there.
All right.
So first of all, let's start with an orange.
Let's drag it into our game.
As you can see, this is the orange and that's how big it would be.
So maybe it's a good choice to reduce its size.
So here, go to model and change the scale factor to 0.3 and then hit apply at the bottom.
As you can see now, it's much smaller and that will be a much better size.
Otherwise it's way too big.
Now let's do the same thing with our cut orange or orange cut 0.3 and then hit apply.
Now let's drag the orange cut in there as well, actually in here.
So now you might wonder what that means.
Well, we have one version, which is the complete orange, which is uncut, and then there is the cut
version, which is the one that will be created as soon as we slice through or swipe through an orange.
And it's two pieces.
So it contains two pieces which will then fall off to different directions.
So we will get this slightly feeling of slicing an orange or in general or slicing a fruit.
All right.
So let's get rid of the orange cut again, because that was just for demonstration purposes.
So let's delete the orange cut here.
Now, the orange has an animator already, as you can see.
It doesn't have a controller, though, and we won't need that for now.
So let's get rid of it.
Remove component.
Now we only have the mesh render the mesh filter transform component.
And what we will need to add is a physics to the rigid body.
Now you might wonder why a 2D physics body and why not 3D?
Well, that's because I only want this orange to move into 2D room.
So it shouldn't move to the it should move to the top, to the left, to the right.
But it shouldn't go in a deep level, so it shouldn't go in the 3D level.
That's why Rigid Body 2D is the right rigid body for us.
Now let's add another component, which is a physics 2D circle collider because as you might guess,
our orange has the form of a circle.
So as you can see now, this is going to be the 2D collider.
You can even see it better if we go to the 2D room.
Now, you see that green little line surrounding our orange is going to be the Circle Collider.
Now the radius of the collider, you can find it here and you could change that.
As you can see here, making it bigger or smaller.
If you want to make the game easier, you would make it bigger or you could even in general scale up
the whole model.
But 0.3 is the right radius for us.
Now we could even get rid of this offset because it was so minimal.
It's just something that unity.
Unity does sometimes.
Well, now the rigid body is all right.
We will need a mass.
We will need a gravity scale.
But what we should change here is the collision detection.
This is fine in most games, but we want it to be super fast.
So we want to have a very quick detection here.
Collision detection, so continuous is the better choice for us because it's a very quick game in which
we slice the orange and it should react very quickly.
Now let's save that orange as a prefab.
So let's create a new prefab folder, folder prefab and drag the orange in here.
And while we're at it, let's save the scene as well.
So save scenes and let's call that one main.
Then we will need a script folder because we're going to work with script.
So let's create that as well script and let's go in there, create a new script.
I see sharp script to be precise and call it fruit.
Now let's open it up.
All right, in here.
What we want to do is we want to create a sliced fruit.
So we will need a sliced fruit.
Prefab for that, which means as soon as we cut our orange, we want to have the orange pieces.
So the two to the two parts of our orange, which will fall off into different directions.
And that's something that we need to set up here.
And that's not just for the orange is for any fruit that we're going to work with.
So let's create a public game object called sliced fruit prefab because we will need the prefab of the
sliced fruit, which is, for example, the sliced orange, and then we'll need a method which slices
the fruit.
So I'm going to call it create sliced fruit.
And what I want to do is simply instantiate the sliced fruit.
So.
Sliced fruit.
Prefab.
At which position?
Well, at the same position as our oranges.
And then transform the rotation.
So also the same rotation that we have now.
The next thing we need to do is, of course to destroy the whole orange.
So our game object, which is the game object that contains the fruit script or holds the fruit script.
All right.
Now let's go into unity again because we don't have the prefab yet, so we don't have the sliced prefab
yet.
Of course, we only have the orange prefab.
So let's go back to our model and let's drag the orange cut again into our game.
Now, the orange cut consists of two different objects, as you can see here.
This is just a folder pretty much which contains our orange cut left and our orange cut right.
So there are two pieces of the orange.
We want them to fly into different directions as soon as this create sliced fruit method is called.
So let's go ahead and do that.
Let's select those two cut pieces and add a component.
And now we're going to add a rigid body, not a rigid body, 2D, because we don't only want to fold
them off to the side, we also want them to slightly rotate.
So it looks a little bit more realistic because the fruit won't always fall off perfectly to each sides.
It will also rotate a little bit.
And that's something that we need to do with a rigid body 3D.
Now, about the collision detection, we can leave it as discrete because those pieces will not be as
important in terms of collision detection.
We don't want them to be sliced again, but we still need a physics.
Box.
Collider Well, in this case, we're going to use a box collider now here, of course, a box collider,
not a box collider to DD as our rigid body is a 3D object.
So now that we have that, let's drag them into our prefabs folder.
So let's actually drag the orange cut as a prefab into our prefabs folder because we want to have both
pieces and we want to have one prefab.
All right, so now we can get rid of our orange cut.
Delete that from here.
Let's save our scene as well.
So now the orange like this orange and the prefab, they both need to have the script that we've just
created.
So let's add the component.
Let's add the script called Fruit.
Now, of course, you see here we will need a sliced fruit prefab.
And it's perfect because orange cut is a sliced fruit prefab.
So let's drag that in there and let's apply that to our prefab orange.
Otherwise it won't have the script.
So you can see the prefab has a script that's really important.
All right, now let's that's done.
Let's try to get that happening.
So let's try to create a sliced fruit and we can use the update method for user input.
So if input dot get key down.
So as soon as the user presses a key and let's just do key code space.
So as soon as somebody presses the space key, let's just call this create sliced fruit method.
All right, so let's save that script.
Let's go back to Unity.
And now it's important that we have an orange in our hierarchy, because otherwise we don't have any
oranges to test that on as we don't have any spawn or yet.
So we will create spanners, which will spawn fruits and shoot them up the air so that we can slice
them.
But now we don't have that yet.
So let's make sure that an orange is in our game and let's start the game now.
Actually, I was too slow, so let's drag it up a little bit.
Let's go into the 2D room and let's pull it up.
So I have enough, enough time and let's maximize on play.
And the press, the key.
And as you can see, the sliced fruit was there and that's all good.
But of course, it doesn't look very nice.
It should rotate somehow or should move towards different directions.
And that's what we can get in our create sliced fruit because we need to add a rigid body here, which
does an explosion.
And in order to do that, we have to do the following thing.
So let's create a rigid body.
And in this case, we need an array of rigid bodies because we have two rigid bodies that we are talking
to.
So the one of the left sliced piece and the one of the right sliced piece, and I'm just going to call
it RB s or rigid bodies on sliced.
And in order to initialize it, we need the game object to initialize it with.
So let's create a game object and let's call it instance of.
Now the instantiate method needs to get a game object in front of it so that this will now not only
instantiate the game object, but will also create a game object which is saved in Inst.
Now we can use that inst to now go ahead and transform the get component component in children and here
to rigid body.
So I want to have the rigid body of each of the two sliced pieces.
And here it's complaining because it's not a component in children, but it's components in children,
because here we have an array of rigid bodies.
So it's plural.
Now let's go through all of the different pieces that we have.
So all of these different sliced orange pieces and take the rigid body called are in RB on sliced.
So we're going through this array here and for each of the pieces we change the transform rotation with
a random rotation.
So we just create a random row tation.
And that's beautiful because you see the random method has a property called rotation which will just
give us a random rotation.
We don't even need to write a method here to do any random rotations here.
So that's pretty cool with with the random class.
Now, next, we want to add an explosion so that the pieces go to different directions and they just
get blasted to the different directions.
And as you can see here, our rigid body has several add methods, add force, add relative force,
add explosion force.
All right.
That's exactly the one that we want to have.
So let's use that.
As you can see, there are multiple overloads and we're going to use this one here just with a explosion
force.
So how powerful should the explosion force be then?
The explosion position?
So the position from where the explosion should start.
And then finally the explosion radius and the value for explosion force should be random.
It shouldn't always be the same kind of explosion.
It should not always look the same.
So sometimes it should fly away further, sometimes less far, and so forth.
So let's create a random thought range.
And the two values I want to use here are 501,000.
So I just want to have a explosion force of between 501,000, which should be random.
Then the explosion position should be transformed that position.
So the position of the game object from which we exploded.
So it's in this case our orange, so the center of our orange.
And then finally what should be the explosion radius?
And I'm just going to enter five.
That's just the value that I have tested and works well here.
So let's use five F now let's save that again and let's go back to Unity and now let's press the key.
And as you can see, our fruit falls off pretty cool.
Now, if we duplicate the orange and reposition each orange it a little bit.
So let's change the values here.
Then you will see how cool our little explosion looks.
All right.
And now finally, this one here as well.
So now let's start the game again.
And you might see the rotation is random and the explosion itself is random as well.
So they fly off to different directions, different to powerful and so forth.
So I think this looks pretty cool already.
So you see, we do great explosions and yeah, I think that's good for this video.
And the next video we're going to check how to spawn those fruits because now we have to drag them into
our scene and that's not the best solution.
So let's save the scene as usual.
Don't forget to save the scene and see you in the next video.