1
00:00:00,020 --> 00:00:03,690
Welcome back, my fellow game developers.

2
00:00:03,690 --> 00:00:07,590
In this video, we are actually going to put the inputs we created to

3
00:00:07,590 --> 00:00:12,050
use and we're going to see how important variables are.

4
00:00:12,045 --> 00:00:15,105
So now when we run our gain,

5
00:00:15,105 --> 00:00:18,275
you'll notice that we don't just stand there.

6
00:00:18,270 --> 00:00:20,450
You don't have anything in the console.

7
00:00:20,445 --> 00:00:23,135
When we press the WASD key,

8
00:00:23,130 --> 00:00:27,020
we can see that our player moves left,

9
00:00:27,020 --> 00:00:29,510
right, left, up, down.

10
00:00:29,510 --> 00:00:32,540
And not only that, if I increase the movement speed,

11
00:00:32,540 --> 00:00:34,790
there we go, We are much faster.

12
00:00:34,790 --> 00:00:37,670
So not only do we move the player with the keys,

13
00:00:37,670 --> 00:00:41,590
we also can control how fast he moved.

14
00:00:41,585 --> 00:00:43,025
So well, that said,

15
00:00:43,025 --> 00:00:48,125
let's not waste any more time and let's get started.

16
00:00:48,670 --> 00:00:53,960
Okay, so now that we have the inputs ready,

17
00:00:53,960 --> 00:00:55,660
we see how they work.

18
00:00:55,655 --> 00:00:58,775
It's time to actually start moving the player.

19
00:00:58,775 --> 00:01:00,205
As we promised.

20
00:01:00,200 --> 00:01:02,330
Let's open up the script right here.

21
00:01:02,330 --> 00:01:06,010
And let's go ahead and remove these debug dot log.

22
00:01:06,005 --> 00:01:07,925
These were just here too.

23
00:01:07,925 --> 00:01:11,105
Make us believe that this is actually working.

24
00:01:11,105 --> 00:01:14,515
Okay, so the first thing that we need to do is we

25
00:01:14,510 --> 00:01:18,430
need to access the transformed position of the player.

26
00:01:18,425 --> 00:01:22,535
So we're going to go ahead and click Transform.

27
00:01:22,535 --> 00:01:26,825
And you can see right here how helpful visual Studio is already.

28
00:01:26,825 --> 00:01:29,515
It's telling us what the transform is.

29
00:01:29,510 --> 00:01:36,650
So the transform that will write right now is the transport attached to this game object.

30
00:01:36,650 --> 00:01:38,000
Which game object?

31
00:01:38,000 --> 00:01:43,400
It's the game object that has the player controller script on it.

32
00:01:43,400 --> 00:01:45,470
And if we look back in here,

33
00:01:45,470 --> 00:01:48,610
we click on the layer.

34
00:01:48,605 --> 00:01:51,485
Anytime now we click on the player.

35
00:01:51,485 --> 00:01:55,885
We can see that the player controller is attached to the player and

36
00:01:55,880 --> 00:02:01,390
the transform which is on the gameObject don't player as this one.

37
00:02:01,385 --> 00:02:04,935
So we're accessing this specific transform.

38
00:02:04,939 --> 00:02:06,729
So back in here,

39
00:02:06,725 --> 00:02:08,725
we'll write transform again,

40
00:02:08,720 --> 00:02:12,280
hit Tab to complete the word dot position.

41
00:02:12,275 --> 00:02:15,905
You can see that can access the position, the scale,

42
00:02:15,905 --> 00:02:20,185
the rotation, everything that is available on the transform.

43
00:02:20,180 --> 00:02:22,250
And we write position.

44
00:02:22,250 --> 00:02:28,320
Now, this is going to be equal to a new vector three.

45
00:02:28,324 --> 00:02:30,274
And it's going to be,

46
00:02:30,275 --> 00:02:33,395
let's say for now just 0.1 f.

47
00:02:33,395 --> 00:02:38,495
And we can also write dot 1 f. This means 0,

48
00:02:38,495 --> 00:02:42,625
and we'll keep the z axis on the same place. We'll save that.

49
00:02:42,620 --> 00:02:45,670
Now, you might be thinking why Vector3

50
00:02:45,665 --> 00:02:49,175
didn't we just say that we don't want to modify this?

51
00:02:49,175 --> 00:02:51,515
Well, if we hover over position,

52
00:02:51,515 --> 00:02:57,205
you can see that the world space position of the transform as a vector three,

53
00:02:57,200 --> 00:03:01,720
that can also go through the documentations and see that it

54
00:03:01,715 --> 00:03:06,515
is actually the transformed position of any object,

55
00:03:06,515 --> 00:03:09,025
regardless whether we are using 3D,

56
00:03:09,020 --> 00:03:11,450
2D 1D, That's impossible,

57
00:03:11,450 --> 00:03:14,720
but you get the picture vector three position.

58
00:03:14,720 --> 00:03:17,230
We always access the vector C position,

59
00:03:17,225 --> 00:03:20,225
but we keep it as 0 f.

60
00:03:20,225 --> 00:03:22,375
Now the scope is not complete,

61
00:03:22,370 --> 00:03:26,870
but I'm going to use it just so we learn as we make mistakes,

62
00:03:26,870 --> 00:03:28,190
I'm going to save this.

63
00:03:28,190 --> 00:03:30,400
I'm going to go back in here.

64
00:03:30,395 --> 00:03:34,285
And let's just get some real values in here so we know,

65
00:03:34,280 --> 00:03:38,720
so minus five and let's say this is minus1 will save that.

66
00:03:38,720 --> 00:03:43,610
We're going to run our game and let's look. There we go.

67
00:03:43,610 --> 00:03:46,240
So 0.10.1.

68
00:03:46,235 --> 00:03:49,475
Now, what seems to be the problem?

69
00:03:49,475 --> 00:03:52,505
Aren't we calling it every single frame?

70
00:03:52,504 --> 00:03:59,484
Well, the problem here is that we are simply assigning this value right here,

71
00:03:59,480 --> 00:04:04,550
the new vector, 30.10.10 f to the position.

72
00:04:04,550 --> 00:04:09,130
Every single frame we are not increasing the transform position.

73
00:04:09,125 --> 00:04:12,095
So how are we going to start increasing?

74
00:04:12,095 --> 00:04:14,195
Let me just stop the playwright here.

75
00:04:14,195 --> 00:04:21,565
We're going to write that we take the transform dot position of the game object

76
00:04:21,560 --> 00:04:28,930
and then we add to it the 0.10.1 vector 3, every single frame.

77
00:04:28,925 --> 00:04:31,115
So we're going to save that.

78
00:04:31,115 --> 00:04:33,265
And let's go back and look at our game,

79
00:04:33,260 --> 00:04:35,060
how it looks right now.

80
00:04:35,055 --> 00:04:39,825
We run the game and there you go. Very goals.

81
00:04:39,820 --> 00:04:41,860
The player has just left the building,

82
00:04:41,860 --> 00:04:45,220
as you can see, as you can see right now what happens in here.

83
00:04:45,220 --> 00:04:47,650
We take the transform.py position.

84
00:04:47,650 --> 00:04:51,820
We assign in the already available transform,

85
00:04:51,820 --> 00:04:54,460
but position plus the vector 3,

86
00:04:54,460 --> 00:04:56,710
and we do that every single frame.

87
00:04:56,710 --> 00:04:59,850
So let's say we start off at 0, 0.

88
00:04:59,845 --> 00:05:02,175
We take 000 right here,

89
00:05:02,170 --> 00:05:03,630
we add to this vector,

90
00:05:03,625 --> 00:05:07,035
the next frame we have 0.21.

91
00:05:07,030 --> 00:05:12,030
We take it, we add to it, it becomes 0.2.

92
00:05:12,025 --> 00:05:15,285
We take it, we add to it becomes 0.3.

93
00:05:15,280 --> 00:05:17,540
So every single frame we increase it.

94
00:05:17,535 --> 00:05:20,875
Now, as you can see, there is redundant code in here.

95
00:05:20,870 --> 00:05:24,370
Redundant means we are using it multiple times.

96
00:05:24,365 --> 00:05:28,225
To avoid this, we can simply remove this right

97
00:05:28,220 --> 00:05:32,300
here and instead add a plus behind the equals.

98
00:05:32,300 --> 00:05:35,930
And this is the exact same syntax as before.

99
00:05:35,930 --> 00:05:37,840
So if we write this again,

100
00:05:37,835 --> 00:05:40,675
let me just duplicate this.

101
00:05:40,670 --> 00:05:45,050
I'll simply add two slashes.

102
00:05:45,050 --> 00:05:46,420
This becomes a comment.

103
00:05:46,415 --> 00:05:47,465
And as we've said,

104
00:05:47,465 --> 00:05:50,005
a common does not execute the code,

105
00:05:50,000 --> 00:05:52,640
and we'll simply remove this.

106
00:05:52,640 --> 00:05:58,010
So as a small reminder of what this plus equals means.

107
00:05:58,010 --> 00:05:59,780
So we'll save that.

108
00:05:59,780 --> 00:06:03,290
We'll go back into our game and let's just make this a bit smaller,

109
00:06:03,290 --> 00:06:05,620
so we'll add another 0 and here,

110
00:06:05,615 --> 00:06:08,015
save that back in our game.

111
00:06:08,015 --> 00:06:11,585
We run the code, the game.

112
00:06:11,585 --> 00:06:13,825
And there we go,

113
00:06:13,820 --> 00:06:17,590
we can see him slowly floating away and again, great.

114
00:06:17,585 --> 00:06:20,795
So now we have our player moving around.

115
00:06:20,795 --> 00:06:24,085
But we don't want these values to be just arbitrary.

116
00:06:24,080 --> 00:06:27,710
We don't want to have 0.1 or 0.01.

117
00:06:27,710 --> 00:06:32,680
We actually want the input that we've gotten here to move the player.

118
00:06:32,675 --> 00:06:35,665
So this will be your challenge.

119
00:06:35,660 --> 00:06:39,160
Your challenge is to move the player position with the input.

120
00:06:39,155 --> 00:06:43,745
So you're going to access the position of the player's transform.

121
00:06:43,745 --> 00:06:47,635
You're going to make the position a new vector 3.

122
00:06:47,630 --> 00:06:51,320
And you're going to use the movement input.txt and dot

123
00:06:51,320 --> 00:06:54,800
y as the x and y of this new vector

124
00:06:54,800 --> 00:07:02,410
3 will also keep the z axis at 0 F. And some just a bit of extra help.

125
00:07:02,405 --> 00:07:06,775
I recommend that you go through the Unity documentation and see the example.

126
00:07:06,770 --> 00:07:10,720
They were so nice at Unity company somewhere

127
00:07:10,715 --> 00:07:15,565
to give us an example of exactly what we are looking for.

128
00:07:15,560 --> 00:07:21,220
So pause the video right now and go do the challenge.

129
00:07:21,215 --> 00:07:23,305
Okay, welcome back.

130
00:07:23,300 --> 00:07:25,430
So if we scroll down right here,

131
00:07:25,430 --> 00:07:28,720
you can see that we get the horizontal input,

132
00:07:28,715 --> 00:07:30,685
we get the vertical input,

133
00:07:30,680 --> 00:07:35,150
and then we put all of them inside of the transformed position.

134
00:07:35,150 --> 00:07:39,260
This is the exact example that we need and our gain.

135
00:07:39,260 --> 00:07:45,580
So if we go back in here, instead of 0.01,

136
00:07:45,575 --> 00:07:55,465
I'm going to write the movement input dot x. I'm going to write the movement input dot y.

137
00:07:55,460 --> 00:07:58,240
And I'm going to save all of that.

138
00:07:58,235 --> 00:07:59,915
Now back in our game,

139
00:07:59,915 --> 00:08:03,655
we run the game and now our player doesn't move.

140
00:08:03,650 --> 00:08:05,380
If we want to move him to the right,

141
00:08:05,375 --> 00:08:07,745
we had the Dickey he moves to the right,

142
00:08:07,745 --> 00:08:09,985
the left, and he disappeared.

143
00:08:09,980 --> 00:08:14,690
So as you can see, our player is extremely, extremely fast.

144
00:08:14,690 --> 00:08:19,970
Why is that? Well, it's because updates being called every single frame.

145
00:08:19,970 --> 00:08:21,890
So if we try to play again,

146
00:08:21,890 --> 00:08:23,680
let me show you what I mean.

147
00:08:23,675 --> 00:08:25,915
If we look up here on the stats,

148
00:08:25,910 --> 00:08:28,670
we can see how many frames per second we have.

149
00:08:28,670 --> 00:08:30,830
So you know what frames per second is?

150
00:08:30,830 --> 00:08:33,260
If you've ever played a game and you can see right now

151
00:08:33,260 --> 00:08:36,290
it's almost a thousand frames per second.

152
00:08:36,290 --> 00:08:40,210
We are calling this a thousand times, a 1000 times.

153
00:08:40,205 --> 00:08:41,545
We are adding one,

154
00:08:41,540 --> 00:08:46,250
so we're adding a 1000 every time or every second if you'd like.

155
00:08:46,250 --> 00:08:48,890
So how are we going to control this?

156
00:08:48,890 --> 00:08:51,220
Well, there is a very simple way,

157
00:08:51,215 --> 00:08:56,785
and that is by multiplying this by Time.deltaTime.

158
00:08:56,780 --> 00:08:59,060
And what happens right now,

159
00:08:59,060 --> 00:09:01,120
Time.deltaTime, if we hover over it,

160
00:09:01,115 --> 00:09:05,645
it's the interval and seconds from the last frame to the current one.

161
00:09:05,645 --> 00:09:10,925
So what this does is it takes the time between each frame,

162
00:09:10,924 --> 00:09:16,074
we multiply it by this one and now will become frame rate and dependent.

163
00:09:16,070 --> 00:09:18,410
Let me show you what that means.

164
00:09:18,410 --> 00:09:22,550
This means that when we run the game,

165
00:09:22,550 --> 00:09:27,880
the time it takes from the first frame to the second one is added into the movement.

166
00:09:27,875 --> 00:09:30,965
And now when we hit the D key, there we go.

167
00:09:30,965 --> 00:09:32,705
We are much slower.

168
00:09:32,705 --> 00:09:36,715
And if we had the W, we move up, SVMs down, a,

169
00:09:36,710 --> 00:09:39,040
we move left, move right,

170
00:09:39,035 --> 00:09:41,035
and so on and so forth.

171
00:09:41,030 --> 00:09:42,580
How cool is that?

172
00:09:42,575 --> 00:09:45,515
What we have in other plural,

173
00:09:45,515 --> 00:09:48,085
who we are extremely low.

174
00:09:48,080 --> 00:09:50,300
This is no way of playing a game,

175
00:09:50,300 --> 00:09:53,270
especially when we have scary enemies following us.

176
00:09:53,270 --> 00:09:58,720
So this is why we had this movement speed variable all the time.

177
00:09:58,715 --> 00:10:01,535
This will allow us to control our speed.

178
00:10:01,535 --> 00:10:06,815
So now we will also multiply this by the movement speed.

179
00:10:06,815 --> 00:10:08,255
We will save that.

180
00:10:08,255 --> 00:10:09,925
So back in our gain,

181
00:10:09,920 --> 00:10:12,050
we can see that the movement speed is 20.

182
00:10:12,050 --> 00:10:13,540
Let's set it to five.

183
00:10:13,535 --> 00:10:16,255
Save that, run the game.

184
00:10:16,250 --> 00:10:18,580
And now we can move up,

185
00:10:18,575 --> 00:10:21,605
down, left, right, however we want.

186
00:10:21,605 --> 00:10:25,415
How cool is that we finally have a character,

187
00:10:25,415 --> 00:10:30,685
a player that is moving in a world that we've created by using simple code.

188
00:10:30,680 --> 00:10:33,220
Let's make this 50.

189
00:10:33,215 --> 00:10:35,105
Let's look at it. Well, there we go.

190
00:10:35,105 --> 00:10:37,895
We can even control how fast it goes.

191
00:10:37,895 --> 00:10:41,015
I mean, seriously, how excited are you?

192
00:10:41,015 --> 00:10:42,655
Maybe it's just me.

193
00:10:42,650 --> 00:10:44,920
Let's keep it at five. I think five.

194
00:10:44,915 --> 00:10:47,315
No fibers actually slow. Let's make then.

195
00:10:47,315 --> 00:10:48,205
Yeah, there we go.

196
00:10:48,200 --> 00:10:51,200
So 10 looks really, really nice.

197
00:10:51,200 --> 00:10:52,480
And with that done,

198
00:10:52,475 --> 00:10:54,085
I hope you enjoy it.

199
00:10:54,080 --> 00:10:55,960
Before I introduce the next video,

200
00:10:55,955 --> 00:10:58,255
you'll notice that we can actually still walk through

201
00:10:58,250 --> 00:11:00,650
walls and walk away, which is not good.

202
00:11:00,650 --> 00:11:02,060
That's why in the next video,

203
00:11:02,060 --> 00:11:05,540
we will be applying physics to our player and making

204
00:11:05,540 --> 00:11:09,350
him interact with various objects in our world.

205
00:11:09,350 --> 00:11:11,030
With that said, four,

206
00:11:11,030 --> 00:11:13,550
I go make sure to commit your changes.

207
00:11:13,550 --> 00:11:15,920
I'm not going to do with every single video because

208
00:11:15,920 --> 00:11:18,530
it's something that you have to become automatic.

209
00:11:18,529 --> 00:11:21,499
And every time you do something important in your game,

210
00:11:21,500 --> 00:11:24,110
make sure to stage all work,

211
00:11:24,110 --> 00:11:25,610
commit all your work,

212
00:11:25,610 --> 00:11:29,100
and I'll see you in the next video.

