Welcome back, my fellow game developers.
In this video, we are going to not only be rotating the gun,
but we are also going to be pointing in the right direction.
But not only that,
not only are we pointing in the right direction,
we can also point back.
And this is very important and this will be your challenge.
We are going to be learning how we can use if conditions,
if statements in order to rotate from left to right.
And it's going to be very fond,
is going to be amazing because we are making
our gain much more alive, much more effective.
So let's not waste any more time.
And let's get started.
Oh, okay.
So now our gum is actually rotating.
It is always pointing towards the mouse. That's great.
But what happens when we go over a certain limit?
You can see right now that the GAN still pointing towards the mouth.
But it is, first of all, it's inverted.
Second of all, the player isn't looking in the proper direction,
so we need to fix all of that.
But how, how can we tell our player to behave in a way when he
is looking in one direction and another way when he is looking
in the other direction or should be looking at how can we tell him?
If it's like this,
then we want to do something.
If it's something else,
we want to do something else.
Well, this is what we call the if statement.
Let me take you through it.
So conditional IF statement structure.
This is how the syntax looks like.
We have some condition that needs to be true.
It's inside of these two brackets right here.
And if that condition is true,
then the code inside of these two squiggly lines is executed.
If it's not, then we have something called the else.
If, if this condition is not true,
else if some other condition is true,
this code will be executed.
And if neither this condition nor the second one is true,
then we have something called the else.
And this code will be executed if none of the previous conditions are true,
you can add as many else if statements as you want so you can add.
The first one is always if you can even not do an else,
if you can immediately add an else or you can add 15 else ifs, if you need to,
you might never need that money,
but if you ever do need more than one,
more than two, you can add.
So we have a condition in here.
If it's true, this code executes and we stop.
We do not continue looking down.
If this not, this first one isn't true.
We look to the second one.
If it is, we execute,
if it's not, we continue.
Finally, if we don't find any condition that is true, we execute else.
So let's go ahead and implement that.
So what are we looking for in here?
We are looking to rotate the entire object.
If so, what are we looking for in here?
We want to see if the mouse position
is less than because let's look at the dome player for example,
this is the position on the x back in the scene.
So when we are moving backwards,
you can see that it's less when we are moving to the right. That means it's small.
So imagine the mouse position as being a another game object.
For example, We are in here and this area to the left of the player,
that means the most position is less than this green point,
which means we want to rotate the entire don't player to the other side.
But how are we actually going to do that?
Well, there is the scale right here,
which makes it super easy.
So if I change the scale on the x,
you can see that he becomes wider and wider.
But what happens when we go and negative?
So when we go Negative,
yep, there we go.
We actually flip the player.
And this is what we want to be adjusting whenever we are looking in the other direction.
And this direction, when the mouse position is less than the screen point,
which is the player.
We want to be looking right here.
And this direction we want to change the scale to minus one,
not only the ACS, also the y.
We will see why.
So now that we have those things established,
let's create our first if statement.
So in here, we are going to create enough.
We're going to open the two brackets or columns.
I'm not sure what they're called.
We're going to check for the mouse position dot x.
We don't need the y. It doesn't matter.
And if it's less than the screen point, the RX.
Then we are going to take the transform local scale,
which is the local scale of the object holding this script.
That means the entire don't player is going to be new vector 3 minus 1,
f 1, f 1.
Save that.
We run the gain.
This is while the y is still positive.
So if we look in the other direction,
you can see that the gun is upside down.
So our goal, let me just show you and the way I pause this using keys is control
shift P. So what we need to do is if we grab the weapons arm,
if I set it to minus one, there you go.
You can see that we are back at a proper scale or a proper advertisement.
So I'm going to set this to minus1,
save that back in our game, run the game.
And now we look up and down, the gang rotates.
If we go to the other side, there you go.
The gun looks properly.
Great. So everything is set up.
The only problem is when we look back,
the player doesn't change.
So how are we going to make sure that when we don't have this condition as true,
how are we going to rotate the player back?
Well, it's your challenge.
Your challenge is to create an else statement.
You'll need to add to the if statements and else statements.
We already saw how that looks.
This will activate when the mouse position x is smaller than the screen point x.
In that case, we need to set the local scales or all the local scales back to one.
And I'm going to give you a small hint,
which is to set the scale to vector three dot one,
vector three dot one.
You can also research on Unity's documentation.
It means that it's a vector 3, 1, 1, 1.
Pause the video right now and go do the challenge.
Okay, welcome back.
How did you get all of that?
I really, really hope that you are always trying to bust with these challenges.
So I'm going to create an else statement in here.
And what I'm going to do is I'm going to take the transform local scale.
And it's going to be equal to vector three dot one.
Let's save that.
And I'm going to also duplicate this.
So the way we duplicate it as remember Control D. And instead of the transform,
I'm going to use the weapons are,
I'm going to save all of that.
I'm going to go back into my game.
I'm going to run it.
So now I look to the right,
my gun rotates properly.
Everything is good. I look to the left,
the gun rotates properly.
The player is looking in the right direction.
Now if I go back, there we go.
So you can see that we have fixed our problem.
The player always looks in the right direction.
We have the gun pointing properly,
and we finally look amazing while doing it.
So now we can walk around.
You can see that it's much more natural because it's always pointing towards the gun.
Not only is the weapon pointing properly towards the gun,
weapon problem pointing towards the mouse,
but also deep layer,
entire body is rotating and pointing in the right direction.
So that done, make sure to go ahead and commit the changes that we made.
And in the next video we are going to be adding more flavor to our games.
How, by creating the walking, the idling,
and maybe even the torpedo animations for the player can't wait to start animating.
I'll see you in the next one.