WEBVTT

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

00:00.620 --> 00:01.700
Welcome to the video.

00:01.700 --> 00:05.360
So we are almost done with the development of the character.

00:05.360 --> 00:06.890
We just have a couple of more steps.

00:06.890 --> 00:08.540
Like we need to make a knockback.

00:08.540 --> 00:11.360
We need to make a buffer jump and coyote jump.

00:11.360 --> 00:17.450
I want to set up most important functionality in one section so player is ready to go and we are ready

00:17.450 --> 00:21.200
to develop enemies, traps, levels, all of the other stuff that we need.

00:21.200 --> 00:23.330
And we don't have to come back to player as much.

00:23.330 --> 00:26.930
But no worries, there is just a couple of more videos to go and we're good.

00:26.930 --> 00:32.270
So in this one we're going to make a knockback so player can be knocked by the trap or by the enemy.

00:32.270 --> 00:36.980
First I want to remove this platform so we have a bit more space for the knockback.

00:36.980 --> 00:39.350
I'm going to place player over here.

00:39.740 --> 00:42.530
And let's first set up the animation.

00:42.530 --> 00:47.120
We're going to go to Animator Create new clip.

00:48.100 --> 00:49.300
Animations.

00:50.260 --> 00:51.430
Player knocked.

00:51.460 --> 00:53.050
This is how it's going to be.

00:53.320 --> 00:57.160
Then we need to go to graphics main character, virtual guy.

00:57.220 --> 01:02.710
There is this hit and we're going to take these sprites and place them over here.

01:02.710 --> 01:05.200
And let's make sample rate 12.

01:06.330 --> 01:07.560
Now let's see them.

01:08.590 --> 01:09.640
Yeah, this is good.

01:09.640 --> 01:11.170
And I want to make it a bit slower.

01:11.170 --> 01:13.990
So player is slowed down when he's knocked.

01:13.990 --> 01:15.370
At least it looks like that.

01:17.330 --> 01:19.700
That's why we have sample rate 12.

01:19.730 --> 01:23.720
Now let's go to animator and set up new animation.

01:24.290 --> 01:27.830
And most of the time we switched between animations with a boolean.

01:27.860 --> 01:30.980
This time I want to show you how to use trigger.

01:30.980 --> 01:35.270
We're going to make a trigger parameter and let's name it as knockback.

01:36.160 --> 01:38.980
Now let's drag this player knocked over here.

01:38.980 --> 01:45.250
And I want to make a transition to knocked from any state just like that.

01:46.150 --> 01:50.650
Any state means we're going to transfer to this animation from any state we have over here.

01:50.650 --> 01:51.280
Doesn't matter.

01:51.280 --> 01:54.190
We are in the idle jump, full wall slide.

01:54.190 --> 01:55.720
It will be interrupted.

01:55.720 --> 02:00.370
And we're going to jump to the knockback straight ahead, which is really good because player can get

02:00.370 --> 02:01.900
knocked while he's running.

02:01.900 --> 02:04.390
While he's wall jumping, he may be hit by a trap.

02:04.390 --> 02:06.190
So so that is what we need to do here.

02:06.190 --> 02:12.790
We need to make him vulnerable at any moment of his animation over here on a condition we're just going

02:12.790 --> 02:14.140
to add condition.

02:14.780 --> 02:16.190
Knock back and that's it.

02:16.220 --> 02:17.810
Now let's open settings.

02:17.810 --> 02:21.080
And over here we're going to make transition duration zero.

02:21.080 --> 02:22.880
And it is very important.

02:22.880 --> 02:25.550
Make sure you uncheck this over here.

02:25.550 --> 02:32.450
It's supposed to be unchecked because otherwise player will be able to transfer to a knock back animation

02:32.450 --> 02:34.250
again and again from himself.

02:34.250 --> 02:37.460
So it's going to be sorry it's going to be in the cycle just like that.

02:37.580 --> 02:41.780
So in this animation we're going to make transition to idle move.

02:42.050 --> 02:47.120
And here we don't need any condition because we're going to transfer to idle move anyway.

02:47.120 --> 02:48.320
So we don't need a condition.

02:48.320 --> 02:51.380
But this time we need an exit time.

02:52.290 --> 02:56.190
Because we want to be sure this animation is played only once.

02:56.250 --> 03:00.600
Transition duration can be zero, but exit time over here should be one.

03:00.600 --> 03:01.320
Okay.

03:01.530 --> 03:02.430
Yes.

03:02.670 --> 03:06.450
Now let's go to the player script.

03:07.630 --> 03:09.070
And set up knockback.

03:09.070 --> 03:11.830
It's going to work almost the same as a wall jump.

03:11.830 --> 03:13.600
Actually, it's going to be super similar.

03:13.600 --> 03:15.100
We're going to need a header.

03:15.100 --> 03:17.290
I'm going to name it as knockback.

03:17.290 --> 03:23.620
And this header are going to have serialized field, private float, knockback duration because we want

03:23.620 --> 03:25.840
to know for how long player is knocked.

03:26.290 --> 03:27.910
Now let's make it one by default.

03:27.910 --> 03:30.430
Also we want to know the direction of a knockback.

03:30.430 --> 03:36.040
So I'm going to do private Vector2 knockback power.

03:36.460 --> 03:39.820
And then we need private Boolean is knocked.

03:40.370 --> 03:45.890
And also we want to know if player can be knocked at the moment, because if he's in the knock back

03:45.890 --> 03:48.470
already, we don't want to start knock back again.

03:48.470 --> 03:52.490
So let's do Privatebool can be knocked.

03:53.280 --> 03:54.000
Okay.

03:54.000 --> 03:54.840
Very good.

03:54.840 --> 03:55.860
We have the script.

03:55.860 --> 03:56.310
Now.

03:56.310 --> 03:59.070
Let's go and make a public method.

04:00.040 --> 04:01.120
Public void.

04:01.120 --> 04:02.080
Knockback.

04:03.000 --> 04:10.410
In this method, first we want to say is knocked true and we want to trigger the animation.

04:10.440 --> 04:13.350
Let's go and type anim set trigger.

04:14.520 --> 04:15.390
Knock back.

04:17.430 --> 04:22.560
Make sure this name is same to the name in the parameters over here.

04:24.300 --> 04:27.210
So this should be the same as this one.

04:27.600 --> 04:28.140
Okay.

04:29.270 --> 04:30.920
Actually, mine is not the same.

04:31.340 --> 04:32.300
Knock.

04:33.640 --> 04:34.150
Back.

04:36.370 --> 04:37.120
Okay.

04:37.270 --> 04:39.490
You see how important it is to be sure?

04:39.820 --> 04:40.870
Uh, yeah.

04:41.080 --> 04:44.020
Now, how do we call this method in the future?

04:44.020 --> 04:47.860
We're going to call this method from the enemy or from the trap we have.

04:47.860 --> 04:50.620
That script will trigger this method.

04:50.620 --> 04:54.730
That's why we made it public, because this one should be available to other scripts.

04:54.730 --> 04:56.530
As of now, we don't have any traps.

04:56.530 --> 04:57.640
We don't have any enemies.

04:57.640 --> 05:01.120
And I don't want to distract you from what is important right now.

05:01.120 --> 05:04.540
So we're just going to call this method with a button on a keyboard.

05:04.540 --> 05:09.100
But later on, I'm going to show you how to trigger this method from outside of a script.

05:09.100 --> 05:09.700
Okay.

05:10.090 --> 05:12.010
As of now, let's just do this.

05:12.010 --> 05:14.590
If input get key down.

05:14.590 --> 05:14.860
Key.

05:14.860 --> 05:16.000
Code key.

05:16.820 --> 05:18.620
Then we're going to call knockback.

05:20.120 --> 05:21.080
Simple as that.

05:21.080 --> 05:25.700
Um, let's just go ahead and try to trigger the animation and see if it works.

05:30.410 --> 05:35.360
I'm going to run around, then I'm going to press K and I'm knocked.

05:35.360 --> 05:36.590
Nice.

05:36.590 --> 05:38.330
This is working very well.

05:38.330 --> 05:39.680
Let's go back to script.

05:41.330 --> 05:47.210
And we want to enable knockback, and then we want to disable knockback after the knockback duration.

05:47.210 --> 05:48.980
And for that we need what?

05:48.980 --> 05:49.730
Think about it.

05:49.730 --> 05:51.050
We did it in the last video.

05:51.050 --> 05:53.030
Yes we need a coroutine.

05:53.030 --> 05:55.910
So we're going to go ahead to the coroutine we have.

05:56.900 --> 05:58.490
And let's make another one.

05:58.790 --> 06:00.980
Private eye enumerator.

06:02.440 --> 06:05.260
Knock knock back routine.

06:05.260 --> 06:06.430
Let's call it that.

06:06.670 --> 06:10.810
Over here we're going to do can be knocked equals to false.

06:10.810 --> 06:13.120
So player cannot be knocked again.

06:13.120 --> 06:15.670
Then let's say is knocked equals true.

06:15.670 --> 06:17.170
So we know he's knocked.

06:17.170 --> 06:23.770
Then we're going to do yield return new waitforseconds of knockback duration.

06:24.250 --> 06:28.660
Then we're going to say can be knocked again true and is knocked.

06:29.380 --> 06:29.980
Pulse.

06:30.430 --> 06:31.090
All right.

06:31.090 --> 06:32.800
So this is going to be good.

06:32.830 --> 06:36.820
Uh, let's go back to the knockback method.

06:36.820 --> 06:42.490
And instead of this we're going to say we're going to type Startcoroutine knockback routine.

06:43.590 --> 06:44.490
Simple as that.

06:44.490 --> 06:51.600
Also, while we are knocked, we don't want to do anything with a character player not supposed to be

06:51.600 --> 06:52.770
able to control it.

06:52.770 --> 06:57.210
He is not supposed to be able to move around, jump, or do anything else.

06:58.630 --> 07:02.080
We can do that simply by going here and type.

07:02.080 --> 07:04.630
If is knocked, then return.

07:04.630 --> 07:08.770
So now none of this will work while we are knocked.

07:08.770 --> 07:14.800
Basically, we're going to stop anything here for the duration of a knockback and let's go there and

07:14.800 --> 07:15.610
try this.

07:17.370 --> 07:19.050
We have knockback duration one.

07:19.050 --> 07:20.100
Knockback power.

07:20.100 --> 07:21.450
Let's make it.

07:21.450 --> 07:23.970
Oh, we don't use it yet okay, let's keep it at zero.

07:23.970 --> 07:27.060
Let's just see if I can jump after the knockback.

07:27.060 --> 07:31.950
So I'm going to do knockback and I cannot jump but I can jump after the knockback.

07:34.150 --> 07:35.320
This is good, right?

07:38.960 --> 07:39.980
Yes.

07:39.980 --> 07:41.150
Very good.

07:41.180 --> 07:44.690
Now let's go and apply velocity of the knockback.

07:46.520 --> 07:48.650
So over here.

07:49.350 --> 07:53.250
We can do RB velocity equals to new vector two.

07:53.250 --> 07:54.960
Knock back power.

07:55.610 --> 07:58.430
Dot x multiplied by.

07:58.430 --> 08:06.170
Let's use the opposite facing direction for now and knock back power dot y going to be the same without

08:06.170 --> 08:06.680
changes.

08:06.680 --> 08:12.110
And also we want to be sure we cannot be knocked while we are in the knock back.

08:12.110 --> 08:14.030
So over here we want to type.

08:14.300 --> 08:20.660
If is knocked then return, which means we cannot knock back ourself again while we are in the knock

08:20.660 --> 08:20.990
back.

08:20.990 --> 08:21.860
So we check that.

08:21.860 --> 08:23.570
Then we start a coroutine.

08:24.940 --> 08:27.010
Which does the thing that we need.

08:27.010 --> 08:28.750
We cannot be knocked again and we are knocked.

08:28.750 --> 08:33.430
And we know that after knockback duration, we cancel the knockback and we say that we can be knocked

08:33.430 --> 08:34.240
again.

08:34.240 --> 08:38.350
And then over here we trigger the animation and we also give it a velocity.

08:38.350 --> 08:40.150
So he flies for a little bit.

08:40.150 --> 08:41.740
Let's go and try that.

08:47.210 --> 08:47.990
And.

08:49.730 --> 08:51.950
He didn't fly because I have zero here.

08:51.950 --> 08:54.620
Let's make it five and seven.

08:55.250 --> 08:56.150
And again.

08:56.150 --> 08:56.900
Boom.

08:57.110 --> 08:58.160
Nice, right?

08:58.190 --> 08:59.330
We were knocked.

08:59.570 --> 09:01.070
Yeah, that is cute.

09:01.070 --> 09:03.710
Although I see animation is not working very well.

09:03.710 --> 09:06.350
So maybe we're going to adjust and do it differently.

09:07.990 --> 09:08.500
Yeah.

09:08.500 --> 09:10.570
As of now, the problem is very simple.

09:10.570 --> 09:15.190
We have knockback duration one and animation is shorter than one second.

09:15.190 --> 09:19.900
Because of that and because we exited animation after one cycle over here.

09:21.470 --> 09:23.270
We can see that I'm doing knockback.

09:23.480 --> 09:29.900
I'm exiting animation, and only after that, um, I'm going back to the regular state, but still we're

09:29.900 --> 09:31.040
doing a bit of a slide.

09:31.040 --> 09:33.680
So one solution would be just to do.

09:34.830 --> 09:38.430
Knockback duration point, maybe 65.

09:41.390 --> 09:42.650
And now.

09:42.890 --> 09:44.870
Sorry, I forgot the power.

09:45.200 --> 09:46.280
Five and seven.

09:51.720 --> 09:54.150
So knockback and it's good.

09:54.150 --> 09:55.860
So yeah, we can do that.

10:01.180 --> 10:06.550
Or if you want to have a longer knockback, then you would have to do a bit of a change.

10:06.580 --> 10:12.430
You could say that we transfer here if knockback is true and can be knocked through.

10:13.810 --> 10:19.480
You could transfer here with a boolean, then you would remove exit time over here.

10:19.480 --> 10:23.380
Just remove exit time and make transfer from a player knocked to idle.

10:23.380 --> 10:25.780
Move with a condition is knocked false.

10:25.780 --> 10:26.590
Something like that.

10:26.590 --> 10:28.570
And it's supposed to work for me.

10:28.570 --> 10:33.880
I think I'll keep just knock back duration around seven and if we need to change it in the future,

10:33.880 --> 10:34.630
we're going to change it.

10:34.630 --> 10:37.570
But as of now it works very well, so why not use it like that?

10:37.570 --> 10:41.740
We also need to know, you know, we cannot spend too much time on the balance right now because we

10:41.740 --> 10:43.750
are not sure how the game is going to feel.

10:43.750 --> 10:49.300
Maybe knockback duration of a 0.7 is even longer than we should have, and we can make it a bit, you

10:49.300 --> 10:50.650
know, softer for the player.

10:50.650 --> 10:53.110
It all depends on the difficulty of the game you want to have.

10:53.110 --> 10:54.610
So let's keep it at that.

10:54.610 --> 10:56.620
Actually, let's do it as I do.

10:56.620 --> 11:00.490
Then once we have levels and enemies, we can always rebalance.

11:01.800 --> 11:02.610
Yes.

11:02.610 --> 11:08.040
Now this method is ready for the future, and I'm going to show you how to call it from outside of a

11:08.040 --> 11:09.420
player script later on.

11:09.420 --> 11:14.520
We're also going to improve it, and knockback will actually depend on the object that is damaging the

11:14.520 --> 11:17.070
player, the knockback direction, but that's going to be later.

11:17.070 --> 11:18.930
As of now, let's just keep it at that.

11:18.930 --> 11:21.480
I'm going to remove this if statement over here.

11:22.300 --> 11:23.140
And.

11:23.750 --> 11:24.770
I'm sorry.

11:27.850 --> 11:31.570
Yeah, and we are ready to go to the next video.
