Hello guys.
Welcome to the video.
So we are almost done with the development of the character.
We just have a couple of more steps.
Like we need to make a knockback.
We need to make a buffer jump and coyote jump.
I want to set up most important functionality in one section so player is ready to go and we are ready
to develop enemies, traps, levels, all of the other stuff that we need.
And we don't have to come back to player as much.
But no worries, there is just a couple of more videos to go and we're good.
So in this one we're going to make a knockback so player can be knocked by the trap or by the enemy.
First I want to remove this platform so we have a bit more space for the knockback.
I'm going to place player over here.
And let's first set up the animation.
We're going to go to Animator Create new clip.
Animations.
Player knocked.
This is how it's going to be.
Then we need to go to graphics main character, virtual guy.
There is this hit and we're going to take these sprites and place them over here.
And let's make sample rate 12.
Now let's see them.
Yeah, this is good.
And I want to make it a bit slower.
So player is slowed down when he's knocked.
At least it looks like that.
That's why we have sample rate 12.
Now let's go to animator and set up new animation.
And most of the time we switched between animations with a boolean.
This time I want to show you how to use trigger.
We're going to make a trigger parameter and let's name it as knockback.
Now let's drag this player knocked over here.
And I want to make a transition to knocked from any state just like that.
Any state means we're going to transfer to this animation from any state we have over here.
Doesn't matter.
We are in the idle jump, full wall slide.
It will be interrupted.
And we're going to jump to the knockback straight ahead, which is really good because player can get
knocked while he's running.
While he's wall jumping, he may be hit by a trap.
So so that is what we need to do here.
We need to make him vulnerable at any moment of his animation over here on a condition we're just going
to add condition.
Knock back and that's it.
Now let's open settings.
And over here we're going to make transition duration zero.
And it is very important.
Make sure you uncheck this over here.
It's supposed to be unchecked because otherwise player will be able to transfer to a knock back animation
again and again from himself.
So it's going to be sorry it's going to be in the cycle just like that.
So in this animation we're going to make transition to idle move.
And here we don't need any condition because we're going to transfer to idle move anyway.
So we don't need a condition.
But this time we need an exit time.
Because we want to be sure this animation is played only once.
Transition duration can be zero, but exit time over here should be one.
Okay.
Yes.
Now let's go to the player script.
And set up knockback.
It's going to work almost the same as a wall jump.
Actually, it's going to be super similar.
We're going to need a header.
I'm going to name it as knockback.
And this header are going to have serialized field, private float, knockback duration because we want
to know for how long player is knocked.
Now let's make it one by default.
Also we want to know the direction of a knockback.
So I'm going to do private Vector2 knockback power.
And then we need private Boolean is knocked.
And also we want to know if player can be knocked at the moment, because if he's in the knock back
already, we don't want to start knock back again.
So let's do Privatebool can be knocked.
Okay.
Very good.
We have the script.
Now.
Let's go and make a public method.
Public void.
Knockback.
In this method, first we want to say is knocked true and we want to trigger the animation.
Let's go and type anim set trigger.
Knock back.
Make sure this name is same to the name in the parameters over here.
So this should be the same as this one.
Okay.
Actually, mine is not the same.
Knock.
Back.
Okay.
You see how important it is to be sure?
Uh, yeah.
Now, how do we call this method in the future?
We're going to call this method from the enemy or from the trap we have.
That script will trigger this method.
That's why we made it public, because this one should be available to other scripts.
As of now, we don't have any traps.
We don't have any enemies.
And I don't want to distract you from what is important right now.
So we're just going to call this method with a button on a keyboard.
But later on, I'm going to show you how to trigger this method from outside of a script.
Okay.
As of now, let's just do this.
If input get key down.
Key.
Code key.
Then we're going to call knockback.
Simple as that.
Um, let's just go ahead and try to trigger the animation and see if it works.
I'm going to run around, then I'm going to press K and I'm knocked.
Nice.
This is working very well.
Let's go back to script.
And we want to enable knockback, and then we want to disable knockback after the knockback duration.
And for that we need what?
Think about it.
We did it in the last video.
Yes we need a coroutine.
So we're going to go ahead to the coroutine we have.
And let's make another one.
Private eye enumerator.
Knock knock back routine.
Let's call it that.
Over here we're going to do can be knocked equals to false.
So player cannot be knocked again.
Then let's say is knocked equals true.
So we know he's knocked.
Then we're going to do yield return new waitforseconds of knockback duration.
Then we're going to say can be knocked again true and is knocked.
Pulse.
All right.
So this is going to be good.
Uh, let's go back to the knockback method.
And instead of this we're going to say we're going to type Startcoroutine knockback routine.
Simple as that.
Also, while we are knocked, we don't want to do anything with a character player not supposed to be
able to control it.
He is not supposed to be able to move around, jump, or do anything else.
We can do that simply by going here and type.
If is knocked, then return.
So now none of this will work while we are knocked.
Basically, we're going to stop anything here for the duration of a knockback and let's go there and
try this.
We have knockback duration one.
Knockback power.
Let's make it.
Oh, we don't use it yet okay, let's keep it at zero.
Let's just see if I can jump after the knockback.
So I'm going to do knockback and I cannot jump but I can jump after the knockback.
This is good, right?
Yes.
Very good.
Now let's go and apply velocity of the knockback.
So over here.
We can do RB velocity equals to new vector two.
Knock back power.
Dot x multiplied by.
Let's use the opposite facing direction for now and knock back power dot y going to be the same without
changes.
And also we want to be sure we cannot be knocked while we are in the knock back.
So over here we want to type.
If is knocked then return, which means we cannot knock back ourself again while we are in the knock
back.
So we check that.
Then we start a coroutine.
Which does the thing that we need.
We cannot be knocked again and we are knocked.
And we know that after knockback duration, we cancel the knockback and we say that we can be knocked
again.
And then over here we trigger the animation and we also give it a velocity.
So he flies for a little bit.
Let's go and try that.
And.
He didn't fly because I have zero here.
Let's make it five and seven.
And again.
Boom.
Nice, right?
We were knocked.
Yeah, that is cute.
Although I see animation is not working very well.
So maybe we're going to adjust and do it differently.
Yeah.
As of now, the problem is very simple.
We have knockback duration one and animation is shorter than one second.
Because of that and because we exited animation after one cycle over here.
We can see that I'm doing knockback.
I'm exiting animation, and only after that, um, I'm going back to the regular state, but still we're
doing a bit of a slide.
So one solution would be just to do.
Knockback duration point, maybe 65.
And now.
Sorry, I forgot the power.
Five and seven.
So knockback and it's good.
So yeah, we can do that.
Or if you want to have a longer knockback, then you would have to do a bit of a change.
You could say that we transfer here if knockback is true and can be knocked through.
You could transfer here with a boolean, then you would remove exit time over here.
Just remove exit time and make transfer from a player knocked to idle.
Move with a condition is knocked false.
Something like that.
And it's supposed to work for me.
I think I'll keep just knock back duration around seven and if we need to change it in the future,
we're going to change it.
But as of now it works very well, so why not use it like that?
We also need to know, you know, we cannot spend too much time on the balance right now because we
are not sure how the game is going to feel.
Maybe knockback duration of a 0.7 is even longer than we should have, and we can make it a bit, you
know, softer for the player.
It all depends on the difficulty of the game you want to have.
So let's keep it at that.
Actually, let's do it as I do.
Then once we have levels and enemies, we can always rebalance.
Yes.
Now this method is ready for the future, and I'm going to show you how to call it from outside of a
player script later on.
We're also going to improve it, and knockback will actually depend on the object that is damaging the
player, the knockback direction, but that's going to be later.
As of now, let's just keep it at that.
I'm going to remove this if statement over here.
And.
I'm sorry.
Yeah, and we are ready to go to the next video.