Hello, guys.
In this video, we're going to make a knockback.
It's going to be pretty simple task.
And first, what we're going to do for this is create animation of knockback.
Let's select our player.
Create new clip assets, animations and player knockback.
Now we don't have knock back animation in the graphics folder.
Actually, we have, but it's kind of different from what we need.
As you can see in this animation, he's falling and then he's standing up.
But we kind of always in the action, we always run in in our game.
So we're going to modify it by just a bit.
I'm going to take my first sprite, drag it here, then second Sprite, drag it here, then third Sprite,
then I'll go to my role animation and I'll take this one.
This looks like he's landing on his back.
Drag it, then this one.
And then from the seventh up to 10th, I guess, just like that.
Now let's put sample rate 12 and see how it looks.
I did something wrong.
Let me see.
So what I want to do is to make impression he's rolling back, and then he's standing up.
So let's delete this.
We have only one, two, three from knock back.
Then first one from him landing on his back.
Let's delete this one.
One, two, three.
And then number six.
Number five.
Like this.
Number four.
Number three.
Number two.
And then number nine.
Yeah.
So it goes like this.
And number ten for standing up.
Let me see.
Now that's more like what we need.
We can see sprites over here if you want to see a precise.
Let's go to animator.
Create bullion.
I'm going to make more space here so we can see it.
Let's create bullion.
Can is knocked.
I'm going to drag it maybe here.
Make transition from any stage to knock back condition is knocked true.
Let me drag it above exit time.
No duration transition Duration zero from knockback transition to idle and exit time here will be just
one.
Duration zero.
We're going to add an event here.
But later for now, let's just go to script.
Oh, wait a second.
We need to go to any state to jump full and add condition here.
Is not should be false.
If we want to go to jump fall, so animation will not be interrupted.
Now let's go to code.
Let's create header.
Knock back info.
Then we're going to create serialized field private vector to knock back direction and private boolean
that tells us if player is knocked or not, so is knocked.
Now let's go to update.
And just here we're going to say if is knocked then return.
So basically it means if player is knocked, we're not going to do any of this functionality.
So we're not changing speed player is not moving, there is no input, nothing.
So we cannot do anything until knockback is over.
Now let's go below the update and make a function private void knockback which will set is knocked to
true and we'll set velocity of the player just wants to direction of the knockback.
So rb dot velocity equals to knockback direction.
Okay, That's good.
Now let's go here and create another function.
Private void.
Knock back.
Cancel knockback.
Which will do only one thing it will set is not to false.
So maybe we can return it as one line.
Just like this.
Now we want to call this knock back somehow.
And later, are we going to do it when player gets hit by a trap or something?
But now we don't have any trap or anything in the game.
I just want to set up knock back first so I'll do short input to make knock back.
So it will be like this input get key down key code dot key K.
So now if I'll press my K on the keyboard he'll be knocked basically.
Then later we're just going to call this function from a trap or anything we need.
Let's go back to Unity.
Find the player.
But let me find the script and we're going to set knock back direction.
Remember, on the X, it should be negative value.
So he goes to the left, I'll try -12 and for the Y, I want to try seven.
Now.
Let's see it in the game.
Okay.
We're running and pressing.
Kay.
Boom!
Knocked.
Okay.
What is wrong with that?
All right, let's do it as a challenge.
Can you figure out what is wrong?
Why knockback didn't work?
I mean, it worked, but there was no animation, so I'm giving you a chance to fix that.
One, two, three.
Okay, let's go back.
Let's go back to code and find our animator controllers and set anim.
Set bool.
Is knocked to.
Is knocked.
Bullion.
Key.
Now we need to find our player animation.
Knock back add in event in the end over here that says cancel knockback.
OC play mode.
I'm running.
I'll press.
Kay.
Boom.
Knocked.
Kay.
That is my fault.
This is what happens if you don't touch this checkbox.
Can transmit to self.
We need to uncheck this.
We need to make sure we are in check in transit to self.
Now, you know, if your animation freezes on the first frame and check and transit to self.
Right.
Boom.
Knocked.
This looks pretty good, right?
We can make his floating animation a bit longer.
That I'll go to animation of the knockback.
Where is the floating animation?
This one.
And I'll take all of the frames and I'll just move them to the right by one frame.
And I need to move this event now, like this.
So now this frame will be played for two frames.
Yeah, I think that's better.
If you want to change something, you can change speed of the animation, for example.
Now let me find it.
Knock back change sample rate to 15.
Then he'll do it faster.
Boom.
If you want to change strengths of the knock back its power, you can change knockback direction.
So more on the X like -20.
He'll get pushed further just like that.
Or if you put higher y he'll he'll be knocked higher.
But I think I chose pretty good values.
-12 and seven.
Okay, very good.
Let's go to the next video.