1
00:00:00,000 --> 00:00:06,240
Hey, welcome back, My favorite game developers in this video,

2
00:00:06,235 --> 00:00:10,125
our gun is no longer a static object.

3
00:00:10,120 --> 00:00:11,740
If you look closely,

4
00:00:11,740 --> 00:00:13,810
you can see that when we move down,

5
00:00:13,810 --> 00:00:15,250
the gun moves down.

6
00:00:15,250 --> 00:00:16,600
When we look up,

7
00:00:16,600 --> 00:00:17,970
the gun locks up.

8
00:00:17,965 --> 00:00:21,225
Obviously, there are still a couple of things that need to be

9
00:00:21,220 --> 00:00:25,740
fixed because holding the gun like that I feel is pretty uncomfortable,

10
00:00:25,735 --> 00:00:27,505
but no worries about that.

11
00:00:27,505 --> 00:00:31,735
This is the first step in naming our gun properly.

12
00:00:31,735 --> 00:00:35,635
So let's not waste any more time and let's get started.

13
00:00:35,635 --> 00:00:37,335
There's a lot of math in this.

14
00:00:37,330 --> 00:00:40,070
So put your thinking caps on.

15
00:00:41,770 --> 00:00:46,810
Now that we are moving and we're bumping into things,

16
00:00:46,805 --> 00:00:50,135
it's time to start aiming our weapon.

17
00:00:50,135 --> 00:00:55,495
To do that, we want to rotate the weapon around a certain position.

18
00:00:55,490 --> 00:01:01,460
We don't want to actually be rotating the exact weapon because if we look in here,

19
00:01:01,460 --> 00:01:03,400
if we go to the scene,

20
00:01:03,395 --> 00:01:04,475
you can see right now,

21
00:01:04,475 --> 00:01:06,995
if we try to rotate the gun as it is,

22
00:01:06,995 --> 00:01:08,675
it doesn't look as good.

23
00:01:08,675 --> 00:01:13,415
We need a point from which the gun will be rotating.

24
00:01:13,415 --> 00:01:17,915
So what we'll do is we're going to right-click on the dome player.

25
00:01:17,915 --> 00:01:20,675
We're going to create an empty game object.

26
00:01:20,675 --> 00:01:26,305
We are going to call it the weapons arm.

27
00:01:26,300 --> 00:01:29,720
And we're going to move it to a certain point.

28
00:01:29,720 --> 00:01:34,940
And this is the point where the gun or from which the gun will be rotating.

29
00:01:34,940 --> 00:01:40,570
So I'm going to put it around almost the hand or the arm of the player.

30
00:01:40,565 --> 00:01:42,575
And I am going to make the shotgun,

31
00:01:42,575 --> 00:01:45,575
a child of that weapons are.

32
00:01:45,575 --> 00:01:49,495
So now when we rotate the weapons arm,

33
00:01:49,490 --> 00:01:55,400
you'll notice that the shotgun stays in its place almost around the arm right here,

34
00:01:55,400 --> 00:02:02,720
and it looks much more natural as we rotate the shotgun or the weapons are.

35
00:02:02,720 --> 00:02:05,530
So now it's time for a bit of cold.

36
00:02:05,525 --> 00:02:07,105
But before we do that,

37
00:02:07,100 --> 00:02:10,330
how are we actually going to be rotating this arm?

38
00:02:10,325 --> 00:02:14,065
It's going to be dependent on where our mouth is.

39
00:02:14,060 --> 00:02:16,130
So let's say we are up here.

40
00:02:16,130 --> 00:02:19,190
We want the shot gun to point towards the mouse.

41
00:02:19,190 --> 00:02:21,770
How are we going to calculate that?

42
00:02:21,770 --> 00:02:29,210
Well, we will need a couple of points and let me explain it using math, yay math.

43
00:02:29,210 --> 00:02:31,130
So let's get started.

44
00:02:31,130 --> 00:02:34,900
First thing first we need the player position on the screen.

45
00:02:34,895 --> 00:02:37,345
Well, I use the term player position,

46
00:02:37,340 --> 00:02:42,500
but it's actually the point on which we are rotating the shotgun around.

47
00:02:42,500 --> 00:02:44,600
But for now, let's just keep it simple.

48
00:02:44,600 --> 00:02:46,690
Call this the player position,

49
00:02:46,685 --> 00:02:49,585
and we have the mouse position on the screen.

50
00:02:49,580 --> 00:02:55,360
So what we want is we want to get the vector on these two points.

51
00:02:55,355 --> 00:02:59,345
This is the direction which are gone will be pointing.

52
00:02:59,345 --> 00:03:06,055
But how are we actually going to calculate the angle we want the gun to be pointing in?

53
00:03:06,050 --> 00:03:11,390
Well, we know the exposition and we know the y position.

54
00:03:11,390 --> 00:03:13,580
Well, I meant X and Y.

55
00:03:13,580 --> 00:03:18,830
So what we can do is we can use something called the arc tangent.

56
00:03:18,830 --> 00:03:22,670
And this is something available for us in math.

57
00:03:22,670 --> 00:03:26,170
F of unit is library.

58
00:03:26,165 --> 00:03:29,555
So we can use that to calculate the arctan.

59
00:03:29,555 --> 00:03:33,595
And if you don't know what arctan or you don't even know what Dan is

60
00:03:33,590 --> 00:03:38,210
tangent I believe it's called it's the front side,

61
00:03:38,210 --> 00:03:41,960
which is the y divided by V x.

62
00:03:41,960 --> 00:03:43,370
And when we do that,

63
00:03:43,370 --> 00:03:45,050
we get a certain number,

64
00:03:45,050 --> 00:03:46,450
which is the tangent.

65
00:03:46,445 --> 00:03:48,355
So to get the angle,

66
00:03:48,350 --> 00:03:50,930
we use something called the arc tangent,

67
00:03:50,930 --> 00:03:52,960
which is kind of a reverse.

68
00:03:52,955 --> 00:03:57,035
But all you need to know is that we get the difference in the height,

69
00:03:57,035 --> 00:04:00,475
we get the difference in the distance.

70
00:04:00,470 --> 00:04:05,950
And using that, we calculate the angle in which argon should be pointing out.

71
00:04:05,945 --> 00:04:08,825
This is basic elementary math.

72
00:04:08,825 --> 00:04:10,975
I'm not sure if you're familiar with that.

73
00:04:10,970 --> 00:04:13,960
If you want to delve deeper into it, be my guest,

74
00:04:13,955 --> 00:04:17,275
I believe this will be enough explanation.

75
00:04:17,270 --> 00:04:22,030
So let's translate these mathematical equations into mathematical codes.

76
00:04:22,025 --> 00:04:26,695
So I'm going to go back and our script in here in the player controller.

77
00:04:26,690 --> 00:04:32,540
And the first thing we need is the two reference for the camera and for the weapons are.

78
00:04:32,540 --> 00:04:36,550
So I'm going to start off by creating a serialized field,

79
00:04:36,545 --> 00:04:38,755
which will be a transform.

80
00:04:38,750 --> 00:04:44,540
And it's going to represent the weapons are great.

81
00:04:44,540 --> 00:04:49,130
The second variable we're going to create as a private camera,

82
00:04:49,130 --> 00:04:51,910
and it's going to be the main camera.

83
00:04:51,905 --> 00:04:55,735
And the cool thing about this is that we can immediately go into

84
00:04:55,730 --> 00:05:00,670
Stuart and say that the main camera is equal to the camera.

85
00:05:00,665 --> 00:05:02,495
Dot main.

86
00:05:02,495 --> 00:05:04,735
And using this camera, that main,

87
00:05:04,730 --> 00:05:09,610
we immediately can get a reference to the main camera which we have.

88
00:05:09,605 --> 00:05:11,155
And here, great.

89
00:05:11,150 --> 00:05:15,550
Now we need to know where our mouth is in the world.

90
00:05:15,545 --> 00:05:17,995
So we are going to scroll down and here,

91
00:05:17,990 --> 00:05:21,970
and I'm just going to remove these comments because we no longer need them.

92
00:05:21,965 --> 00:05:23,875
If you feel you need them,

93
00:05:23,870 --> 00:05:29,240
keep them as a reminder for later on how we changed and moved through our code.

94
00:05:29,240 --> 00:05:32,780
So now I'm going to get the position of the mouse.

95
00:05:32,780 --> 00:05:36,100
I'm going to use a vector three.

96
00:05:36,095 --> 00:05:41,545
And it's going to be called the mouse position.

97
00:05:41,540 --> 00:05:48,080
And it's going to be equal to the input dot mouse position.

98
00:05:48,080 --> 00:05:52,340
And you can see right now that immediately the Visual Studio Community

99
00:05:52,340 --> 00:05:56,810
gives us the current mouse position in pixel coordinates.

100
00:05:56,810 --> 00:05:58,670
So great, that's what we need.

101
00:05:58,670 --> 00:06:00,970
And obviously, I already know this stuff.

102
00:06:00,965 --> 00:06:03,215
If you want to research this,

103
00:06:03,215 --> 00:06:05,915
you would have needed to go into Google,

104
00:06:05,915 --> 00:06:07,595
how to go get most position.

105
00:06:07,595 --> 00:06:10,915
You have gotten the documentations for unity.

106
00:06:10,910 --> 00:06:14,410
You would have found this input dot mouse position.

107
00:06:14,405 --> 00:06:15,635
You would have used it.

108
00:06:15,635 --> 00:06:17,245
This is just a shortcut.

109
00:06:17,240 --> 00:06:20,420
Let's continue on. Next thing we need is the screen point.

110
00:06:20,420 --> 00:06:27,830
So screen point, and it's going to be equal to the main camera,

111
00:06:27,830 --> 00:06:33,560
the world camera, world to screen point.

112
00:06:33,560 --> 00:06:36,350
And you can see the definition and here is

113
00:06:36,350 --> 00:06:40,820
the transform position from world space and to screen space.

114
00:06:40,820 --> 00:06:43,190
There we go. This is what we need.

115
00:06:43,190 --> 00:06:48,560
Transform local scale, I mean local position.

116
00:06:48,560 --> 00:06:52,490
And this will give us the local position of the transform,

117
00:06:52,490 --> 00:06:55,130
which is the player right now.

118
00:06:55,130 --> 00:06:58,750
And now it's time to calculate the angle.

119
00:06:58,745 --> 00:07:01,205
Let's first of all, save all of that.

120
00:07:01,205 --> 00:07:06,315
Let's go ahead and assign the arm for the player.

121
00:07:06,315 --> 00:07:09,145
So I'm going to drag the weapons arm right now.

122
00:07:09,145 --> 00:07:10,495
We're going to save that.

123
00:07:10,495 --> 00:07:14,925
And now the next step is to actually calculate the angle.

124
00:07:14,920 --> 00:07:17,520
So I'm going to first of all,

125
00:07:17,515 --> 00:07:22,485
show you this again so you can go back and look at it if you need to.

126
00:07:22,480 --> 00:07:25,440
And then I'm going to issue you a challenge.

127
00:07:25,435 --> 00:07:29,415
And your challenge is going to be to calculate the angle.

128
00:07:29,410 --> 00:07:33,030
So first thing you do is you need to subtract

129
00:07:33,025 --> 00:07:38,635
the mouse x position from the screen point position of the x.

130
00:07:38,635 --> 00:07:42,715
Obviously, you will need to do the same for the y.

131
00:07:42,715 --> 00:07:44,535
And then you'll need to calculate

132
00:07:44,530 --> 00:07:51,020
the math F dot arctangent to find that in the Unity documentation.

133
00:07:51,020 --> 00:07:53,650
Go ahead, pause the video right now.

134
00:07:53,645 --> 00:07:54,905
Go do your best.

135
00:07:54,905 --> 00:07:55,655
Try it.

136
00:07:55,655 --> 00:07:58,955
You won't lose anything by doing your best.

137
00:07:58,955 --> 00:08:03,385
Pause the video right now and go do the challenge.

138
00:08:03,380 --> 00:08:05,810
Hey, welcome back.

139
00:08:05,810 --> 00:08:07,250
So as you can see,

140
00:08:07,250 --> 00:08:09,110
I've just looked up math,

141
00:08:09,110 --> 00:08:11,600
arc tangent in Unity.

142
00:08:11,600 --> 00:08:13,660
Now I am right here.

143
00:08:13,655 --> 00:08:17,695
What do I do? Well, I need to see how this works.

144
00:08:17,690 --> 00:08:19,890
What does do? It returns a float.

145
00:08:19,894 --> 00:08:23,004
This is our how I use matlab.org Tange.

146
00:08:23,000 --> 00:08:24,200
Okay, great.

147
00:08:24,200 --> 00:08:25,960
Then let's go back in here.

148
00:08:25,955 --> 00:08:29,605
So first thing we need to do is we need to calculate the difference.

149
00:08:29,600 --> 00:08:33,760
I'm going to put both of these into a vector two.

150
00:08:33,755 --> 00:08:36,415
So I'm going to use a vector to,

151
00:08:36,410 --> 00:08:39,330
I'll call this the offset.

152
00:08:39,334 --> 00:08:42,484
So it's the offset between the two points.

153
00:08:42,485 --> 00:08:47,825
It will be equal to a new vector two.

154
00:08:47,825 --> 00:08:55,145
And in here I'm going to first of all get the mouse position dot x and subtract from it,

155
00:08:55,145 --> 00:08:58,495
the screen point directs.

156
00:08:58,490 --> 00:09:03,060
And I'm going to do the same on the why.

157
00:09:07,750 --> 00:09:13,870
Now I'm going to create a float because as we saw this method,

158
00:09:13,865 --> 00:09:17,675
the method dot arctan returns a float,

159
00:09:17,675 --> 00:09:25,225
is going to be equal to math F dot arc tan or no, Eta.

160
00:09:25,220 --> 00:09:27,980
It's called a tan two.

161
00:09:27,980 --> 00:09:36,600
And I'm going to use the offset dot y divided by the offset DRX.

162
00:09:36,730 --> 00:09:39,620
Great, so I'm going to save that.

163
00:09:39,620 --> 00:09:46,780
But whenever we want to assign the actual rotation to the weapons are,

164
00:09:46,775 --> 00:09:50,045
we cannot simply do it using a float.

165
00:09:50,045 --> 00:09:54,745
What we need to do is we need to use an extra method,

166
00:09:54,740 --> 00:10:00,880
which is the method that rad two degrees.

167
00:10:00,875 --> 00:10:04,745
And you can see this converts radians to degrees constant.

168
00:10:04,745 --> 00:10:07,405
So we will need to do that.

169
00:10:07,400 --> 00:10:11,720
And the final thing we need to do is we will need to access the weapons on

170
00:10:11,720 --> 00:10:17,170
dot rotation and now get ready for a bit of a scary world.

171
00:10:17,165 --> 00:10:19,555
We are going to use quaternions.

172
00:10:19,550 --> 00:10:23,810
So quaternion dot Bueller.

173
00:10:23,809 --> 00:10:30,169
And we're going to set it as 00 on the x-axis.

174
00:10:30,170 --> 00:10:32,870
We don't rotate on the y-axis, we don't rotate.

175
00:10:32,870 --> 00:10:36,830
We only rotate around the z-axis using the angle.

176
00:10:36,830 --> 00:10:39,320
Now, before you run away,

177
00:10:39,320 --> 00:10:42,580
what is Turnitin dot Hulu?

178
00:10:42,575 --> 00:10:45,155
If you look on the earlier right here,

179
00:10:45,155 --> 00:10:50,285
you can see that it returns a rotation that rotate Z degrees around z axis,

180
00:10:50,285 --> 00:10:51,715
x around x axis,

181
00:10:51,710 --> 00:10:53,060
and so on and so forth.

182
00:10:53,060 --> 00:10:55,400
So quaternion dot.

183
00:10:55,400 --> 00:10:57,020
If you are interested,

184
00:10:57,020 --> 00:10:58,930
you can delve deep into this.

185
00:10:58,925 --> 00:11:03,055
Me personally, I've been rotating objects in Unity for

186
00:11:03,050 --> 00:11:07,460
several years and I've never quite understood what they

187
00:11:07,460 --> 00:11:11,630
mean by quaternion is just a way to convert

188
00:11:11,630 --> 00:11:16,340
from the XYZ into an actual rotation and space.

189
00:11:16,340 --> 00:11:19,550
So feel free to delve deep,

190
00:11:19,550 --> 00:11:22,450
go look it up, make sure you understand everything.

191
00:11:22,445 --> 00:11:25,285
If not, if you're not bothered by not fully

192
00:11:25,280 --> 00:11:28,910
understanding what is happening with quaternion u dot.

193
00:11:28,910 --> 00:11:34,640
Trust me, you can go through your game development journey without worrying about it.

194
00:11:34,640 --> 00:11:38,150
So this is simply a way to convert x,

195
00:11:38,150 --> 00:11:41,450
y, and z into an angle into a rotation.

196
00:11:41,450 --> 00:11:42,580
We'll save that.

197
00:11:42,575 --> 00:11:44,555
We'll go back into Unity.

198
00:11:44,555 --> 00:11:47,915
And we are going to run the game.

199
00:11:47,915 --> 00:11:51,875
And let's see what is happening with the gun right now,

200
00:11:51,875 --> 00:11:53,995
we move up, the gun moves up.

201
00:11:53,990 --> 00:11:56,210
We move down. The gun moves down.

202
00:11:56,210 --> 00:11:58,430
If we go to the left, yep.

203
00:11:58,430 --> 00:12:02,480
Our gun looks a bit weird when we look in the other direction,

204
00:12:02,480 --> 00:12:06,490
but when it comes to the proper direction, there you go.

205
00:12:06,485 --> 00:12:09,445
We move and even adjusts as we walk.

206
00:12:09,440 --> 00:12:11,830
So you can see when we move up,

207
00:12:11,825 --> 00:12:13,115
the gun moves down.

208
00:12:13,115 --> 00:12:18,425
Always looking at exactly where our mouse position is.

209
00:12:18,425 --> 00:12:22,315
So great now we have a gun that is actually firing,

210
00:12:22,310 --> 00:12:25,910
which is amazing in my opinion, not actually firing,

211
00:12:25,910 --> 00:12:30,730
which will be firing in the right direction later on as we move.

212
00:12:30,725 --> 00:12:33,445
But obviously there is something wrong when

213
00:12:33,440 --> 00:12:36,280
we try to move the mouse in the other direction,

214
00:12:36,275 --> 00:12:38,025
it does move properly.

215
00:12:38,029 --> 00:12:43,069
That's what we are going to be fixing in the next video while learning

216
00:12:43,069 --> 00:12:48,699
something very nice encoding which is called the if statements and if conditions.

217
00:12:48,695 --> 00:12:50,845
So I'll see you there.

218
00:12:50,840 --> 00:12:52,160
But before we go,

219
00:12:52,160 --> 00:12:55,640
always make sure to stage all your files.

220
00:12:55,640 --> 00:12:57,250
Mega commits.

221
00:12:57,245 --> 00:13:03,125
I'm going to call this made my gun point in

222
00:13:03,125 --> 00:13:09,575
the right direction or not envy right angle.

223
00:13:09,575 --> 00:13:10,945
Because in the next video,

224
00:13:10,940 --> 00:13:13,250
we'll make it point in the right direction,

225
00:13:13,250 --> 00:13:16,520
will make the whole player point and the right direction.

226
00:13:16,520 --> 00:13:19,290
So, see you then.

