Our player is not flipping to the left direction once it starts moving to the left direction, so we
need to flip that.
But before we we do.
Let me show you.
So I'm going to click, uh click on this play button in the top in order to start our game.
And let's have a look on this.
So we should able to see that we're not gonna flip to the left direction.
So we need to flip that.
So let me show you first.
So as you can see we are moving to the left direction, but we're not flipping at all to the left direction.
So if I select my player in the hierarchy then move over to Inspector Transform component and then rotation
and go with y axis.
And if I put in the y something like -180.
As you can see, as soon as we put -180 it's flipped to the left direction.
So basically what we have to do, we need to set the Euler angles in the y to -180 as you can see.
So let's get out of play mode.
And for that we will create a variable.
We're just going to keep track of which direction we're looking.
So let's open up our player script by double clicking on it.
And let's go to the top of our class.
Somewhere around here.
And that parameter doesn't need to be public so we can make it private instead.
So this will be private private.
And this will be bool which can be true or false.
And let's call this one facing right.
Facing right.
And let's close that up with semicolon.
And we will move over to this start function.
So we do know that start function get call in this start of the game.
So in this start of the game I'm going to say facing right to true.
And let's close that up with semicolon.
So let me explain why we created this parameter and why we set it equal to true.
So this parameter we just created to keep track of which direction we're looking.
So we're looking to the right direction.
So that's why I just named that parameter to something like facing right.
And in the start of the game I just said facing right.
equal to true.
As you can see, we're looking to the right direction.
So that's why uh, so that's why in the start of the game we just said facing right.
True.
But once we're gonna start moving to the left direction, we need to flip this player.
Uh, so what we have to do, we're going to make a function for it.
So let's go to our script and let's make the function somewhere around here.
And for function we're going to type void.
And let's call this function to something like flip.
Let's make first bracket and some curly brackets.
And what we want to check over here.
We basically want to check if we're moving to the left direction or not.
And we do know that that movement parameter right over here, as you can see, we created to just,
uh, which direction we're going.
Uh, this right over here is responsible.
Uh, we do know that if the movement is something like minus one means we're holding down a key.
So this player will start moving to the left direction.
So that movement is just gonna be minus one.
Basically means we're just gonna move or start moving to the left direction.
So we need to flip our player to the left direction.
So now what we're gonna do, we're gonna move over to this flip function.
And over here we will check.
So let's make if condition, let's make first bracket and some curly brackets.
So what we want to check over here.
We basically want to check if our movement is lesser than zero.
And we do note that once we press down a key on our keyboard, that movement just gonna, just gonna
be minus one.
And we do know that if this if this movement is minus one and minus one is lesser than zero, it basically
means true.
Minus one means that we are moving to the left direction.
So what we need to do, we need to flip.
But we also want to check if we started moving to the left.
Direction basically means that movement is minus one.
And we do know that minus one is lesser than zero f.
At the same time, if we are looking to the right direction, we started moving to the left direction,
but we are looking to the right direction, then what we have to do?
We need to flip this player to the left direction.
So also we want to check.
So we're going to use this an end condition.
And we will check.
We will check that if facing right is equals equals to true basically means we're looking to the right
direction.
So now what we have to do.
We need to flip this player.
So to in order to flip that we're going to move over to this rotation.
Or you can simply say Euler angles in the y.
We're going to set to something like -180.
And we do know that once we set -180 this player flips to the left direction.
So what we will do in this situation.
So we will go to transform.
Then we're going to access the Euler angles which going to have x x y and z axis.
And we're going to set equal to a new Vector3 y.
We set it equal to new Vector3.
Since this transform dot Euler angles just going to have x axis, y axis, and the z axis.
And this new Vector3 is also going to have the x axis, y axis, and the z axis.
And let's close that off with semicolon.
So we want to flip to the left direction.
So in the y part we need to do we basically need to put minus one ATF.
So we're going to put minus one ATF in the Y.
And we're not going to do at all anything in the x axis.
So we're going to put zero f.
And we will do the same thing in the z axis.
So once we flip our player to the left direction.
Uh so if we flip to the left direction, as you can see it will no longer able to look to the right
direction.
So that's why after flipping we will set that facing right to false.
Because in that moment we're not going to look towards to the right direction.
So that's why.
So after flipping so after flipping we're going to set facing right equal to false because we no longer
able to look to the right direction.
And now we're going to press down Ctrl S to save our script.
And now let's move over to unity.
Firstly, we're checking if movement is lesser than zero f or not.
Means that if we started moving to the left direction or not at the same time we checking to the right.
If we're looking to the right direction, then firstly moving over to this player transform, then Euler
angles and we're setting y to -180.
So it's just going to flip to the left direction.
So let's have a look on this.
But we're gonna get nothing.
Let me show you why.
So just wait a moment to compile our script.
And once it's done, we can just check it right away.
Once our compilation is done for our script.
So just wait a little moment.
And now if we go ahead and then click on this play button in the top, you will see nothing gonna happen.
Even if we press down a key Y, because we also need to call that function.
We basically made that flip function, but we never calling it.
So if I press down a key, as you can see, it's not looking to the left direction because we also need
to call the flip function.
So let's get out of play mode and where we need to call it, we need to call it this update function.
Because this update function runs every single frame and every single frame, we need to check which
direction we going or, uh, which direction we simply moving on.
So we're going to call it something over here.
So let's call the flip function here.
And then close that off with semicolon after setting up the movement right over here, as you can see.
And if I save my script and then move over to unity, you will see that as soon as we gonna start moving
to the left direction, we will see our player to flip to the left direction.
Since we setting the Euler angles in the y to minus one 8180, let me show you.
So let's click on this play button in the top in order to start our game.
And let's, uh, check it.
So just wait a moment.
And now, if I hold down a key, as you can see, as soon as we press down a key, uh, this player
just flips to the left direction.
But if I, uh, right now start moving to the right direction, it's not, uh, flipping to the right
direction at all after flipping to the left.
So what we basically have to do if we select our player in the hierarchy, move over to Inspector transform
and rotation in the Y or Euler angles in the y.
If we set back to zero, as you can see, it's flipped to the right direction.
So basically we need to check if we just moving to the right direction.
Then we basically have to set back this y to zero.
So it's going to flip to the right direction.
And after flipping we also need to set that facing right parameter to true.
Why?
Because as you can see in this situation we're gonna look to the right direction.
So that's why we need to set facing right to true.
So let's get out of play mode and let's move over to script and let's move over to our flip function.
So over here we will check else if else if our movement movement is greater than zero f.
And we do know that if movement is greater than zero f basically means we holding down D means we started
moving to the right direction.
And also we will check if we're not looking to the right direction.
Basically this player have just looking to the left direction.
So also we will check if we started moving right direction and we not uh, looking to the right direction
basically means facing right equals equals to false.
If that is the case then what we're going to do, we're going to set back our Euler angles to Euler
angles in y to zero.
So I'm going to copy this line of code by Ctrl C.
And let's paste it over here by Ctrl V.
And so what we will do firstly we will go to transform then Euler angles.
And in the y we're going to set back to zero f.
And after flipping to the right direction.
We need to set the spacing right parameter to true instead of false, because in that situation, we're
going to look to the right direction.
As you can notice right over here.
So firstly what we're doing.
Firstly we're checking if our movement is greater than zero.
Basically means we're moving to the right direction.
And we do know that if we hold down D key that movement just gonna be one.
So one basically is greater than zero.
And we don't know that.
And also we're checking if facing right is false.
Basically means this player is looking to the left direction.
Then what we're doing we're just setting the Y to zero F.
And after that we're just setting facing right parameter to true.
Because in that in this situation, this player just gonna look to the right direction.
So that's why we uh, we just set facing.
Right.
True.
And if you just want to don't do it like this.
If facing right and if, if you just put exclamation mark, it basically means the same thing.
Facing right basically means facing right is false.
It's basically the same thing.
Or you can just simply do it.
Equals equals to false.
And if you just want to check with true you can just leave it like this.
It's also basically means true true.
And if you just put exclamation mark right in front of this, facing right basically means it false.
So firstly we're checking if movement is greater than zero means we started moving to the right direction.
And if we're looking to the left direction then firstly we flipping.
And after flipping we just setting back facing right parameter to true.
As you can see.
So now if I press down Ctrl s to set my script and then move over to unity, you will able to notice
that as soon as we're gonna start moving to the right direction, our player just gonna flip to the
right direction.
Since we setting Euler angles in the y to zero or back to zero.
So now if we go ahead and then click on this play button in the top to play our game.
We can check that out.
Uh, firstly, let's start moving.
Moving.
Or let's begin with a key basically to the left direction.
So if I move.
As you can see, it's moving to the left direction.
And player just flipped to the left direction.
And now if I start moving to the right direction we can see player flips to the right direction.
And if we start moving one more time again to the left direction, we can see it's flipping to the left
direction and it's flipping to the right direction as well.
Basically, this player is flipping in the direction it's going.
So basically that is a pretty great thing.
And let's suppose you still want to see this, uh, private, uh, this right over here, this private
facing right parameter, uh, underneath your player script.
Then how you can do it for, uh, to do it, just go over to Inspector in the top right you can see
three dots.
So just click there and then click on this debug mode.
And as soon as we have chosen we can see our facing right parameter.
But I'm gonna go back to my normal mode.
So you can just click on that three dot once again in order to go back in normal mode.
And I'm gonna get out of play mode by just clicking on that play button.
So I hope you get the idea that how you can flip your player in the direction player is going.
So I hope you get you get the idea.
And also we need to make the idle animation clip and all of those things.
And now I'm gonna press down Ctrl s on my keyboard in order to save my project.
Or we can simply move over to top left file in unity editor, then click on the save button in order
to save our project.
And make sure if you've done any changes in in this player GameObject in the hierarchy, then make sure
you go over to Inspector Overrides and then apply all the changes you made over here.
It will be save this player GameObject prefab as well.
So now I'm going to press Ctrl S to save my project.