Hello guys.
Welcome to the video.
So in this one I'm going to show you how to fill up level with the fruits.
And also I want to show you how to make them look different.
If you want to do that okay it's going to be optional.
First of all we're going to go to Asset Folder and make.
Folder that is going to be called prefab.
Now, if you never heard of a prefab.
It is very easy to explain.
You can take a game object and you can drag it to the prefab folder.
Now we have sort of a blueprint of a game object, and you can take this game object and drag it into
the scene to make another copy of the fruit.
It's not going to be the very same game object.
It's going to be a different game object, but with all of the settings of the first one.
Basically, it's like, you know, a blueprint of a Coca Cola bottle.
You have a Coca Cola bottle, you can get another one.
They look same.
They are basically the same bottles, but they are different bottles.
I hope that makes sense.
So it is the same with the fruits.
And now we can place a couple of them just by dragging it like so.
Or maybe duplicating it here and place it somewhere on the level.
Yeah, so that is good enough.
Now, what I want to do is to make a manager for the fruits that would collect all of the fruits on
the level and make them random if we want to.
Okay.
Uh, first let's set up the visuals for the fruit.
So it may look different every time.
We're going to go to the animator over here.
And we can do it in a couple of different ways.
What I want to do is to use a blend tree, because I think it's pretty suitable for the case we have
here.
First, we need to make a bunch of animations with different graphics.
This will take some time to set up, but it's worth it totally.
So let's go here.
Now.
Wait, we need to go to the graphic first item fruits.
We need to see them all okay.
Now let's create new clip.
And we're going to go to Assets Animation.
Let's do it here for now is going to be fruit.
Banana.
We're gonna open banana, pick the sprites, drag them.
Make sample rate 20.
Nice.
Let's, uh, make a new one.
Wrote.
Cherry.
Obituaries.
Get them.
Drag them 20.
Create new clip.
Wrote TV.
We're going to open this.
Take the Q drag it here.
Sample rate 20.
Now let's do a new one.
Melon.
Fruit.
Melon.
Let's open it.
Drag the sprite.
Sample rate 20.
Nice.
Okay, now we need, uh, orange.
Let's do a new clip.
Route.
Orange.
Going to get sample array 20.
Assign these sprites over here.
Oh.
All of them.
Then we need pineapple.
Let's do new clip.
Fruit.
Pineapple.
We're going to take pineapples.
Drag them here.
Sample rate 20.
And last one is strawberry fruit.
Strawberry.
Let's open strawberry.
Take the sprite.
Drag them.
Sample rate 20.
Okay, that was quick enough.
I thought it would take longer.
Now we need to delete all of this over here.
Let's just remove it and let's make new blend tree.
We're going to call it.
Route.
Uh, let's just call it fruit.
Why not?
Then we're going to open this blind tree.
And over here we're going to add motions.
We have one, two three, four, five, six, seven eight fruits.
So we need.
Eight of them.
Let's uncheck Automate threshold and let's select all of them.
So fruit apple.
Fruit banana.
Cherry.
Here we.
Mellon.
This is orange.
Pineapple.
Strawberry.
Uh, now let's do a threshold.
It's going to be zero one.
Uh, it's going to be difficult, but we need to do it.
I'm sorry.
Uh, zero for the apple.
For the banana.
One for the cherry.
Let's find it.
Where is the cherry?
It's not here.
Ah, it is here for cherry.
It's two.
Or the kiwi.
It goes three.
Or the melon is going to be.
Or.
On the orange.
It's going to be five.
Or the pineapple going to be six.
And for the strawberry, it's going to be seven.
Okay, now we have this blend tree, very similar to what we had in the beginning, but it has more
animations.
Let's open preview.
Let's go to play mode.
And we're going to just.
Change the blend tree and it's going to change the sprite.
Simple as that.
Okay.
Very easy to control.
So we can just send a parameter value to the animator.
And it's going to choose, uh, animation that we want to have on this particular fruit.
Now let's rename this to something like Fruit Index.
And it's going to use fruit index.
And then we can go to fruit itself.
And over here we can make a public method that would assign random look to the fruit.
Oh, and by the way, over here, we're supposed to have getcomponent in children.
Because the animator is a child of a game object.
Okay, so let's make some sort of, um.
Private void that would say set random look.
And then we would take an integer with a random number, and this random number would be from 0 to 7.
Because in the animator over here on a blend tree of the fruit, we have from 0 to 7, and it is eight
options total.
So let's go to the fruit script.
And make int random index equals to random range.
Range from 0 to 8.
Now, why did I type from 0 to 8?
If we have from 0 to 7, the point is random range gives you a random integer within minimum and maximum
value.
But as you can see it says min inclusive and max exclusive.
So it will be not taken and it will be a random number from 0 to 7.
Now let's go over here and type enum set float of uh root index to the value of a random index.
All right.
Now we can call this method in the start.
Let's just try set random look.
And let's go back to unity and see how does this work.
I'm going to go to Scene Window and I'm going to take a bunch of fruits duplicate them and let's go
to play mode.
Now we can see we have three different fruits and they look different very well.
So now let's make a boolean that would control if fruits should be random or not.
Okay.
That is very easy to do.
We need to go to game manager over here I'll make a header for the fruits manager management.
And over here I'm going to do a public bool.
Routes.
Have random look.
Also, it is not very good idea to access boolean directly.
So instead of just using game manager dot instance dot fruits have random look, I'm going to make a
method that would return me a value of a boolean.
So let's just do public bool fruits have random look and it simply will return me.
Fruits have random look.
Yeah, it is a bit better practice.
Now.
We can go back to fruit item over here and inside of a set.
Random look I'm going to do.
If game manager.
Routes have random look.
Equals to false, then return and we can rename this method to something like set random look if needed.
Okay.
Yeah this makes a bit more sense.
So it's easier to read even if you don't know what is happening here.
You open the script you see in there where we are getting a component in the start, we are assigning
game manager.
Then we have some method that says set random look if needed.
All right.
What the method does, it takes game manager and sees if fruits have random look and if it's a false
then we return.
This would not work.
Otherwise we take in a random index and assigning it inside of animator.
Very cool.
Let's go back and just make sure it is working.
I'm going to set true over here and going to go to play mode.
And yeah, it is random.
Now I'm gonna go off the play mode and check the boolean, go to play mode again.
And they did not change their look very well.
Now there is only one thing to do and we're good to go to the next video.
And that is what if you don't want to have a random look for the fruits?
What if you want to have them look different, but you want to decide how they're going to look on the
level?
Maybe this level going to be only with apples and bananas, for example.
Well, that could be solved in a very different ways.
We could have like a bunch of different solutions.
One of them is more efficient than the other.
But this is a course mostly for beginners.
So I'm going to take a simple approach.
Uh, we can take a variant of a prefab and change it slightly, and it would let us know how how the
things are working.
Uh, first, let's go to the fruit itself.
And let's make an enum that would have a type of fruits.
It would tell us which fruit it is.
This is how it works.
We're going to do public enum fruit type.
And if you never see an enum before, you can basically give a name for anything you want, for example
apple banana.
And then you can go over here and make.
Uh.
Serialize field.
Private fruit type.
Fruit type.
Now we can simply choose what it is, what it will be.
Apple or banana.
Back in unity.
On the fruit itself.
We have a drop down menu with the apple or banana, so you can use it to choose what you want to have
here.
And this enum has a number over here, which will help us to assign a value in the animator as well.
Before we do that, let's uh, put all of the fruit types we have.
For that, I'm going to go to Animator Open Blend Tree, just like.
That, then I'm going to go to script.
And see we have apple, banana, cherry.
So it should be cherry.
Here we.
Uh, melon.
Orange.
Fine apple.
Strawberry.
Very nice.
Very, very nice.
Now we're going to save this.
And then we're going to go to the.
Yeah, let's maybe let's make another method private void update route visuals.
And over here we're going to do something simple.
We're going to do maybe just anim set float of the route index.
And because this enum over here has a number 012 and so on, we can just basically, uh turn this enum
into a number and give it to the animator.
So we open parentheses over here we type int.
Then we type route type.
Now it will take a fruit type, turn it into integer and then give this value to the animator if we
need.
And we can just maybe do it here.
So if that is false, then update fruit visuals and return and it will just assign the default look
that you want to have on the fruit.
Uh, let's go back to the unity.
Let's just make sure it's working first.
So I'm going to go to scene.
I'm going to choose here a cherry.
And let's make sure Fruit Manager has this as a false value.
And then we're going to go back to play mode and boom we've got a cherry.
Very well.
Now of course we could take.
Prefab from here and just change the type over here.
That would work, right?
This is okay, but it is a bit difficult to do because you would have to cycle through all of the fruits
and you would have to check how do they look.
There is a different approach.
It takes a bit of a time to set up, but it will definitely make it easier later on.
So what we're going to do, uh, we could make the prefab variant just by dragging prefab over here
and choosing prefab variant.
But sometimes it can get messy with the prefab variants, because prefab variant does the copy of the
original prefab.
If you change something on a prefab variant, it's going to stay there.
If you change something on the original prefab, it may affect prefab variant as well.
And because of that, you can easily lose sight of what is happening with your fruits.
I don't want you to have that experience, so let's just make a bunch of original prefabs over here.
We're going to make a regional prefab.
There's going to be a fruit banana.
Then we need to change fruit type over here and we need to go inside of this prefab.
Find an animator, click over here and type banana.
Then select the sprite.
And now you have two different prefabs.
And if you want to place different fruits on the level, you can just drag them from here and place
whatever you want to have.
Okay.
Very well.
So now let me delete this and this I want to rename this to Fruit Apple.
Let's make another original prefab.
And also you should know that prefab variant would have a smaller size.
Just for the sake of a explanation, I'm going to make one prefab variant, and you can go to show in
explorer and see that.
Uh, this is the original prefab.
This is a prefab variant, and they have size of three kilobytes and six kilobytes.
In the perspective of this video game, it does not matter at all.
6kB or 3kB is just, you know, it's just funny because modern days, uh, games have an size of a 100
GB or something like that.
So please don't worry about that.
Just use Prefab original.
Okay?
Yeah.
Let me do this as a cherry.
I'm going to rename it to cherry.
And now that you know how to do these steps I'm going to do other fruits by myself.
I'm just going to speed up the video.
And I trust that you can set up this on your own because you know what to do here.
Also in the end of this section, I'm going to give this project to you.
So you're going to have this setup as well if you want to.
All right.
We've got this set up.
It is very nice and clean.
You can pull different fruits on the level and use them as you like.
And if you want to have a random look for them, you just go to Game Manager and click this check mark
over here.
And they're going to get a random look every time you start the game.
Very well.
So this is cool for now.
Thank you so much for paying attention and I'll see you in the next video.