Hello, guys.
In this video, we're going to make slide ability, and I want to start doing that by actually creating
an animation for this.
Right.
Going to be easy to do.
All what we need is to select the player, click animation, create new clip player.
It'll none of it player slide.
Right.
Then let's go to graphics player find.
Slide and drag sprites over here.
Let's check the speed of the sprites.
Going to be 15, I guess is fine.
I guess this looks fine.
Okay.
Let's go to animator now, and let's think about this first.
We need to condition that we're going to use.
And there's going to be a bullion.
Is sliding.
Let's move it above over here.
And next step is to know when we do in transition to slide.
If you think about this, you're going to do slide from the air from jump or double jump.
It should be on the ground.
So let's do this from the move state.
Let's make a transition to play or slide.
With the condition is slight in true and check exit time and duration should be zero.
Now let's make another transition back.
A slide in false duration zero and check exit time.
That's good enough.
Now let's go to the script of the player to make a slide.
We're going to use a timer.
How We can use a timer.
I'm going to explain first and then we're going to make slide itself.
Let me create a public variable over here, public float timer.
Then I'll go to update and I'll type timer equals to timer minus time, dot delta time.
So time, dot delta time is a time that was taken to go from the last frame to current one and basically
just to make it simple to understand.
And so we can use it, we use time dot, delta time whenever we need to count time in seconds.
As you can see here, now we're taking our float time and doing minus time, dot, delta time, every
update.
So right now, if I go to my unity and I'll go to play mode, you can see that my timer decreasing all
the time.
It goes to minus three, minus four, minus five and so on, basically like a seconds.
And we're going to use this timer to make a slight ability.
Basically what we're going to do is to make that player can slide if timer more than zero.
And at the same time we're going to give timer the value of 2 seconds and it's going to decrease itself.
And when it will be below the zero slide, the ability will be stopped.
This how it's going to work.
I know time dot delta time might be confusing at first, but believe me, with the time it will be easier
to understand and it's going to be just a very comfortable tool that you use for cooldowns and some
ability that takes time and so on.
So let's just try and practice.
All right?
I'm going to delete this and we can delete this one as well.
And I'm going to create a Heather.
Slide info.
Now, what do we need for a slide?
First we need the speed.
So serialize field, private float, slide, speed.
Then we need time.
How long are we going to slide?
Let's do serialize field, private float, slide time and we need a counter that we're going to use
to check how long we slide in.
So private float, slide time counter.
And this the value we're going to decrease with the time.
Also, we need the boolean to know if we are sliding at the moment, so private ball is sliding.
Cool.
I'm going to save it.
Go to update and before you've seen I typed something like this slide time counter equals to slide time
counter minus time, the delta time.
But this looks kind of long and we can make it shorter if you will type slide time counter minus equals
time to time it would actually mean the same.
These two lines mean totally the same.
They just written differently and you are allowed to use whatever way you like, but I think it's best
to keep it cleaner and lighter.
So I'll keep this option.
Now we're going to go to check input over here, go to definition, and we're going to type here if
input and I don't remember if there is a button for slide.
So I want to use get key this time, but we can change it later if we need to.
So get key down.
Key down echoed left shift.
And if this was pressed, then we make a sliding.
True.
Now let's think about this.
What we can have with this value is slight and true.
First, we can set an animator, and let's do it here.
And I actually want to change the look of it a bit, said Bull.
Is sliding to a sliding now animator set.
And let me move these two lines of code above.
So we're going to have section for float and for bullion.
Yeah.
Now, when we press shift, we set a slide in.
True animator is on and now we need to change speed of the character as well.
So let's go to movement over here and type.
If is sliding then RB velocity equals to a new vector two is going to be slight speed this time and
we're not going to change y.
Else.
If it's not sliding, then regular velocity like we are running.
If we're going to run it like that, it's going to work and we're going to enter the slide state, but
we're never going to exit the slide state.
Let me just select the player and change his velocity of the slide.
Player.
Slide speed to, let's say, 15.
Now, if I'll run, I'm in the slide, but I'm not exiting the slide.
It's going to work endlessly.
So we need to make a condition to exit the slide.
How are we going to do it?
We're going to go back to code, create a function that we're going to call, check or slide.
And let's click alt enter generate method.
And inside of this check slide, we're going to do this.
If slide time counter less than zero, then we're going to set a sliding pulse.
All right.
Now we need to set a counter when we're pressing the button.
And actually, it's going to do a lot more than that, not only set in slide into true and set the counter.
So we better create a method for this one as well.
Let's select the line, click alt, enter, extract method and we're going to call it slide button and
inside we're going to do is slide in.
True.
And we're going to set slide time counter.
Equals two slide time wines named slide timer.
Okay, let's press control.
RR.
And type here.
Slide time.
And here as well.
Slide.
Time counter.
Okay.
This is done?
I guess it is done.
Yeah.
Let me see.
We actually want to make sure it's working.
So let's go and do this.
I need to set slide time so it's going to be point four and slide speed going to be.
Let's try.
20.
Okay, let's go now.
And we don't slide and going back.
We get in faster and going back to running.
Running.
Perfect.
Now, there are a couple of issues here.
First is if I place any role in front of the character.
Like this.
First, I cannot enter the slide.
I can enter the animation, but I cannot move.
Because if you remember, we restrict movement.
If there is a wall detected and that is absolutely correct behavior, in my opinion, you should not
be able to do slide if you are standing.
So it's kind of like a gameplay decision, I would say.
So if you did not jump over the wall or if you did not do slide under the platform, it means you made
a mistake.
And for that you are paying with your time and speed.
So we need to jump over the platform and get your speed again.
So that's why what we're going to do here is to make sure you can enter slide only if you have velocity
on the X.
So we go to slide button over here and type.
If RB velocity dot x not equals to zero, then we do these two.
Just like that.
Yes, So I can do slide, but if I will lose my speed for some reason.
Next.
So.
Then I have to jump over and try it again and try again.
All right.
I guess that's it for this video.
We have work in slide.
We still have things we need to fix with the slide and make it better.
We still have things to improve, so let's move on.