Hello guys.
Welcome to the video.
In this one I want to show you how to flip the character because because right now if we try to run
to the left he does not flip.
And it feels weird, right?
Let me explain what we can do here.
So one of the ways is to flip the sprite over here.
But actually I do not prefer this way for one simple reason.
When we flip the sprite, we do not flip anything else except the sprite itself.
So now my character is looking to the left, but his vector right, he's facing direction, goes to
the right, and we cannot work with it because later on we're going to have a wall check for wall jump,
which going to be casted to the right, and I want this check to be flipped as well when we flip in
the character, because if you do it not like that, but if you rotate the character on the Y by 180.
He also going to be facing left, but at the same time we're going to have his vector.
Uh, to dot right, facing left.
So we sort of know facing direction.
We flip the facing direction and we flip sprite as well at the same time.
That's why it is important.
Now, how do we flip the character together with the facing direction.
Actually I just showed you we're going to change rotation on the Y over here.
And we're going to rotate character by 180 on the zero every time we want to flip him.
And this is how it's going to look boom and then boom just like that.
So let's go and do that in the script.
Let's go and apply this rotation to the transform in the script.
Um, somewhere all the way below but above on drawing gizmos.
I'm going to make a method private void flip.
Inside of this method I want to do.
Transform.
Rotate.
Zero and x 180 on the Y and zero on the Z, so we only rotate him on the y axis.
Now let's save this and let's just call this function to be sure it is working on the update I'm going
to do if input get key down key code C clip.
Just want to show you how it's going to work.
Let's save this.
Let's go back.
And I'm standing here, I'm going to click the C button and I'm flipping.
Okay.
Now of course I cannot click this key on the keyboard every time I need to flip the character, I need
to do it somehow, automatically.
So this is how we're going to do it.
We're going to make a method that will check if player is facing right and if he is facing right, but
his velocity is negative and he running to the left means we need to flip him.
Then if he's facing left, but he's supposed to be facing right because he's moving to the right, we're
going to flip him again.
For that over here.
We need to make private boolean facing right.
And we can give it a default value by just typing equals to true.
So from the start this boolean will be true and we know that we are facing right.
Now let's go.
Somewhere here.
And let's make a method.
Private void handle.
Lip.
And inside of this method, we're going to make an if statement that will check if our velocity dot
x is less than zero means we're moving to the left and he is facing right and facing right, then we
need to add a flip method over here.
So he's going to flip like that.
Now let's go and check if he is flipping when he ran into the left.
And we just need to call this method in the update.
So over here after the movement I'm going to do handle flip.
Okay, let's save it and let's go back.
Now I'm standing.
I'm going to run to the left and we flip in.
But it is sort of too much.
It's okay.
We're going to fix it anyway.
Now he flipped.
And when I'm trying to run to the right, he's not flipping.
Right.
So how are we going to fix it?
First of all, when we flip, we need not only to flip the character, but we need to say that he is
not facing right anymore.
And we could do it by saying facing right equals to false.
Over here in the curly brackets.
But that would be sort of an extra job because you can set Boolean to an opposite value in a different
way.
Let me just bring it back to what it was.
And over here, every time we call a method flip, we're going to say facing right equals to not facing
right.
Exclamation mark over here means not.
And this one basically says if you are true, become false.
If you are false, become true.
Just take an opposite value from what you have.
Now let's go again to unity and see how it's working.
And I'm standing there, I'm going to run to the left and I'm flipping only once.
But when I try to run to the right, there is no flip.
So we need to add flip for the right side as well.
We can do that by adding here an Or condition.
So if you type two slashes like that then it means or it's like typing or.
Right.
And we're doing one check or if.
RB velocity dot x is bigger than zero, which means we're moving to the right and not facing right means
we need to face the right side.
And that's why we need to flip again.
So let's go ahead and see if it's working.
And we're good to go.
We're only going to need to add one more update here.
One more improvement.
So I'm running to the right, left, right, left and it even works with the jump.
Yay!
Very cool rate.
I love it so much.
So basically we've got a platformer here.
You just take a platform, uh, place it here, and now you have a platformer where you need to jump.
Very cool.
Now let's go to player and add one variable over here.
It's going to be integer private int facing direction.
It's going to be equals to one.
By default.
We're going to need this later on for different purposes.
And this one will just help us to know when character is facing right or left.
And we need it as a number because we're going to use it to move character to different sides, for
example for knockback or for the wall jump or anything like that.
So let's go back to the.
Flip over here and type.
Facing direction equals to facing direction multiplied by minus one, and this will multiply value by
minus one every time we flip it.
So it will be 1 or -1.
Let's go and see how it works in a debug mode.
So here debug we can see facing direction is equals to one.
I'm facing left minus one one minus one one minus one.
Very cool right.
Yeah I think so.
Well great job.
Thank you for paying attention.
I'll see you in the next video.