1
00:00:00,000 --> 00:00:03,550
Welcome back, my fellow game developers.

2
00:00:03,554 --> 00:00:08,384
In this video, we are going to not only be rotating the gun,

3
00:00:08,385 --> 00:00:13,535
but we are also going to be pointing in the right direction.

4
00:00:13,530 --> 00:00:15,120
But not only that,

5
00:00:15,120 --> 00:00:17,870
not only are we pointing in the right direction,

6
00:00:17,865 --> 00:00:20,285
we can also point back.

7
00:00:20,280 --> 00:00:23,220
And this is very important and this will be your challenge.

8
00:00:23,220 --> 00:00:26,910
We are going to be learning how we can use if conditions,

9
00:00:26,910 --> 00:00:30,710
if statements in order to rotate from left to right.

10
00:00:30,710 --> 00:00:32,420
And it's going to be very fond,

11
00:00:32,420 --> 00:00:35,410
is going to be amazing because we are making

12
00:00:35,405 --> 00:00:38,855
our gain much more alive, much more effective.

13
00:00:38,855 --> 00:00:40,925
So let's not waste any more time.

14
00:00:40,925 --> 00:00:43,255
And let's get started.

15
00:00:43,250 --> 00:00:45,260
Oh, okay.

16
00:00:45,260 --> 00:00:49,820
So now our gum is actually rotating.

17
00:00:49,820 --> 00:00:53,980
It is always pointing towards the mouse. That's great.

18
00:00:53,975 --> 00:00:59,335
But what happens when we go over a certain limit?

19
00:00:59,330 --> 00:01:03,610
You can see right now that the GAN still pointing towards the mouth.

20
00:01:03,605 --> 00:01:06,785
But it is, first of all, it's inverted.

21
00:01:06,785 --> 00:01:11,055
Second of all, the player isn't looking in the proper direction,

22
00:01:11,059 --> 00:01:13,729
so we need to fix all of that.

23
00:01:13,730 --> 00:01:21,290
But how, how can we tell our player to behave in a way when he

24
00:01:21,290 --> 00:01:24,980
is looking in one direction and another way when he is looking

25
00:01:24,980 --> 00:01:29,020
in the other direction or should be looking at how can we tell him?

26
00:01:29,015 --> 00:01:30,565
If it's like this,

27
00:01:30,560 --> 00:01:32,210
then we want to do something.

28
00:01:32,210 --> 00:01:34,490
If it's something else,

29
00:01:34,490 --> 00:01:36,320
we want to do something else.

30
00:01:36,320 --> 00:01:40,370
Well, this is what we call the if statement.

31
00:01:40,370 --> 00:01:42,200
Let me take you through it.

32
00:01:42,200 --> 00:01:46,150
So conditional IF statement structure.

33
00:01:46,145 --> 00:01:49,445
This is how the syntax looks like.

34
00:01:49,445 --> 00:01:53,195
We have some condition that needs to be true.

35
00:01:53,195 --> 00:01:56,845
It's inside of these two brackets right here.

36
00:01:56,840 --> 00:02:00,350
And if that condition is true,

37
00:02:00,350 --> 00:02:05,940
then the code inside of these two squiggly lines is executed.

38
00:02:05,944 --> 00:02:09,384
If it's not, then we have something called the else.

39
00:02:09,380 --> 00:02:12,430
If, if this condition is not true,

40
00:02:12,425 --> 00:02:16,715
else if some other condition is true,

41
00:02:16,715 --> 00:02:19,085
this code will be executed.

42
00:02:19,085 --> 00:02:24,715
And if neither this condition nor the second one is true,

43
00:02:24,710 --> 00:02:27,370
then we have something called the else.

44
00:02:27,365 --> 00:02:33,485
And this code will be executed if none of the previous conditions are true,

45
00:02:33,485 --> 00:02:38,455
you can add as many else if statements as you want so you can add.

46
00:02:38,450 --> 00:02:43,070
The first one is always if you can even not do an else,

47
00:02:43,070 --> 00:02:48,860
if you can immediately add an else or you can add 15 else ifs, if you need to,

48
00:02:48,860 --> 00:02:51,290
you might never need that money,

49
00:02:51,290 --> 00:02:53,960
but if you ever do need more than one,

50
00:02:53,960 --> 00:02:55,510
more than two, you can add.

51
00:02:55,505 --> 00:02:57,895
So we have a condition in here.

52
00:02:57,890 --> 00:03:00,920
If it's true, this code executes and we stop.

53
00:03:00,920 --> 00:03:03,950
We do not continue looking down.

54
00:03:03,950 --> 00:03:06,980
If this not, this first one isn't true.

55
00:03:06,980 --> 00:03:08,290
We look to the second one.

56
00:03:08,285 --> 00:03:09,755
If it is, we execute,

57
00:03:09,755 --> 00:03:11,155
if it's not, we continue.

58
00:03:11,150 --> 00:03:16,450
Finally, if we don't find any condition that is true, we execute else.

59
00:03:16,445 --> 00:03:18,755
So let's go ahead and implement that.

60
00:03:18,755 --> 00:03:21,625
So what are we looking for in here?

61
00:03:21,620 --> 00:03:25,610
We are looking to rotate the entire object.

62
00:03:25,609 --> 00:03:29,739
If so, what are we looking for in here?

63
00:03:29,735 --> 00:03:33,485
We want to see if the mouse position

64
00:03:33,485 --> 00:03:37,735
is less than because let's look at the dome player for example,

65
00:03:37,730 --> 00:03:41,450
this is the position on the x back in the scene.

66
00:03:41,450 --> 00:03:44,720
So when we are moving backwards,

67
00:03:44,720 --> 00:03:49,580
you can see that it's less when we are moving to the right. That means it's small.

68
00:03:49,580 --> 00:03:53,890
So imagine the mouse position as being a another game object.

69
00:03:53,885 --> 00:03:58,885
For example, We are in here and this area to the left of the player,

70
00:03:58,880 --> 00:04:03,290
that means the most position is less than this green point,

71
00:04:03,290 --> 00:04:09,430
which means we want to rotate the entire don't player to the other side.

72
00:04:09,425 --> 00:04:11,795
But how are we actually going to do that?

73
00:04:11,795 --> 00:04:14,245
Well, there is the scale right here,

74
00:04:14,240 --> 00:04:15,890
which makes it super easy.

75
00:04:15,890 --> 00:04:18,440
So if I change the scale on the x,

76
00:04:18,440 --> 00:04:21,190
you can see that he becomes wider and wider.

77
00:04:21,185 --> 00:04:24,275
But what happens when we go and negative?

78
00:04:24,274 --> 00:04:26,074
So when we go Negative,

79
00:04:26,075 --> 00:04:27,715
yep, there we go.

80
00:04:27,710 --> 00:04:29,930
We actually flip the player.

81
00:04:29,930 --> 00:04:35,440
And this is what we want to be adjusting whenever we are looking in the other direction.

82
00:04:35,435 --> 00:04:40,675
And this direction, when the mouse position is less than the screen point,

83
00:04:40,670 --> 00:04:42,170
which is the player.

84
00:04:42,170 --> 00:04:44,540
We want to be looking right here.

85
00:04:44,540 --> 00:04:48,220
And this direction we want to change the scale to minus one,

86
00:04:48,215 --> 00:04:50,305
not only the ACS, also the y.

87
00:04:50,300 --> 00:04:51,760
We will see why.

88
00:04:51,755 --> 00:04:55,075
So now that we have those things established,

89
00:04:55,070 --> 00:04:57,980
let's create our first if statement.

90
00:04:57,980 --> 00:05:02,230
So in here, we are going to create enough.

91
00:05:02,225 --> 00:05:06,115
We're going to open the two brackets or columns.

92
00:05:06,110 --> 00:05:07,700
I'm not sure what they're called.

93
00:05:07,700 --> 00:05:11,050
We're going to check for the mouse position dot x.

94
00:05:11,045 --> 00:05:13,165
We don't need the y. It doesn't matter.

95
00:05:13,160 --> 00:05:19,760
And if it's less than the screen point, the RX.

96
00:05:19,755 --> 00:05:25,405
Then we are going to take the transform local scale,

97
00:05:25,405 --> 00:05:30,205
which is the local scale of the object holding this script.

98
00:05:30,205 --> 00:05:38,355
That means the entire don't player is going to be new vector 3 minus 1,

99
00:05:38,350 --> 00:05:42,980
f 1, f 1.

100
00:05:43,920 --> 00:05:46,550
Save that.

101
00:06:59,350 --> 00:07:01,450
We run the gain.

102
00:07:01,445 --> 00:07:03,535
This is while the y is still positive.

103
00:07:03,530 --> 00:07:05,490
So if we look in the other direction,

104
00:07:05,494 --> 00:07:07,894
you can see that the gun is upside down.

105
00:07:07,895 --> 00:07:13,045
So our goal, let me just show you and the way I pause this using keys is control

106
00:07:13,040 --> 00:07:18,730
shift P. So what we need to do is if we grab the weapons arm,

107
00:07:18,725 --> 00:07:21,235
if I set it to minus one, there you go.

108
00:07:21,230 --> 00:07:27,140
You can see that we are back at a proper scale or a proper advertisement.

109
00:07:27,135 --> 00:07:30,075
So I'm going to set this to minus1,

110
00:07:30,070 --> 00:07:33,900
save that back in our game, run the game.

111
00:07:33,895 --> 00:07:36,895
And now we look up and down, the gang rotates.

112
00:07:36,895 --> 00:07:39,525
If we go to the other side, there you go.

113
00:07:39,520 --> 00:07:41,820
The gun looks properly.

114
00:07:41,815 --> 00:07:44,565
Great. So everything is set up.

115
00:07:44,560 --> 00:07:46,710
The only problem is when we look back,

116
00:07:46,705 --> 00:07:48,235
the player doesn't change.

117
00:07:48,235 --> 00:07:54,705
So how are we going to make sure that when we don't have this condition as true,

118
00:07:54,700 --> 00:07:58,260
how are we going to rotate the player back?

119
00:07:58,255 --> 00:08:00,585
Well, it's your challenge.

120
00:08:00,580 --> 00:08:04,270
Your challenge is to create an else statement.

121
00:08:04,270 --> 00:08:07,710
You'll need to add to the if statements and else statements.

122
00:08:07,705 --> 00:08:09,705
We already saw how that looks.

123
00:08:09,705 --> 00:08:16,285
This will activate when the mouse position x is smaller than the screen point x.

124
00:08:16,280 --> 00:08:23,910
In that case, we need to set the local scales or all the local scales back to one.

125
00:08:24,370 --> 00:08:27,550
And I'm going to give you a small hint,

126
00:08:27,545 --> 00:08:31,775
which is to set the scale to vector three dot one,

127
00:08:31,775 --> 00:08:33,025
vector three dot one.

128
00:08:33,020 --> 00:08:36,010
You can also research on Unity's documentation.

129
00:08:36,005 --> 00:08:40,465
It means that it's a vector 3, 1, 1, 1.

130
00:08:40,460 --> 00:08:45,080
Pause the video right now and go do the challenge.

131
00:08:45,080 --> 00:08:47,270
Okay, welcome back.

132
00:08:47,270 --> 00:08:49,190
How did you get all of that?

133
00:08:49,190 --> 00:08:54,680
I really, really hope that you are always trying to bust with these challenges.

134
00:08:54,680 --> 00:08:57,680
So I'm going to create an else statement in here.

135
00:08:57,680 --> 00:09:03,530
And what I'm going to do is I'm going to take the transform local scale.

136
00:09:03,530 --> 00:09:07,930
And it's going to be equal to vector three dot one.

137
00:09:07,925 --> 00:09:09,665
Let's save that.

138
00:09:09,665 --> 00:09:13,435
And I'm going to also duplicate this.

139
00:09:13,430 --> 00:09:18,580
So the way we duplicate it as remember Control D. And instead of the transform,

140
00:09:18,575 --> 00:09:20,825
I'm going to use the weapons are,

141
00:09:20,825 --> 00:09:22,655
I'm going to save all of that.

142
00:09:22,655 --> 00:09:25,355
I'm going to go back into my game.

143
00:09:25,355 --> 00:09:27,365
I'm going to run it.

144
00:09:27,365 --> 00:09:30,335
So now I look to the right,

145
00:09:30,335 --> 00:09:32,015
my gun rotates properly.

146
00:09:32,015 --> 00:09:34,415
Everything is good. I look to the left,

147
00:09:34,415 --> 00:09:36,395
the gun rotates properly.

148
00:09:36,394 --> 00:09:39,214
The player is looking in the right direction.

149
00:09:39,215 --> 00:09:42,085
Now if I go back, there we go.

150
00:09:42,080 --> 00:09:46,090
So you can see that we have fixed our problem.

151
00:09:46,085 --> 00:09:49,265
The player always looks in the right direction.

152
00:09:49,265 --> 00:09:51,625
We have the gun pointing properly,

153
00:09:51,620 --> 00:09:55,180
and we finally look amazing while doing it.

154
00:09:55,175 --> 00:09:56,755
So now we can walk around.

155
00:09:56,750 --> 00:10:02,590
You can see that it's much more natural because it's always pointing towards the gun.

156
00:10:02,585 --> 00:10:06,955
Not only is the weapon pointing properly towards the gun,

157
00:10:06,950 --> 00:10:09,950
weapon problem pointing towards the mouse,

158
00:10:09,950 --> 00:10:12,310
but also deep layer,

159
00:10:12,305 --> 00:10:18,405
entire body is rotating and pointing in the right direction.

160
00:10:18,409 --> 00:10:23,749
So that done, make sure to go ahead and commit the changes that we made.

161
00:10:23,750 --> 00:10:28,730
And in the next video we are going to be adding more flavor to our games.

162
00:10:28,730 --> 00:10:32,870
How, by creating the walking, the idling,

163
00:10:32,870 --> 00:10:41,330
and maybe even the torpedo animations for the player can't wait to start animating.

164
00:10:41,330 --> 00:10:44,010
I'll see you in the next one.

