WEBVTT

00:00.000 --> 00:00.450
Hello guys.

00:00.450 --> 00:01.440
Welcome to the video.

00:01.440 --> 00:04.260
So in this one I want to make respond for the player.

00:04.260 --> 00:08.550
So when he dies he can get on the starting position or something like that.

00:08.550 --> 00:13.260
There are a couple of things we need to do to make it work, but they all are very simple.

00:13.260 --> 00:13.920
Okay.

00:16.070 --> 00:20.480
First, let's make a respawn point so we can use it.

00:20.510 --> 00:22.220
I'm going to go to graphics.

00:22.520 --> 00:27.260
Uh items checkpoints start over here.

00:27.260 --> 00:29.930
We need to be sure they have pixel per unit 16.

00:29.930 --> 00:32.240
So let's, uh, select them.

00:32.920 --> 00:35.740
And make sure you have pixel per unit 16 over here.

00:35.770 --> 00:38.710
Now let's take sprite of the idle one.

00:38.710 --> 00:39.820
Just like that.

00:44.970 --> 00:50.100
I think we can take it and choose layers layer for this one.

00:50.950 --> 00:51.250
Now.

00:51.250 --> 00:51.490
Wait.

00:51.490 --> 00:56.530
Maybe it should be like, maybe enemy should pass by this one and we should see the enemies.

00:56.620 --> 01:00.070
So I think let's put it on the ground.

01:01.770 --> 01:02.340
Let's do it.

01:02.340 --> 01:04.260
Order in layer one.

01:04.950 --> 01:05.760
Okay.

01:06.170 --> 01:06.890
Yeah.

01:07.780 --> 01:08.920
Or is it item.

01:08.920 --> 01:09.370
Yeah.

01:09.370 --> 01:12.520
It's an I think it's an item.

01:13.360 --> 01:13.780
Okay.

01:13.780 --> 01:14.950
Let's keep it as an item.

01:14.950 --> 01:18.520
And then once we have enemies we're going to see what is better working for this one.

01:18.760 --> 01:23.230
And just to be sure it is behind other fruits, I'm going to do minus one here.

01:23.230 --> 01:23.890
Okay.

01:23.890 --> 01:29.800
So now we have a starting point that is on the layer of items I mean certain layer.

01:29.800 --> 01:35.320
And now we need to rename this to something like Start Point.

01:35.320 --> 01:37.810
This is the place where we're going to start the game.

01:39.760 --> 01:43.690
And for this one, we don't need anything else for now, so this is just fine.

01:43.720 --> 01:45.880
Let's go to the game manager.

01:45.880 --> 01:47.380
We need to open the script.

01:47.380 --> 01:49.480
Or actually, let me show you something.

01:49.570 --> 01:50.170
Over here.

01:50.170 --> 01:54.370
We have reference to the player, but when player dies.

01:56.170 --> 02:00.520
This reference is missing, so we need to refill this reference as well.

02:01.480 --> 02:02.920
Let's go back to script.

02:05.070 --> 02:06.870
And over here I'm going to do header.

02:09.310 --> 02:10.210
Player.

02:10.210 --> 02:11.890
Then we need serialize field.

02:11.890 --> 02:13.330
Private GameObject.

02:18.070 --> 02:19.870
I'm going to name it player.

02:21.940 --> 02:22.690
Prefab.

02:22.690 --> 02:26.290
So we have prefab of a player and we can respawn him.

02:28.730 --> 02:29.840
Then we need to realize.

02:29.840 --> 02:30.320
Field.

02:30.320 --> 02:30.830
Private.

02:30.830 --> 02:31.790
Transform.

02:32.540 --> 02:37.670
Respawn point, so we know position where a player should be after he responds.

02:37.880 --> 02:42.830
Now let's go ahead over here and make a public void.

02:42.830 --> 02:47.480
Respawn player and inside we need to do GameObject.

02:47.630 --> 02:56.420
New player equals to instantiate player prefab, add respawn point, dot position.

02:56.420 --> 03:02.000
And we don't need to type dot transform here because respawn point is a transform itself.

03:02.570 --> 03:04.580
And then we're not going to change rotation.

03:04.580 --> 03:06.920
So it's going to be quaternion dot identity.

03:07.810 --> 03:08.770
Very nice.

03:08.770 --> 03:13.390
Now, as you remember, I said that we need to refill this parameter over here because we're going to

03:13.390 --> 03:15.190
use it later on in different places.

03:15.190 --> 03:23.890
So one of the ways to do that would be to change this game object to just player over here.

03:25.250 --> 03:25.460
Oh.

03:25.460 --> 03:26.330
I'm sorry.

03:27.230 --> 03:28.040
Just like that.

03:28.040 --> 03:31.010
So now this reference is this reference.

03:31.010 --> 03:32.810
And we're going to instantiate the object.

03:32.810 --> 03:35.000
But this one is a game object.

03:36.110 --> 03:40.040
But nevertheless we can type dot get component.

03:41.300 --> 03:49.130
Of a player and it would create an object, and also it would get component and assign it to this variable

03:49.130 --> 03:50.030
over here.

03:50.030 --> 03:51.440
But I don't like it.

03:51.440 --> 03:55.970
I don't like to do it this way because it's kind of, I would say cluttered.

03:56.000 --> 03:57.920
I want it to be more readable.

03:57.920 --> 04:01.160
That's why I'm going to do this.

04:01.850 --> 04:06.230
I'm going to make it as a GameObject new player.

04:06.230 --> 04:10.460
And then I'm going to do player equals to new player.

04:11.600 --> 04:14.480
Get component of a player and that's it.

04:14.480 --> 04:16.340
This is very nice and very clean.

04:17.580 --> 04:21.510
Now let's save this and let's go to Dead Zone.

04:22.080 --> 04:29.190
And over here, after we die, we're going to do game manager dot instance respond player.

04:29.190 --> 04:30.180
Just that.

04:30.180 --> 04:33.120
Now let's go ahead and see how does this work.

04:34.260 --> 04:36.240
Oh, sorry, I forgot to do something.

04:37.220 --> 04:41.570
We need to take this start point, move it to respawn point over here.

04:41.630 --> 04:44.720
Also, maybe let's take Game Manager and move it all the way above.

04:45.140 --> 04:49.700
Then we need to go to assets prefab and we need to make prefab of a player.

04:49.700 --> 04:51.470
We can just drag it like so.

04:53.680 --> 04:58.210
Then on a game manager, we're going to take the prefab and give it over here.

04:58.780 --> 04:59.650
Very cool.

04:59.680 --> 05:04.540
Now let's go to the game mode and let's just jump into a dead zone.

05:04.540 --> 05:07.780
And we have a new player every time we die.

05:08.560 --> 05:09.790
Very cool.

05:10.620 --> 05:14.730
Now I want to make it a bit prettier because I don't like how it looks right now.

05:14.730 --> 05:19.680
One of the ways to do that would be to add a delay for eastbound.

05:20.560 --> 05:23.500
So let's make a variable serialized field.

05:23.500 --> 05:24.790
Private float.

05:24.790 --> 05:25.810
Respond.

05:25.810 --> 05:26.590
Delay.

05:28.020 --> 05:39.390
Then we're going to go over here and make a private I enumerator respawn CoroCoro coroutine.

05:39.390 --> 05:46.230
And over here we're going to do yield return new wait for a seconds of respawn delay.

05:46.440 --> 05:48.600
And then we're going to take these two lines of code.

05:48.600 --> 05:49.620
Cut them out.

05:50.740 --> 05:52.240
And place them in here.

05:52.540 --> 05:55.300
And this method would only start a coroutine.

05:56.720 --> 05:58.430
Startcoroutine.

05:58.460 --> 05:59.450
Respawn.

05:59.450 --> 06:00.440
Coroutine.

06:00.590 --> 06:03.830
So now we respawning player with a coroutine.

06:04.400 --> 06:08.630
But this coroutine is private, so we cannot call it outside of a script.

06:08.870 --> 06:12.950
That's why we have this public method that is called respawn player.

06:12.950 --> 06:15.800
And this method will start a coroutine.

06:15.830 --> 06:18.170
Now let's go ahead and check the delay.

06:20.310 --> 06:23.190
I'm going to give it a value of 1.5 seconds.

06:23.190 --> 06:25.410
That's, uh, good enough, I think.

06:25.950 --> 06:27.360
And you.

06:28.330 --> 06:29.440
You okay?

06:29.440 --> 06:30.430
Very cool.

06:30.460 --> 06:32.980
Now let's make visuals for the respawn.

06:32.980 --> 06:38.200
This time we do need to instantiate any visual effect because I have a better way.

06:38.230 --> 06:39.730
We're going to go to assets.

06:39.940 --> 06:41.110
Uh, sorry.

06:41.110 --> 06:45.490
We're going to go to Players Animator and we're going to create new clip.

06:45.880 --> 06:49.330
Then we're going to go to Animations Folder Player.

06:49.330 --> 06:52.600
And over here we're going to do player respawn.

06:53.600 --> 06:54.350
Nice.

06:54.350 --> 06:55.790
Now let's save it.

06:57.980 --> 07:05.330
We're going to go to graphics main character, take this appearance sprites and place them over here.

07:05.330 --> 07:07.310
Sample rate will be 20.

07:09.530 --> 07:10.100
Yeah.

07:10.100 --> 07:11.060
Very cool.

07:11.060 --> 07:17.000
And on the animator, we're going to take this newly created animation and we're going to click right

07:17.000 --> 07:19.490
button and choose Set Default.

07:21.900 --> 07:28.530
Now this is a default animation that is played when object is created or, you know, just starting

07:28.530 --> 07:29.490
the life cycle.

07:29.490 --> 07:32.610
And from here we're going to make transition to idle move.

07:33.470 --> 07:35.540
With an exit time one.

07:36.960 --> 07:39.120
Transition duration zero.

07:40.060 --> 07:40.780
Okay.

07:40.780 --> 07:44.650
Now let's go back to Game Window.

07:44.650 --> 07:47.230
And we can see this effect at the start.

07:47.530 --> 07:49.180
And even when I die.

07:50.530 --> 07:51.370
It is there.

07:51.370 --> 07:58.510
The only issue is that I don't really like how he's affected by gravity and how he can move while he's

07:58.510 --> 08:00.910
respawning, just in that animation.

08:00.910 --> 08:02.230
So I want to fix it.

08:02.230 --> 08:03.700
You're going to go to player.

08:04.630 --> 08:11.290
Over here, and let's make some sort of a method that says, actually, I think we can just make a method

08:11.290 --> 08:14.890
that would say something like respawn is finished or not.

08:14.920 --> 08:16.210
Just simple as that.

08:16.240 --> 08:16.900
What do we need to do?

08:16.900 --> 08:17.740
Let's do it.

08:17.830 --> 08:21.160
I'm going to go ahead somewhere here.

08:22.230 --> 08:27.450
And I want to make a boolean that would say if I can move, we already have is knocked, but I don't

08:27.450 --> 08:31.500
want to use is knocked for the respawn because it just has a different name.

08:31.500 --> 08:33.930
So it can be very confusing why it is there.

08:33.930 --> 08:37.380
I'm just going to go ahead over here and make a private bool.

08:38.480 --> 08:40.640
Can be controlled.

08:41.560 --> 08:43.450
Maybe even I'll place it above.

08:43.450 --> 08:44.200
Just like so.

08:44.200 --> 08:44.860
Yeah.

08:45.040 --> 08:48.310
And by default it's going to be false.

08:49.740 --> 08:51.750
Let's save this then.

08:51.750 --> 08:58.080
Let's go ahead and below the update I'm going to do public void respawn finished.

08:58.440 --> 09:03.300
And inside of this method we want to check if it is finished or not.

09:03.300 --> 09:07.590
And actually instead of checking we can pass the value into the parentheses.

09:07.590 --> 09:10.620
Let's just type away here bool finished.

09:10.620 --> 09:14.880
And now whenever we call this method we can just pass boolean through.

09:14.880 --> 09:16.920
The parentheses could be something like this.

09:16.920 --> 09:22.140
Respawn finished true or respond finished false.

09:22.140 --> 09:24.510
And this value would be passed over here.

09:24.510 --> 09:28.500
And now we can use this value over here as a local variable.

09:28.500 --> 09:29.550
Very cool.

09:31.140 --> 09:38.280
I want to make a float gravity scale and I would make it equals to arb gravity scale.

09:38.760 --> 09:40.260
We're just going to save the value.

09:40.500 --> 09:42.510
Then over here I'm going to do.

09:42.510 --> 09:52.770
If finished then ARB gravity scale equals to gravity scale and can be controlled equals to true.

09:52.770 --> 09:53.670
Else.

09:54.850 --> 09:56.110
We're going to do.

09:56.890 --> 10:02.350
Our big gravity scale equals to zero can be controlled, equals to false.

10:03.380 --> 10:04.400
Just like that.

10:05.930 --> 10:09.530
And then over here before is knocked.

10:09.530 --> 10:15.410
We're going to do if can be controlled equals to false then return.

10:15.680 --> 10:16.640
Simple as that.

10:16.640 --> 10:20.000
Also it would be a good idea to disable Collider I guess.

10:20.000 --> 10:23.870
So we don't get knocked while we respawn in just in case.

10:23.870 --> 10:25.190
So.

10:26.040 --> 10:26.550
Yeah.

10:26.550 --> 10:31.590
Let's go ahead over here and get private Capsule Collider 2D.

10:31.590 --> 10:33.060
I'm going to call it CD.

10:35.100 --> 10:42.120
Then in the awake we're going to do CD equals to get component of a capsule collider 2D.

10:43.630 --> 10:45.100
Then.

10:45.610 --> 10:46.060
I'm sorry.

10:46.060 --> 10:46.960
That was too quick.

10:46.960 --> 10:47.170
Yeah.

10:48.120 --> 10:48.930
Just like that.

10:50.460 --> 10:52.800
Then inside of this method I would do.

10:54.000 --> 10:57.330
I CD enabled, we can disable a component.

10:57.690 --> 10:58.740
Equals to true.

10:58.740 --> 11:02.670
And over here it would be CD enabled equals to false.

11:03.300 --> 11:04.170
Very nice.

11:04.170 --> 11:05.910
Now we have a goal.

11:05.910 --> 11:12.360
We need to call this method at the beginning of the respawn animation and set it to false.

11:12.360 --> 11:16.530
And then oh by the way, this would not work.

11:16.980 --> 11:18.600
This would not work.

11:18.960 --> 11:19.410
Um.

11:20.400 --> 11:21.030
Yeah.

11:21.510 --> 11:23.280
We need to do something else here.

11:23.310 --> 11:27.150
Let's go and make private float.

11:27.180 --> 11:29.970
Default gravity scale.

11:30.000 --> 11:31.950
I'll just place it into a movement.

11:32.070 --> 11:32.490
Yeah.

11:32.490 --> 11:33.900
We cannot make local variable.

11:33.900 --> 11:35.520
I'm going to quickly explain why.

11:37.830 --> 11:38.880
Over here.

11:38.880 --> 11:42.420
First we would call it with a value of false over here.

11:42.420 --> 11:43.080
Yeah.

11:43.080 --> 11:44.190
And.

11:46.000 --> 11:50.200
If we do that, we're going to say our gravity scale equals to current gravity scale.

11:50.200 --> 11:53.320
And next time we call this method with a value of.

11:54.040 --> 11:54.790
True.

11:56.050 --> 12:01.570
This value would be reassigned again, because this is called in the beginning of the method, and because

12:01.570 --> 12:08.680
we have our great scale zero because we set it to zero when it was false, this would assign it to zero,

12:09.160 --> 12:12.130
and then this value would be used over here as a zero.

12:12.160 --> 12:12.580
Yes.

12:12.580 --> 12:14.920
So we cannot do a local variable.

12:15.070 --> 12:16.390
We need to.

12:17.620 --> 12:18.310
Do.

12:19.120 --> 12:21.250
Default gravity scale over here.

12:23.100 --> 12:24.150
Very nice.

12:24.150 --> 12:26.880
Now we can go to the start method.

12:26.880 --> 12:28.230
We don't have a start method.

12:28.230 --> 12:29.160
We're going to make it.

12:30.040 --> 12:35.080
And in the start I'm going to do default gravity scale equals to ARB gravity scale.

12:35.080 --> 12:40.060
And then I would do respawn finished false.

12:40.060 --> 12:43.870
So this would just give it default values.

12:43.870 --> 12:46.420
So this will just uh sorry.

12:46.420 --> 12:47.230
Let's just.

12:47.850 --> 12:48.720
Do it like so.

12:48.720 --> 12:49.260
Yeah.

12:49.590 --> 12:55.320
So because I'm calling this method with a false false value will be passed over here because of that,

12:55.320 --> 12:59.970
this if statement will not work and else statement will work.

12:59.970 --> 13:03.990
And over here we're going to set gravity scale to zero can be controlled false.

13:03.990 --> 13:05.880
Collider enabled false.

13:07.430 --> 13:10.580
So because of that, player is not going to fall.

13:10.580 --> 13:15.140
He's just going to stand in the air and he will be invincible for some time.

13:15.140 --> 13:15.980
Right.

13:15.980 --> 13:17.420
Let's go and try this.

13:17.870 --> 13:19.940
I'm going to go to play mode and.

13:21.240 --> 13:22.530
I cannot control it.

13:26.420 --> 13:27.350
Very cool.

13:27.350 --> 13:29.240
And also Collider is disabled.

13:29.240 --> 13:29.990
Yes.

13:29.990 --> 13:38.360
Now we have a goal to call this method over here with the value true whenever we are ready and we can

13:38.360 --> 13:41.210
do it with the animation trigger, right?

13:41.210 --> 13:42.590
We could go to.

13:43.920 --> 13:45.690
Animator animation.

13:45.690 --> 13:47.880
Find this player.

13:47.880 --> 13:50.280
Respawn and.

13:52.240 --> 13:52.930
In the end.

13:52.930 --> 13:55.690
Over here, we can add an event.

13:57.310 --> 14:03.130
The only problem is we cannot add event to this game object from a player.

14:03.520 --> 14:09.940
Events are available only when script and animator are on the same game object, just like we have with

14:09.940 --> 14:11.170
a visual effect.

14:12.360 --> 14:13.230
Over here.

14:13.230 --> 14:19.890
We have animator and script together, and that's why this animator on this object can call method from

14:19.890 --> 14:20.760
this script.

14:20.760 --> 14:24.870
But over here, this one has no script and this one has no animator.

14:24.870 --> 14:29.670
So we cannot just directly call method on this animator from this script.

14:29.670 --> 14:30.630
But.

14:31.480 --> 14:33.040
We can go to scripts.

14:34.150 --> 14:37.990
And make new C sharp script player.

14:37.990 --> 14:40.180
Animation events.

14:45.070 --> 14:48.190
So over here I have player animation events.

14:49.190 --> 14:51.410
Now I'm going to type private player.

14:51.410 --> 14:52.400
Player.

14:53.690 --> 14:54.920
In their wake.

14:54.920 --> 15:00.230
I'm going to get player equals to get component in parent of a player.

15:01.680 --> 15:05.760
And this will get a variable player in the parent.

15:05.760 --> 15:06.810
We can go back.

15:08.470 --> 15:12.070
And just go to play mode and see it in a debug window.

15:12.070 --> 15:12.970
Animator.

15:12.970 --> 15:15.040
Sorry, we need to put the script on.

15:15.310 --> 15:16.900
Let's put the script.

15:17.550 --> 15:19.050
Then let's go to play mode.

15:21.650 --> 15:22.490
Debug.

15:22.490 --> 15:25.430
And over here I have player reference.

15:26.000 --> 15:26.990
Very cool.

15:27.050 --> 15:29.180
Now we can go back to this one.

15:29.920 --> 15:31.780
Uh, player animation events.

15:33.370 --> 15:45.700
And I'm going to make an event public void finish respawn which would do just player finish player respawn.

15:45.700 --> 15:46.360
Finished.

15:46.360 --> 15:47.020
True.

15:47.560 --> 15:52.420
So this script will call method from a player which would finish respawn.

15:52.420 --> 15:57.040
And we're going to call this method on the event that will do the job for us.

15:57.070 --> 15:59.620
Okay, let's go back to unity.

16:02.050 --> 16:03.010
On the animator.

16:03.010 --> 16:05.500
Let's switch to normal mode.

16:06.450 --> 16:09.750
And then we're going to find player respawn.

16:09.750 --> 16:15.030
And at this event over here we're going to do player animation events method.

16:15.700 --> 16:16.090
Finish.

16:16.090 --> 16:16.750
Respond.

16:17.410 --> 16:21.070
Now let's select the player and apply overrides.

16:22.460 --> 16:23.810
And let's go to play mode.

16:26.780 --> 16:27.920
I'm going to jump.

16:30.530 --> 16:31.340
Very cool.

16:31.340 --> 16:36.920
I cannot be controlled while I am respawning, but I can be controlled after and I'm falling only when

16:36.920 --> 16:37.550
I'm ready.

16:37.550 --> 16:39.020
I think this is a cool effect.

16:39.020 --> 16:40.160
I like it a lot.

16:46.210 --> 16:50.710
Well, this is good for now and I'll see you in the next video.
