WEBVTT

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

00:00.530 --> 00:01.580
Welcome to the video.

00:01.580 --> 00:04.280
In this one I want to show you something very important.

00:04.280 --> 00:07.880
I want to show you how to create objects at runtime.

00:07.880 --> 00:13.640
This is very important feature that can help us to respawn player when he dies, or maybe respawn enemies

00:13.640 --> 00:18.350
if you want to, or some you know, other things that may be needed in your game in your project.

00:18.350 --> 00:20.660
So to explain how do we create an object?

00:20.660 --> 00:25.700
I want to create a visual effect for the fruit when we picking up the item.

00:26.540 --> 00:29.810
Let's go ahead to the graphics.

00:30.620 --> 00:32.150
Items fruits.

00:32.150 --> 00:35.030
And there is this collected visual effect.

00:35.030 --> 00:40.610
Let's take the first sprite and let's drag it over here I can see we need to do 16.

00:40.610 --> 00:41.150
It's okay.

00:41.150 --> 00:42.380
So we have this.

00:42.440 --> 00:48.380
Let's set it on a items layer and let's do minus one over here.

00:48.380 --> 00:50.810
So it will be behind the fruit itself.

00:50.810 --> 00:55.550
If we set order in layer zero it will always depend on the hierarchy.

00:55.550 --> 00:58.370
If we set it to one it will be always above the fruit.

00:58.370 --> 01:00.020
But we want it to be behind the fruit.

01:00.020 --> 01:01.460
So I'm going to do minus one.

01:01.460 --> 01:02.420
Very well.

01:02.420 --> 01:07.460
Now, uh, I think we do need to make a child object for this one because we're just going to have visual

01:07.460 --> 01:08.300
effect, nothing else.

01:08.300 --> 01:10.580
So we can place animator on the same object.

01:10.580 --> 01:12.260
Let's just name it as pickup.

01:12.260 --> 01:13.100
We fix.

01:13.100 --> 01:15.950
So this is a visual effect for the item pickup.

01:16.370 --> 01:19.340
And let's add an animator.

01:20.200 --> 01:21.130
Uh, controller?

01:21.160 --> 01:23.260
Now we need a controller over here.

01:23.260 --> 01:29.200
So I'm going to go to Assets Animation and create animator controller.

01:29.960 --> 01:32.480
Pick up the fix.

01:32.690 --> 01:37.040
AC okay, now let's drag it on the visual effect over here.

01:37.040 --> 01:38.330
So this is the thing.

01:38.330 --> 01:40.430
That's the only thing we need for now.

01:40.430 --> 01:42.590
Now let's go.

01:43.620 --> 01:48.480
Uh, to the animation, click Create New Clip and we're going to do.

01:49.230 --> 01:49.950
Fruit.

01:50.600 --> 01:52.040
Pick up my fix.

01:52.310 --> 01:53.810
Okay, nice.

01:53.810 --> 01:59.210
And now we need to go back to graphics and grab those sprites.

02:00.090 --> 02:02.160
And put them on the animation.

02:02.160 --> 02:04.050
Sample rate will be 20.

02:05.910 --> 02:07.170
And this is how it looks.

02:08.170 --> 02:08.860
Nice, right?

02:08.860 --> 02:11.110
So this is how it looks when we pick it up the item.

02:12.050 --> 02:17.120
Uh, we also would have to make it transparent once it is done.

02:17.120 --> 02:21.350
Like once we don't have anything to display once animation is over.

02:21.350 --> 02:25.820
And we could disable this visual effect right after the last frame.

02:25.820 --> 02:29.870
But for that, we would have to make a script, which I don't really want to do, because that would

02:29.870 --> 02:30.650
be just for one.

02:30.650 --> 02:34.100
Actually, it would be a good idea because I can show you how to use animation events.

02:34.100 --> 02:35.120
So let's do it.

02:35.420 --> 02:38.240
First, let's create the visual effect.

02:38.480 --> 02:42.860
After we pick up the fruit for that we need to make it as a prefab.

02:42.860 --> 02:44.420
So I'm going to go to prefab folder.

02:44.420 --> 02:46.940
And I'm going to drag this visual effect over here.

02:46.940 --> 02:53.150
Now whenever we want to use a visual effect we can just create this object at runtime.

02:53.800 --> 02:55.270
This is how we do it.

02:55.270 --> 02:57.430
We go to the fruit apple.

02:57.430 --> 02:59.230
Let's go to the script.

02:59.230 --> 03:00.490
Actually, it can be any fruit.

03:00.490 --> 03:02.140
I'm just choosing this one.

03:02.140 --> 03:04.450
And over here we need serialize field.

03:04.630 --> 03:06.220
Private GameObject.

03:07.250 --> 03:10.640
Pick up very fix very well.

03:10.640 --> 03:15.020
So now we have a reference to the object that would serve as a visual effect.

03:15.020 --> 03:17.510
Now once we are.

03:18.170 --> 03:19.130
Done with the fruit.

03:19.130 --> 03:22.970
Once we destroy in it, we can create a visual effect.

03:23.900 --> 03:26.150
For that, we can use instantiate.

03:27.390 --> 03:32.880
And usually I like to get a local variable of the game object in case we need to do some manipulations

03:32.880 --> 03:33.180
with it.

03:33.180 --> 03:34.920
So I'm going to do game object.

03:35.970 --> 03:39.210
New fix equals to instantiate.

03:39.660 --> 03:44.370
Pick up a fix so this one will create object at runtime.

03:44.370 --> 03:48.630
Then if you need to do something with it, you can do a new fix and do some stuff.

03:48.630 --> 03:51.930
For example, I want to destroy this effect after one second.

03:51.930 --> 03:59.460
So I'm going to do destroy a new way fix, and I'm going to give it a delay of maybe even 0.5 seconds.

03:59.460 --> 04:00.420
So that's it.

04:00.420 --> 04:02.400
So now this will create an object.

04:02.880 --> 04:05.700
And I think we can go ahead and just see how does this work.

04:07.130 --> 04:08.720
Let's go to unity.

04:10.130 --> 04:13.850
Now we would have to assign this visual effect to all of the prefabs we have.

04:13.850 --> 04:16.010
So I'm going to go to prefab folder.

04:16.900 --> 04:20.710
Select all of the fruits and just drag this effect over here.

04:20.980 --> 04:21.850
Very well.

04:21.850 --> 04:25.390
Now these all have in their visual effect.

04:25.390 --> 04:28.390
And I'm going to go ahead and try to pick up the items.

04:29.720 --> 04:32.720
Now we can see what is happening there if you don't destroy the object.

04:32.720 --> 04:34.640
So I'm going to just switch it off for now.

04:35.290 --> 04:36.550
And let's go.

04:41.370 --> 04:43.320
And we created a visual effect.

04:43.320 --> 04:46.470
The only problem is it is created over here.

04:48.190 --> 04:50.320
Not at the position of the fruit.

04:50.320 --> 04:52.540
To fix that, we need to go to the script.

04:52.540 --> 04:58.240
And over here in the parenthesis we can pass a transform position.

04:58.240 --> 05:03.160
And because we type transform position over here, it will take position of the fruit.

05:04.820 --> 05:07.970
Because we instantiate an object inside of a fruit script.

05:07.970 --> 05:13.970
So transform position of a fruit script is a position of the object right position of the fruit.

05:13.970 --> 05:16.040
So we type here transform position.

05:16.040 --> 05:18.590
And now we need to pass here a rotation.

05:18.590 --> 05:20.570
That is how it's supposed to be.

05:20.570 --> 05:26.810
So I'm going to type quaternion identity which basically means do not change the rotation.

05:26.810 --> 05:27.920
This is how it is.

05:28.340 --> 05:29.840
So I'm going to save this.

05:30.880 --> 05:31.840
Going to go back.

05:34.800 --> 05:37.890
Go to play mode and I'm going to pick up the.

05:37.890 --> 05:39.420
Let me switch off this one.

05:40.940 --> 05:45.950
I'm going to pick up the fruit and we can see visual effect was created and the fruit itself.

05:45.950 --> 05:48.950
The only issue is it was played a couple of times.

05:48.950 --> 05:49.580
Here.

05:49.580 --> 05:51.380
We have a couple of ways to solve it.

05:51.380 --> 05:53.720
One of them we can go to asset.

05:54.410 --> 06:02.420
Animations, find the pickup animation through the pickup wave fix, and we can uncheck loop time over

06:02.420 --> 06:02.990
here.

06:02.990 --> 06:05.480
Because of that, it will be played only once.

06:05.480 --> 06:06.110
Let's go ahead.

06:06.110 --> 06:07.640
I'm going to show you how does it look.

06:08.120 --> 06:09.470
So here it is.

06:10.350 --> 06:17.160
It will stop at the last frame, and we could find an empty sprite somewhere and put it in the end of

06:17.160 --> 06:21.510
the animation so it will become invisible once visual effect is played.

06:21.960 --> 06:24.240
That is one of the ways to do it.

06:24.240 --> 06:30.420
Let me go ahead and quickly find some empty sprite I think we can just go to.

06:31.500 --> 06:32.580
Let's go to items.

06:32.580 --> 06:33.180
Fruits.

06:33.210 --> 06:34.590
Let's take this collected.

06:34.590 --> 06:36.660
I'm going to duplicate it.

06:36.960 --> 06:39.270
Let's just do copy paste.

06:39.390 --> 06:41.460
I'm going to name it as empty.

06:41.640 --> 06:47.550
And in the sprite editor over here, instead of all of this, we can simply.

06:48.390 --> 06:52.740
Take this area where we have nothing.

06:53.100 --> 06:55.320
So now we're going to hit apply.

06:55.350 --> 07:00.960
This will be an empty sprite over here, and we can go to the end of the animation and place an empty

07:00.960 --> 07:02.340
sprite over here.

07:02.460 --> 07:06.540
So now it will play the animation and then it will become invisible.

07:06.540 --> 07:09.720
And then we can destroy it whenever we are ready.

07:09.720 --> 07:11.700
And this is how it would look.

07:16.920 --> 07:18.960
Yeah, and this works pretty well.

07:18.960 --> 07:24.120
And to be honest, if I would just be making projects for myself, I would do it this way because I

07:24.120 --> 07:25.590
kind of like how tricky it is.

07:25.590 --> 07:30.630
We don't need to do anything extra, but this is a great chance for me to show you how to work with

07:30.630 --> 07:33.150
animation events, and that's what I'm going to do.

07:33.570 --> 07:36.720
Let's go to the scripts and let's make.

07:37.410 --> 07:38.850
Uh C sharp script.

07:39.610 --> 07:40.150
Road.

07:40.150 --> 07:42.520
Pick up the fix.

07:43.940 --> 07:44.810
Just like that.

07:45.280 --> 07:52.840
And over here we're going to make a method public void destroy me, in which we're going to just type

07:52.840 --> 07:56.050
destroy game object just like that.

07:56.050 --> 07:57.640
That's all that we need.

08:00.620 --> 08:05.090
And then if we go to pick up visual effect over here and put the script.

08:05.880 --> 08:08.190
On the GameObject over here.

08:08.190 --> 08:15.060
Then in the animation over here, we can go to the frame after the last frame over here.

08:15.910 --> 08:19.210
And over here we can click Add Event.

08:19.920 --> 08:23.880
We added an event and over here for this event we can choose function.

08:23.880 --> 08:29.040
And because we have this fruit pickup visual effect script, we can go to methods over here and find

08:29.040 --> 08:31.770
Destroy Me method that we just created.

08:31.770 --> 08:33.450
This can be useful for many cases.

08:33.450 --> 08:35.520
This is just an example on how to use events.

08:35.520 --> 08:37.170
This function can say anything.

08:37.170 --> 08:38.850
You can update the variables.

08:38.850 --> 08:40.980
You know so many things can be done here.

08:40.980 --> 08:44.070
So we're going to assign this method over here.

08:44.070 --> 08:48.390
And because of that this method will be called exactly at this frame.

08:48.390 --> 08:53.580
And because of that fruits will be destroyed even if we don't set timer.

08:54.120 --> 08:58.800
Uh, over here, let me just set five for example.

08:58.800 --> 09:03.450
So it should be destroyed after five seconds, but you'll see it will be destroyed once animation is

09:03.450 --> 09:04.140
over.

09:06.340 --> 09:08.500
And there it goes.

09:08.770 --> 09:09.910
Works like a charm.

09:17.300 --> 09:18.260
Yeah, we need to.

09:21.160 --> 09:24.730
We need to do apply overrides to everything.

09:24.730 --> 09:31.600
So this, uh, change will be applied to all of the visual effects, even in the prefab folder over

09:31.600 --> 09:34.390
here, because this one had none.

09:34.390 --> 09:38.860
So over here we can see we have an event and it is assigned okay.

09:40.020 --> 09:40.860
Now let's go back.

09:40.860 --> 09:42.180
We can delete this one.

09:43.310 --> 09:47.030
We can clear the console and let's go to game window.

09:47.330 --> 09:52.100
And I'm going to pick up fruit and visual effect played and destroyed.

09:53.350 --> 09:55.240
So I showed you two approaches.

09:55.240 --> 09:56.020
How we can do it.

09:56.020 --> 09:57.460
Which one to choose is up to you.

09:57.460 --> 10:00.760
I just want to teach you as many things as possible.

10:00.760 --> 10:01.270
All right.

10:01.270 --> 10:05.800
For educational purpose, I'm going to keep this script in the script folder.

10:05.800 --> 10:10.420
Let it be there and let's use animation event because it doesn't really matter what we choose here.

10:12.610 --> 10:19.660
But you should know that we could just use a delay over here and empty sprite in the end of the animation.

10:19.660 --> 10:22.780
Yeah, well, for now, I'm just going to remove it.

10:23.770 --> 10:25.600
Like so and I guess that's it.

10:25.600 --> 10:27.820
So this is how you use instantiate.

10:27.820 --> 10:31.450
And we're going to use it to respawn the player in a couple of videos.

10:31.450 --> 10:33.610
So yeah, I guess this is it.

10:33.610 --> 10:35.290
Thank you so much for paying attention.

10:35.290 --> 10:37.000
And I'll see you in the next video.
