WEBVTT

00:00.050 --> 00:00.500
Hello guys.

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

00:01.550 --> 00:07.400
So now we know how Ontriggerenter works and we're going to use it to make collectable items.

00:07.880 --> 00:10.310
Let's go to the graphic items.

00:10.310 --> 00:12.470
And over here we have fruits.

00:12.620 --> 00:14.810
Let's get one of the fruits.

00:14.810 --> 00:19.130
Actually we need to select all of them and make pixel per unit 16.

00:19.130 --> 00:21.380
And then we're going to work with one of them.

00:21.650 --> 00:25.910
And after that I'm going to show you how to apply different skin to different fruits.

00:25.910 --> 00:28.520
For starters let's just work with one of them.

00:29.160 --> 00:35.280
Let's set certain layer to items and let's name it as can we?

00:35.280 --> 00:37.740
Yeah, let's keep visuals as a child object.

00:37.740 --> 00:41.460
So let's call it animator and let's make an empty parent.

00:41.820 --> 00:43.560
I'm going to call it fruit.

00:43.920 --> 00:44.730
Very well.

00:44.730 --> 00:47.790
Now let's add component new script.

00:47.790 --> 00:49.410
And I'm going to call it fruit.

00:49.410 --> 00:54.960
But if you have coins in your game or something like that, you can just name it as coin or you know,

00:54.960 --> 00:55.950
anything you like.

00:56.710 --> 00:59.170
So let's create and add this component.

01:00.400 --> 01:04.450
Now let's take the script and drag it inside of a script folder so it's cleaner.

01:04.450 --> 01:07.630
And after that we can go inside of this fruit.

01:11.290 --> 01:16.990
And first, what we need to do is to get access to the game manager, because now we don't have any

01:16.990 --> 01:17.830
other objects.

01:17.830 --> 01:21.520
So I want to keep score of the fruits we have on the game manager.

01:21.520 --> 01:26.800
Again, we can use an instance, but also we can create a variable and assign it through the instance.

01:26.800 --> 01:27.940
This is how it works.

01:27.940 --> 01:29.230
We're going to do private.

01:30.030 --> 01:30.990
Game manager.

01:31.020 --> 01:32.160
Game manager.

01:32.280 --> 01:35.280
Then I want to do this in the start, not in the awake.

01:35.280 --> 01:41.010
Let's do game manager equals to game manager dot instance.

01:41.010 --> 01:47.610
I'm doing this in the start and not in the awake because uh, game manager is assigned in the awake.

01:47.610 --> 01:55.140
And if we get component in the awake as well, it may happen that awake of this script will be called

01:55.140 --> 01:56.670
before the game manager.

01:56.670 --> 02:02.040
And because it will be called before this, awake will not have time to assign the instance, and it's

02:02.040 --> 02:04.500
going to be null when we train to get it.

02:04.500 --> 02:05.130
Okay.

02:05.130 --> 02:13.740
And now let's go to Game Manager and let's make, uh, public int fruits collected.

02:15.060 --> 02:17.070
And I want to make a public.

02:17.810 --> 02:23.030
Void add route and this method will do one simple thing.

02:23.030 --> 02:28.790
It's going to do fruits collected plus plus when we type in plus plus just like that, it means plus

02:28.790 --> 02:29.330
one.

02:29.960 --> 02:34.520
And the same thing would be if we would type fruits collected plus one the very same.

02:34.520 --> 02:36.020
But it is easier that way.

02:36.020 --> 02:38.870
So let's do plus plus okay.

02:38.870 --> 02:42.950
Now on the fruit we're going to do on trigger enter to D.

02:43.010 --> 02:48.290
And then we're going to check player player getcomponent of a player.

02:48.290 --> 02:54.380
If player not equals to null then game manager and route just like that.

02:55.550 --> 02:56.000
Okay.

02:56.000 --> 02:57.920
That's all what we need to do for the script.

02:57.950 --> 03:00.380
Now let's go back here.

03:00.740 --> 03:02.870
Let's add a circle.

03:02.870 --> 03:03.680
Collider.

03:03.710 --> 03:04.340
Circle.

03:04.340 --> 03:05.300
Collider 2D.

03:05.930 --> 03:08.150
Here it is, I think.

03:08.800 --> 03:09.880
Yeah, should be fine.

03:09.880 --> 03:14.230
It's fine if Circle Collider is a bit bigger because we need to make it easy for the player.

03:14.230 --> 03:16.420
In fact, we can make it even bigger.

03:16.420 --> 03:18.040
Let's make it 0.75.

03:18.040 --> 03:20.020
Maybe so player will pick it up.

03:20.170 --> 03:21.250
Uh, anyway.

03:21.640 --> 03:22.390
Nice.

03:22.390 --> 03:25.090
Now let's select this trigger over here.

03:25.090 --> 03:27.700
And now we need to play animation for the fruit.

03:27.700 --> 03:30.460
So we're going to go to Assets Animations.

03:30.460 --> 03:35.740
And let's make folder for all of these folder layer.

03:36.160 --> 03:38.020
We're going to drag it like so.

03:38.670 --> 03:40.230
And then let's make.

03:41.340 --> 03:43.650
Just animate our controller this time.

03:43.890 --> 03:45.780
Root AC root.

03:45.780 --> 03:46.380
Animator.

03:46.380 --> 03:47.190
Controller.

03:47.670 --> 03:51.180
Now let's put it on the animator over here.

03:51.180 --> 03:52.140
Very nice.

03:52.140 --> 03:55.560
Now we need to get reference to this controller.

03:55.560 --> 03:57.990
We can go to the script over here.

03:58.680 --> 03:59.850
And make.

04:00.530 --> 04:02.360
Private animator.

04:03.000 --> 04:07.980
Annum and then we can do annum equals to get component of the animator.

04:08.780 --> 04:12.140
Actually would be better to do it in their wake, just in case.

04:13.740 --> 04:15.360
So let's do it like so.

04:17.460 --> 04:22.320
I know it looks a bit extra that we have two methods where we're getting different things, but let's

04:22.320 --> 04:23.010
keep it that way.

04:23.010 --> 04:24.780
It's just safer that way.

04:24.810 --> 04:26.250
Now we have animator.

04:26.250 --> 04:27.690
Are we going to need it in the future?

04:27.690 --> 04:28.080
Right.

04:28.080 --> 04:35.520
So let's just keep it at that and let's go to the graphics folder and get some animation for the animator

04:36.000 --> 04:39.150
I'm going to go to graphics items fruits.

04:39.150 --> 04:41.490
These are the fruits we're going to use.

04:42.360 --> 04:44.700
I'm going to select animator of the animator.

04:44.730 --> 04:47.190
Go to animation and create new clip.

04:48.960 --> 04:50.040
In the animations.

04:50.040 --> 04:53.430
I'm going to make a fruit apple.

04:54.150 --> 04:55.380
Let's save this.

04:55.380 --> 05:00.630
Let's take sprites of an apple and just drag it like so.

05:01.110 --> 05:05.580
Also, I learned that it's better to set sample array 20 looks better.

05:05.580 --> 05:08.400
So we're going to change sample rate for everything but a bit later.

05:08.400 --> 05:10.230
As of now let's just do it for the fruit.

05:10.410 --> 05:12.660
So this looks good I like it.

05:13.810 --> 05:14.980
Very nice.

05:14.980 --> 05:18.940
Um, there's going to be default animation, so it's going to be played anyway.

05:18.940 --> 05:21.250
I just want to see how it all worked out.

05:21.640 --> 05:23.050
Let's take the fruit.

05:23.050 --> 05:27.460
Let's place it somewhere here and let's go to play mode.

05:29.420 --> 05:30.530
So now we have a fruit.

05:30.530 --> 05:30.830
It is.

05:30.830 --> 05:31.610
They're bouncing.

05:31.610 --> 05:33.290
I'm going to go there and pick it up.

05:34.770 --> 05:36.240
I did not pick it up.

05:36.690 --> 05:37.620
Why not?

05:38.610 --> 05:40.230
Oh, I did actually.

05:40.230 --> 05:40.920
I'm sorry.

05:40.920 --> 05:42.000
We did pick up the fruit.

05:42.000 --> 05:45.000
It is two, three, four, five.

05:45.000 --> 05:46.500
We did not destroy the fruit.

05:46.500 --> 05:47.610
That's the problem.

05:47.880 --> 05:56.190
Let's go to the script and we're going to open curly brackets and do destroy game object.

05:56.190 --> 05:57.810
So it's going to be destroyed as well.

05:58.260 --> 05:59.010
Very nice.

05:59.010 --> 06:00.000
Let's go back.

06:04.620 --> 06:06.300
And boom.

06:06.300 --> 06:09.930
I have one fruit on the game manager over here.

06:09.930 --> 06:10.890
Very cool.

06:10.920 --> 06:11.760
Hello guys.

06:11.760 --> 06:14.490
I've noticed a mistake over here in the wake.

06:14.490 --> 06:22.290
It should be getcomponent in children, not getcomponent because animator is a child of a fruit GameObject.

06:22.290 --> 06:23.700
All right, that's good.

06:23.700 --> 06:26.760
Thank you so much for paying attention and I'll see you in the next video.
