WEBVTT

00:00.110 --> 00:00.590
Hello guys.

00:00.590 --> 00:01.550
Welcome to the video.

00:01.550 --> 00:04.700
So in this one I'm going to show you how to fill up level with the fruits.

00:04.700 --> 00:07.220
And also I want to show you how to make them look different.

00:07.220 --> 00:09.350
If you want to do that okay it's going to be optional.

00:09.350 --> 00:12.410
First of all we're going to go to Asset Folder and make.

00:14.050 --> 00:16.810
Folder that is going to be called prefab.

00:17.140 --> 00:19.300
Now, if you never heard of a prefab.

00:19.330 --> 00:20.830
It is very easy to explain.

00:20.830 --> 00:24.250
You can take a game object and you can drag it to the prefab folder.

00:25.200 --> 00:31.800
Now we have sort of a blueprint of a game object, and you can take this game object and drag it into

00:31.800 --> 00:35.700
the scene to make another copy of the fruit.

00:35.730 --> 00:37.920
It's not going to be the very same game object.

00:37.920 --> 00:42.330
It's going to be a different game object, but with all of the settings of the first one.

00:42.330 --> 00:46.800
Basically, it's like, you know, a blueprint of a Coca Cola bottle.

00:46.800 --> 00:49.920
You have a Coca Cola bottle, you can get another one.

00:49.920 --> 00:51.210
They look same.

00:51.210 --> 00:54.630
They are basically the same bottles, but they are different bottles.

00:54.630 --> 00:55.740
I hope that makes sense.

00:55.740 --> 00:57.060
So it is the same with the fruits.

00:57.060 --> 01:00.150
And now we can place a couple of them just by dragging it like so.

01:00.150 --> 01:05.280
Or maybe duplicating it here and place it somewhere on the level.

01:06.170 --> 01:07.790
Yeah, so that is good enough.

01:07.790 --> 01:14.330
Now, what I want to do is to make a manager for the fruits that would collect all of the fruits on

01:14.330 --> 01:17.750
the level and make them random if we want to.

01:17.780 --> 01:18.680
Okay.

01:18.980 --> 01:22.880
Uh, first let's set up the visuals for the fruit.

01:22.880 --> 01:24.950
So it may look different every time.

01:24.950 --> 01:27.620
We're going to go to the animator over here.

01:27.620 --> 01:30.410
And we can do it in a couple of different ways.

01:30.410 --> 01:35.450
What I want to do is to use a blend tree, because I think it's pretty suitable for the case we have

01:35.450 --> 01:36.080
here.

01:36.080 --> 01:39.950
First, we need to make a bunch of animations with different graphics.

01:40.460 --> 01:44.180
This will take some time to set up, but it's worth it totally.

01:44.180 --> 01:45.500
So let's go here.

01:45.500 --> 01:45.710
Now.

01:45.710 --> 01:49.640
Wait, we need to go to the graphic first item fruits.

01:49.640 --> 01:51.350
We need to see them all okay.

01:52.600 --> 01:54.490
Now let's create new clip.

01:55.210 --> 01:58.150
And we're going to go to Assets Animation.

01:58.150 --> 02:00.640
Let's do it here for now is going to be fruit.

02:01.440 --> 02:02.280
Banana.

02:03.270 --> 02:06.840
We're gonna open banana, pick the sprites, drag them.

02:06.840 --> 02:08.670
Make sample rate 20.

02:09.150 --> 02:09.750
Nice.

02:09.750 --> 02:12.090
Let's, uh, make a new one.

02:13.390 --> 02:13.930
Wrote.

02:14.740 --> 02:15.550
Cherry.

02:17.040 --> 02:18.120
Obituaries.

02:18.480 --> 02:19.530
Get them.

02:19.710 --> 02:21.990
Drag them 20.

02:23.170 --> 02:24.490
Create new clip.

02:25.170 --> 02:26.790
Wrote TV.

02:28.530 --> 02:29.580
We're going to open this.

02:29.580 --> 02:32.100
Take the Q drag it here.

02:32.100 --> 02:33.600
Sample rate 20.

02:33.630 --> 02:35.310
Now let's do a new one.

02:35.310 --> 02:36.330
Melon.

02:36.330 --> 02:36.930
Fruit.

02:37.440 --> 02:38.430
Melon.

02:39.320 --> 02:40.610
Let's open it.

02:41.180 --> 02:42.440
Drag the sprite.

02:42.980 --> 02:45.050
Sample rate 20.

02:45.230 --> 02:46.250
Nice.

02:47.080 --> 02:50.950
Okay, now we need, uh, orange.

02:50.950 --> 02:52.390
Let's do a new clip.

02:53.420 --> 02:53.870
Route.

02:54.500 --> 02:55.430
Orange.

02:56.710 --> 02:58.420
Going to get sample array 20.

02:58.420 --> 03:01.150
Assign these sprites over here.

03:01.480 --> 03:02.020
Oh.

03:03.160 --> 03:04.000
All of them.

03:06.400 --> 03:07.540
Then we need pineapple.

03:07.540 --> 03:09.130
Let's do new clip.

03:09.460 --> 03:10.150
Fruit.

03:10.780 --> 03:12.070
Pineapple.

03:13.540 --> 03:14.980
We're going to take pineapples.

03:14.980 --> 03:16.030
Drag them here.

03:16.030 --> 03:17.890
Sample rate 20.

03:18.280 --> 03:21.370
And last one is strawberry fruit.

03:21.400 --> 03:22.690
Strawberry.

03:23.840 --> 03:25.250
Let's open strawberry.

03:25.610 --> 03:26.540
Take the sprite.

03:26.540 --> 03:27.260
Drag them.

03:27.260 --> 03:28.550
Sample rate 20.

03:28.580 --> 03:30.230
Okay, that was quick enough.

03:31.550 --> 03:32.930
I thought it would take longer.

03:32.960 --> 03:35.300
Now we need to delete all of this over here.

03:35.300 --> 03:41.150
Let's just remove it and let's make new blend tree.

03:41.180 --> 03:42.380
We're going to call it.

03:43.180 --> 03:43.750
Route.

03:44.890 --> 03:46.150
Uh, let's just call it fruit.

03:46.180 --> 03:46.870
Why not?

03:47.440 --> 03:49.060
Then we're going to open this blind tree.

03:49.060 --> 03:51.820
And over here we're going to add motions.

03:51.820 --> 03:55.420
We have one, two three, four, five, six, seven eight fruits.

03:55.420 --> 03:56.170
So we need.

03:57.440 --> 03:58.370
Eight of them.

04:04.370 --> 04:07.580
Let's uncheck Automate threshold and let's select all of them.

04:07.580 --> 04:09.170
So fruit apple.

04:09.170 --> 04:10.370
Fruit banana.

04:11.250 --> 04:12.270
Cherry.

04:13.050 --> 04:13.920
Here we.

04:14.880 --> 04:15.630
Mellon.

04:16.710 --> 04:18.270
This is orange.

04:20.240 --> 04:21.170
Pineapple.

04:22.100 --> 04:23.120
Strawberry.

04:25.710 --> 04:27.000
Uh, now let's do a threshold.

04:27.000 --> 04:29.580
It's going to be zero one.

04:29.820 --> 04:31.950
Uh, it's going to be difficult, but we need to do it.

04:33.390 --> 04:34.290
I'm sorry.

04:34.590 --> 04:36.360
Uh, zero for the apple.

04:36.810 --> 04:38.010
For the banana.

04:38.160 --> 04:40.860
One for the cherry.

04:40.890 --> 04:42.000
Let's find it.

04:42.000 --> 04:42.990
Where is the cherry?

04:43.200 --> 04:44.370
It's not here.

04:44.790 --> 04:46.710
Ah, it is here for cherry.

04:46.710 --> 04:47.610
It's two.

04:49.920 --> 04:51.150
Or the kiwi.

04:51.150 --> 04:52.920
It goes three.

04:53.870 --> 04:55.730
Or the melon is going to be.

04:57.470 --> 04:58.190
Or.

04:59.730 --> 05:00.510
On the orange.

05:00.510 --> 05:01.950
It's going to be five.

05:03.360 --> 05:05.730
Or the pineapple going to be six.

05:06.420 --> 05:08.610
And for the strawberry, it's going to be seven.

05:08.610 --> 05:14.310
Okay, now we have this blend tree, very similar to what we had in the beginning, but it has more

05:14.310 --> 05:15.210
animations.

05:15.330 --> 05:16.860
Let's open preview.

05:16.860 --> 05:18.030
Let's go to play mode.

05:18.030 --> 05:19.620
And we're going to just.

05:20.240 --> 05:22.760
Change the blend tree and it's going to change the sprite.

05:23.590 --> 05:24.880
Simple as that.

05:24.880 --> 05:25.570
Okay.

05:25.780 --> 05:27.370
Very easy to control.

05:27.370 --> 05:30.790
So we can just send a parameter value to the animator.

05:30.790 --> 05:36.280
And it's going to choose, uh, animation that we want to have on this particular fruit.

05:36.730 --> 05:40.390
Now let's rename this to something like Fruit Index.

05:42.220 --> 05:44.020
And it's going to use fruit index.

05:44.140 --> 05:47.410
And then we can go to fruit itself.

05:48.810 --> 05:53.910
And over here we can make a public method that would assign random look to the fruit.

05:54.600 --> 05:59.400
Oh, and by the way, over here, we're supposed to have getcomponent in children.

06:00.130 --> 06:02.620
Because the animator is a child of a game object.

06:02.890 --> 06:05.410
Okay, so let's make some sort of, um.

06:06.610 --> 06:11.740
Private void that would say set random look.

06:12.490 --> 06:20.200
And then we would take an integer with a random number, and this random number would be from 0 to 7.

06:20.200 --> 06:27.820
Because in the animator over here on a blend tree of the fruit, we have from 0 to 7, and it is eight

06:27.820 --> 06:28.930
options total.

06:28.930 --> 06:30.820
So let's go to the fruit script.

06:32.230 --> 06:37.540
And make int random index equals to random range.

06:39.230 --> 06:42.320
Range from 0 to 8.

06:42.350 --> 06:44.450
Now, why did I type from 0 to 8?

06:44.480 --> 06:52.640
If we have from 0 to 7, the point is random range gives you a random integer within minimum and maximum

06:52.640 --> 06:53.060
value.

06:53.060 --> 06:57.590
But as you can see it says min inclusive and max exclusive.

06:57.590 --> 07:01.970
So it will be not taken and it will be a random number from 0 to 7.

07:03.780 --> 07:12.540
Now let's go over here and type enum set float of uh root index to the value of a random index.

07:13.140 --> 07:13.830
All right.

07:13.860 --> 07:16.110
Now we can call this method in the start.

07:16.110 --> 07:18.240
Let's just try set random look.

07:18.690 --> 07:22.440
And let's go back to unity and see how does this work.

07:22.440 --> 07:29.010
I'm going to go to Scene Window and I'm going to take a bunch of fruits duplicate them and let's go

07:29.010 --> 07:29.820
to play mode.

07:32.380 --> 07:36.850
Now we can see we have three different fruits and they look different very well.

07:36.880 --> 07:41.980
So now let's make a boolean that would control if fruits should be random or not.

07:41.980 --> 07:42.370
Okay.

07:42.370 --> 07:43.750
That is very easy to do.

07:43.960 --> 07:52.570
We need to go to game manager over here I'll make a header for the fruits manager management.

07:54.490 --> 07:57.460
And over here I'm going to do a public bool.

07:59.720 --> 08:00.410
Routes.

08:01.270 --> 08:03.250
Have random look.

08:03.250 --> 08:07.450
Also, it is not very good idea to access boolean directly.

08:07.570 --> 08:13.600
So instead of just using game manager dot instance dot fruits have random look, I'm going to make a

08:13.600 --> 08:17.020
method that would return me a value of a boolean.

08:17.020 --> 08:25.210
So let's just do public bool fruits have random look and it simply will return me.

08:25.210 --> 08:27.160
Fruits have random look.

08:27.970 --> 08:30.340
Yeah, it is a bit better practice.

08:30.340 --> 08:30.790
Now.

08:30.790 --> 08:34.930
We can go back to fruit item over here and inside of a set.

08:34.930 --> 08:36.700
Random look I'm going to do.

08:38.190 --> 08:39.750
If game manager.

08:40.850 --> 08:42.740
Routes have random look.

08:43.520 --> 08:52.610
Equals to false, then return and we can rename this method to something like set random look if needed.

08:53.060 --> 08:53.810
Okay.

08:53.810 --> 08:55.820
Yeah this makes a bit more sense.

08:55.820 --> 08:59.540
So it's easier to read even if you don't know what is happening here.

08:59.540 --> 09:03.920
You open the script you see in there where we are getting a component in the start, we are assigning

09:03.920 --> 09:04.970
game manager.

09:04.970 --> 09:08.750
Then we have some method that says set random look if needed.

09:08.750 --> 09:09.170
All right.

09:09.170 --> 09:14.690
What the method does, it takes game manager and sees if fruits have random look and if it's a false

09:14.690 --> 09:15.830
then we return.

09:15.830 --> 09:17.120
This would not work.

09:17.120 --> 09:22.010
Otherwise we take in a random index and assigning it inside of animator.

09:23.380 --> 09:24.430
Very cool.

09:24.850 --> 09:27.730
Let's go back and just make sure it is working.

09:31.970 --> 09:35.690
I'm going to set true over here and going to go to play mode.

09:36.800 --> 09:38.780
And yeah, it is random.

09:38.780 --> 09:43.580
Now I'm gonna go off the play mode and check the boolean, go to play mode again.

09:43.580 --> 09:47.420
And they did not change their look very well.

09:47.420 --> 09:51.110
Now there is only one thing to do and we're good to go to the next video.

09:51.110 --> 09:54.830
And that is what if you don't want to have a random look for the fruits?

09:54.830 --> 10:00.020
What if you want to have them look different, but you want to decide how they're going to look on the

10:00.020 --> 10:00.350
level?

10:00.350 --> 10:04.160
Maybe this level going to be only with apples and bananas, for example.

10:04.160 --> 10:07.700
Well, that could be solved in a very different ways.

10:07.700 --> 10:10.070
We could have like a bunch of different solutions.

10:10.070 --> 10:12.590
One of them is more efficient than the other.

10:12.590 --> 10:14.990
But this is a course mostly for beginners.

10:14.990 --> 10:16.970
So I'm going to take a simple approach.

10:17.820 --> 10:25.860
Uh, we can take a variant of a prefab and change it slightly, and it would let us know how how the

10:25.860 --> 10:27.240
things are working.

10:27.600 --> 10:29.910
Uh, first, let's go to the fruit itself.

10:31.140 --> 10:35.910
And let's make an enum that would have a type of fruits.

10:35.910 --> 10:37.770
It would tell us which fruit it is.

10:37.770 --> 10:38.850
This is how it works.

10:38.850 --> 10:42.360
We're going to do public enum fruit type.

10:42.420 --> 10:48.660
And if you never see an enum before, you can basically give a name for anything you want, for example

10:48.660 --> 10:50.370
apple banana.

10:50.370 --> 10:53.370
And then you can go over here and make.

10:54.080 --> 10:54.500
Uh.

10:54.500 --> 10:55.790
Serialize field.

10:56.360 --> 10:57.740
Private fruit type.

10:58.890 --> 11:00.090
Fruit type.

11:00.390 --> 11:04.170
Now we can simply choose what it is, what it will be.

11:04.170 --> 11:05.520
Apple or banana.

11:05.550 --> 11:06.990
Back in unity.

11:09.820 --> 11:10.960
On the fruit itself.

11:10.960 --> 11:15.580
We have a drop down menu with the apple or banana, so you can use it to choose what you want to have

11:15.580 --> 11:16.180
here.

11:16.180 --> 11:24.670
And this enum has a number over here, which will help us to assign a value in the animator as well.

11:24.670 --> 11:28.870
Before we do that, let's uh, put all of the fruit types we have.

11:28.870 --> 11:33.520
For that, I'm going to go to Animator Open Blend Tree, just like.

11:34.600 --> 11:36.850
That, then I'm going to go to script.

11:37.840 --> 11:40.240
And see we have apple, banana, cherry.

11:40.240 --> 11:42.400
So it should be cherry.

11:43.890 --> 11:44.430
Here we.

11:45.850 --> 11:46.870
Uh, melon.

11:47.760 --> 11:48.630
Orange.

11:49.630 --> 11:51.550
Fine apple.

11:52.530 --> 11:53.550
Strawberry.

11:54.910 --> 11:55.690
Very nice.

11:55.690 --> 11:56.650
Very, very nice.

11:56.650 --> 11:58.150
Now we're going to save this.

12:01.190 --> 12:04.550
And then we're going to go to the.

12:05.420 --> 12:13.040
Yeah, let's maybe let's make another method private void update route visuals.

12:13.490 --> 12:16.280
And over here we're going to do something simple.

12:16.280 --> 12:21.980
We're going to do maybe just anim set float of the route index.

12:21.980 --> 12:33.380
And because this enum over here has a number 012 and so on, we can just basically, uh turn this enum

12:33.380 --> 12:35.630
into a number and give it to the animator.

12:35.630 --> 12:38.570
So we open parentheses over here we type int.

12:38.570 --> 12:41.300
Then we type route type.

12:41.300 --> 12:47.090
Now it will take a fruit type, turn it into integer and then give this value to the animator if we

12:47.090 --> 12:47.600
need.

12:47.810 --> 12:49.820
And we can just maybe do it here.

12:50.370 --> 12:56.880
So if that is false, then update fruit visuals and return and it will just assign the default look

12:56.880 --> 12:58.560
that you want to have on the fruit.

12:58.920 --> 13:00.780
Uh, let's go back to the unity.

13:00.900 --> 13:03.450
Let's just make sure it's working first.

13:05.000 --> 13:06.260
So I'm going to go to scene.

13:06.260 --> 13:08.660
I'm going to choose here a cherry.

13:08.660 --> 13:12.590
And let's make sure Fruit Manager has this as a false value.

13:12.590 --> 13:17.270
And then we're going to go back to play mode and boom we've got a cherry.

13:17.270 --> 13:18.290
Very well.

13:18.290 --> 13:20.810
Now of course we could take.

13:22.160 --> 13:26.210
Prefab from here and just change the type over here.

13:26.210 --> 13:28.100
That would work, right?

13:28.100 --> 13:34.040
This is okay, but it is a bit difficult to do because you would have to cycle through all of the fruits

13:34.040 --> 13:36.710
and you would have to check how do they look.

13:36.710 --> 13:38.330
There is a different approach.

13:38.330 --> 13:42.890
It takes a bit of a time to set up, but it will definitely make it easier later on.

13:42.890 --> 13:48.800
So what we're going to do, uh, we could make the prefab variant just by dragging prefab over here

13:48.800 --> 13:50.660
and choosing prefab variant.

13:51.650 --> 13:57.920
But sometimes it can get messy with the prefab variants, because prefab variant does the copy of the

13:57.920 --> 13:58.910
original prefab.

13:58.910 --> 14:01.670
If you change something on a prefab variant, it's going to stay there.

14:01.670 --> 14:06.200
If you change something on the original prefab, it may affect prefab variant as well.

14:06.200 --> 14:10.190
And because of that, you can easily lose sight of what is happening with your fruits.

14:10.190 --> 14:15.800
I don't want you to have that experience, so let's just make a bunch of original prefabs over here.

14:16.300 --> 14:18.130
We're going to make a regional prefab.

14:22.170 --> 14:25.650
There's going to be a fruit banana.

14:25.710 --> 14:30.570
Then we need to change fruit type over here and we need to go inside of this prefab.

14:30.570 --> 14:35.070
Find an animator, click over here and type banana.

14:36.130 --> 14:37.750
Then select the sprite.

14:37.750 --> 14:40.090
And now you have two different prefabs.

14:40.090 --> 14:45.100
And if you want to place different fruits on the level, you can just drag them from here and place

14:45.100 --> 14:46.420
whatever you want to have.

14:46.450 --> 14:47.110
Okay.

14:47.500 --> 14:48.310
Very well.

14:48.310 --> 14:52.420
So now let me delete this and this I want to rename this to Fruit Apple.

14:53.210 --> 14:55.670
Let's make another original prefab.

14:55.670 --> 14:59.450
And also you should know that prefab variant would have a smaller size.

14:59.450 --> 15:05.360
Just for the sake of a explanation, I'm going to make one prefab variant, and you can go to show in

15:05.360 --> 15:07.880
explorer and see that.

15:08.910 --> 15:12.150
Uh, this is the original prefab.

15:12.150 --> 15:16.500
This is a prefab variant, and they have size of three kilobytes and six kilobytes.

15:16.500 --> 15:20.070
In the perspective of this video game, it does not matter at all.

15:20.070 --> 15:28.590
6kB or 3kB is just, you know, it's just funny because modern days, uh, games have an size of a 100

15:28.590 --> 15:29.730
GB or something like that.

15:29.730 --> 15:31.110
So please don't worry about that.

15:31.110 --> 15:32.850
Just use Prefab original.

15:32.850 --> 15:33.510
Okay?

15:33.720 --> 15:34.350
Yeah.

15:34.350 --> 15:36.420
Let me do this as a cherry.

15:36.420 --> 15:38.040
I'm going to rename it to cherry.

15:38.040 --> 15:43.140
And now that you know how to do these steps I'm going to do other fruits by myself.

15:43.140 --> 15:44.820
I'm just going to speed up the video.

15:45.300 --> 15:50.580
And I trust that you can set up this on your own because you know what to do here.

15:51.240 --> 15:54.120
Also in the end of this section, I'm going to give this project to you.

15:54.120 --> 15:56.430
So you're going to have this setup as well if you want to.

15:57.090 --> 15:57.840
All right.

15:57.840 --> 15:58.860
We've got this set up.

15:58.860 --> 16:00.150
It is very nice and clean.

16:00.150 --> 16:04.290
You can pull different fruits on the level and use them as you like.

16:04.290 --> 16:08.940
And if you want to have a random look for them, you just go to Game Manager and click this check mark

16:08.940 --> 16:09.510
over here.

16:09.510 --> 16:12.630
And they're going to get a random look every time you start the game.

16:12.630 --> 16:13.290
Very well.

16:13.290 --> 16:14.610
So this is cool for now.

16:14.610 --> 16:18.120
Thank you so much for paying attention and I'll see you in the next video.
