Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
1
00:00:00,000 --> 00:00:04,030
[MUSIC]
2
2
00:00:04,030 --> 00:00:07,125
In this lecture, we're gonna make our
player move up, down, left, right.
3
3
00:00:07,125 --> 00:00:10,356
We'll be introducing the concept
of Time.deltaTime, so
4
4
00:00:10,356 --> 00:00:13,075
that we can make our game
frame rate independent.
5
5
00:00:13,075 --> 00:00:16,100
Okay, let's make our player ship move.
6
6
00:00:16,100 --> 00:00:20,480
First thing, I'm going to do is click
on my player and add a player script.
7
7
00:00:20,480 --> 00:00:23,850
So I clicked on my hierarchy
on the player, game object.
8
8
00:00:23,850 --> 00:00:27,175
And I will now in the [INAUDIBLE]
click on add component.
9
9
00:00:27,175 --> 00:00:30,138
We will call this player,
I've already got it typed in there.
10
10
00:00:30,138 --> 00:00:33,440
And then click on new script,
click Create and add.
11
11
00:00:33,440 --> 00:00:35,050
That will give us a new script.
12
12
00:00:35,050 --> 00:00:37,560
Over in my project folder, I will.
13
13
00:00:37,560 --> 00:00:38,400
Do I have a script done?
14
14
00:00:38,400 --> 00:00:39,430
I don't have a script folder yet.
15
15
00:00:39,430 --> 00:00:42,030
Okay, right click Create Folder.
16
16
00:00:42,030 --> 00:00:44,960
We'll call this script, there were go.
17
17
00:00:44,960 --> 00:00:47,680
Drag the player script
into the scripts folder.
18
18
00:00:47,680 --> 00:00:50,020
Double click on that and open up player.
19
19
00:00:50,020 --> 00:00:53,964
And that should open up Visual Studio for
us, if I can click on it correctly.
20
20
00:00:53,964 --> 00:00:56,726
Sometimes unity takes a little
bit of time to think.
21
21
00:00:56,726 --> 00:00:59,437
Need that half a second
where its like hang on Rick.
22
22
00:00:59,437 --> 00:01:03,430
Just slow down Rick, calm down,
hold your horses, I'm doing some stuff.
23
23
00:01:03,430 --> 00:01:06,770
Give it a minute, and
then I need to click on it again.
24
24
00:01:06,770 --> 00:01:07,874
Here we go,okay.
25
25
00:01:07,874 --> 00:01:10,220
Now, I'm in player.cs, excellante.
26
26
00:01:10,220 --> 00:01:12,850
Now, we're going to do something
to make the player move.
27
27
00:01:12,850 --> 00:01:15,205
And movement needs to
be happening on Update.
28
28
00:01:15,205 --> 00:01:19,200
Cuz we want it to be listening
our input every frame.
29
29
00:01:19,200 --> 00:01:21,830
Every single frame we want to have
the ability to say, move up and left and
30
30
00:01:21,830 --> 00:01:23,080
right and down.
31
31
00:01:23,080 --> 00:01:26,413
So I will start by creating a method,
Move ();.
32
32
00:01:26,413 --> 00:01:32,300
That doesn't exist yet, so
we're getting the red squiggly line.
33
33
00:01:32,300 --> 00:01:37,140
Let's give ourselves a little space down
below, and this will be a private void,
34
34
00:01:37,140 --> 00:01:38,610
so there's no return type.
35
35
00:01:38,610 --> 00:01:39,380
Call it Move.
36
36
00:01:40,520 --> 00:01:43,370
Whoops, auto-complete thought I
was trying to do something else.
37
37
00:01:43,370 --> 00:01:48,360
Move, and then open and
close our squiggly braces.
38
38
00:01:48,360 --> 00:01:50,110
Our curly braces is
probably the better term.
39
39
00:01:50,110 --> 00:01:51,800
Now what do we need to do in here?
40
40
00:01:51,800 --> 00:01:54,450
Well first of all, we want to create
41
41
00:01:54,450 --> 00:01:58,310
this concept that there will be
a new position that we're moving to.
42
42
00:01:58,310 --> 00:02:03,362
So I'm gonna write in
here var new nexXpos,
43
43
00:02:03,362 --> 00:02:06,010
short for position.
44
44
00:02:06,010 --> 00:02:06,810
We can write the whole thing,
45
45
00:02:06,810 --> 00:02:10,540
position but pos is one of those
things that we know it means position.
46
46
00:02:10,540 --> 00:02:12,170
And instead of next I'm saying new.
47
47
00:02:12,170 --> 00:02:15,320
There we go, lots of typos there,
sorry, new x pos.
48
48
00:02:15,320 --> 00:02:17,110
And you might say, well why is this a var?
49
49
00:02:17,110 --> 00:02:19,700
I'll get to that in a moment.
50
50
00:02:19,700 --> 00:02:22,360
Okay, equals, transform,
51
51
00:02:22,360 --> 00:02:27,460
dot position .x so we're doing
this on the x axis at the moment.
52
52
00:02:27,460 --> 00:02:31,610
Just the x axis and we're saying
where do we want to be on the x axis?
53
53
00:02:31,610 --> 00:02:34,602
And we're saying it is where we
currently are plus something.
54
54
00:02:34,602 --> 00:02:37,060
And that plus something is going
to be some sort of change so
55
55
00:02:37,060 --> 00:02:38,220
we'll call this deltaX.
56
56
00:02:38,220 --> 00:02:41,280
Delta referring to change or
difference, or
57
57
00:02:41,280 --> 00:02:46,780
distance between where we currently are
and where we want to be, and semicolon.
58
58
00:02:46,780 --> 00:02:47,740
Okay, so there's our first line.
59
59
00:02:47,740 --> 00:02:50,600
It doesn't know what deltaX is, so
we need to create that as well.
60
60
00:02:50,600 --> 00:02:53,950
I'm gonna type in var again, again,
I'll get back to var in just a moment.
61
61
00:02:53,950 --> 00:02:55,240
Have a little chat about that.
62
62
00:02:55,240 --> 00:03:00,060
deltaX, so the change equals,
this is gonna be cool here.
63
63
00:03:00,060 --> 00:03:04,030
We're gonna use Input, .GetAxis, and
64
64
00:03:04,030 --> 00:03:08,690
we haven't used GetAxis before, and
then we wanna know which particular axis.
65
65
00:03:08,690 --> 00:03:11,770
So waht is the axis, and where do we
get it from, and why are we using it?
66
66
00:03:11,770 --> 00:03:14,050
Let's jump back over into Unity.
67
67
00:03:14,050 --> 00:03:18,550
If you go up to Edit,
Project Settings, and find Inpu,
68
68
00:03:18,550 --> 00:03:20,720
you'll see we have the InputManager.
69
69
00:03:20,720 --> 00:03:24,628
It might be closed up for you,
you need to open up the Chevron thingy.
70
70
00:03:24,628 --> 00:03:28,945
And you can see a number of different
elements, a number of different aspects,
71
71
00:03:28,945 --> 00:03:30,433
axes in our InputManager.
72
72
00:03:30,433 --> 00:03:33,505
We have the Horizontal and
we have the Vertical.
73
73
00:03:33,505 --> 00:03:36,457
So in the past,
in one of our previous sections,
74
74
00:03:36,457 --> 00:03:40,201
in here we would have said
Input.GetKeyDown, for example.
75
75
00:03:40,201 --> 00:03:42,258
That's another, GetKeyDown,
76
76
00:03:42,258 --> 00:03:46,010
it's another method that we can
use within the Input class.
77
77
00:03:46,010 --> 00:03:49,643
And GetKeyDown, we would say
specifically what particular keys.
78
78
00:03:49,643 --> 00:03:52,203
So it might space or we might say,
a, or we might say, b.
79
79
00:03:52,203 --> 00:03:54,438
I'm gonna undo that back to get axes.
80
80
00:03:54,438 --> 00:03:58,962
Instead of doing that, we can just go and
say let's go and use the horizontal axes.
81
81
00:03:58,962 --> 00:04:02,856
The horizontal axes relates
to the x coordinates.
82
82
00:04:02,856 --> 00:04:07,780
And a negative is going to be negative x,
and a positive is going to be positive x.
83
83
00:04:07,780 --> 00:04:09,820
So basically,
that's gonna be left and right,
84
84
00:04:09,820 --> 00:04:14,020
because we know negative x heads to the
left and positive x heads to the right.
85
85
00:04:14,020 --> 00:04:17,200
And the negative button in this
instance is left on the keyboard and
86
86
00:04:17,200 --> 00:04:18,660
the positive is right.
87
87
00:04:18,660 --> 00:04:21,420
Also there's an alternative
negative button which is AND.
88
88
00:04:21,420 --> 00:04:26,430
So if you wanna use WASD to control
your game, or the cursor arrows,
89
89
00:04:26,430 --> 00:04:29,820
the up, down, left, right,
you can do both of those within here.
90
90
00:04:29,820 --> 00:04:30,790
A couple of other settings,
91
91
00:04:30,790 --> 00:04:32,700
we're not gonna play around with
too much at the moment, gravity,
92
92
00:04:32,700 --> 00:04:35,280
the dead zone, sensitivity, etc.
93
93
00:04:35,280 --> 00:04:37,520
Sometimes a little bit more relevant for
keyboard.
94
94
00:04:37,520 --> 00:04:39,160
The cool thing is if you
scroll down a little bit,
95
95
00:04:39,160 --> 00:04:42,500
you'll see down at the bottom
there's another horizontal.
96
96
00:04:42,500 --> 00:04:46,650
Which is set up for the joystick but what
I reallly wanted to show you, is we can
97
97
00:04:46,650 --> 00:04:50,890
just used one aspect that we print our
code, which we're gonna call horiztonal.
98
98
00:04:50,890 --> 00:04:54,920
And then system knows that all
of these different things can be
99
99
00:04:54,920 --> 00:04:56,940
referring to horiztonal.
100
100
00:04:56,940 --> 00:04:58,160
And if you want to change it,
101
101
00:04:58,160 --> 00:05:01,810
then you can change it here in your
in your inspector nice and easily.
102
102
00:05:01,810 --> 00:05:04,650
Rather than having to go into your code
and find your particular spot, where
103
103
00:05:04,650 --> 00:05:09,000
we're referring to the left key or the
right key or whichever key you're using.
104
104
00:05:09,000 --> 00:05:12,580
So I'm gonna copy horizontal,
back over into my player script.
105
105
00:05:12,580 --> 00:05:18,870
in here, where we say input.getaccess
open parenthesis and do our quotation.
106
106
00:05:18,870 --> 00:05:21,810
And then paste in horizontal,
or you can type it but
107
107
00:05:21,810 --> 00:05:26,970
pasting it means we get it We know we get
it correct for sure, and then semi colon.
108
108
00:05:26,970 --> 00:05:30,170
So what we've created here
is a var delta x, and
109
109
00:05:30,170 --> 00:05:33,053
we have a squiggly line
here at the moment.
110
110
00:05:33,053 --> 00:05:35,360
I'm not happy,
I'll get to that in a second.
111
111
00:05:35,360 --> 00:05:39,385
Input dot GetAxis("Horizontal"),
and the reason we got a red,
112
112
00:05:39,385 --> 00:05:42,785
squiggly line, it's not happy that we're
using it before we've declared it.
113
113
00:05:42,785 --> 00:05:44,175
So we're gonna put this line,
114
114
00:05:44,175 --> 00:05:46,955
I just wanted to do it this way
around from a learning point of view.
115
115
00:05:46,955 --> 00:05:50,885
So you could see this one first, that one
second, but I will now put this up above.
116
116
00:05:50,885 --> 00:05:53,616
So we're declaring what deltaX refers to,
and
117
117
00:05:53,616 --> 00:05:56,289
then we're using that
deltaX in our newXPos.
118
118
00:05:56,289 --> 00:05:59,590
So in other words,
where we want our ship to be.
119
119
00:05:59,590 --> 00:06:04,210
So I mentioned the var within our local
variable here, we sometimes use var.
120
120
00:06:04,210 --> 00:06:07,290
Or we could type in float,
that would be just fine.
121
121
00:06:07,290 --> 00:06:09,330
Because this is of type float,
if I undo it.
122
122
00:06:09,330 --> 00:06:12,720
If you mouse over delta x,
123
123
00:06:12,720 --> 00:06:15,630
it shows you that it's a local
variable with type float.
124
124
00:06:15,630 --> 00:06:21,700
So in instances, in situations where Unity
knows, that our Visual Studio knows that
125
125
00:06:21,700 --> 00:06:26,944
this particular variable, because
what follows it must make it a float.
126
126
00:06:26,944 --> 00:06:29,814
Then it's okay for you to say var and
it knows it's a float,
127
127
00:06:29,814 --> 00:06:31,374
it converts it automatically.
128
128
00:06:31,374 --> 00:06:33,706
So to move, you might know this already.
129
129
00:06:33,706 --> 00:06:37,499
If you know this, pause the video and
jump in and give this a try.
130
130
00:06:39,120 --> 00:06:43,286
Otherwise, we're going to
type in transform.position,
131
131
00:06:43,286 --> 00:06:45,920
you guessed it, equals.
132
132
00:06:45,920 --> 00:06:50,690
Well we need to use our new
keyword to create a Vector2.
133
133
00:06:50,690 --> 00:06:52,510
We're working in Vector2 in this game.
134
134
00:06:52,510 --> 00:06:55,580
You could type Vector3, and
then it would want to know X, Y and Z.
135
135
00:06:55,580 --> 00:06:58,750
But we're not working with Z
in this section of the course.
136
136
00:06:58,750 --> 00:07:00,530
So we're just, Using Vector2.
137
137
00:07:00,530 --> 00:07:01,890
And then what'sthat going to be?
138
138
00:07:01,890 --> 00:07:05,930
Well, it's looking for an x and a y.
139
139
00:07:05,930 --> 00:07:07,490
So float x, float y.
140
140
00:07:07,490 --> 00:07:08,850
What's our float x?
141
141
00:07:08,850 --> 00:07:11,830
Float x is going to be the new x position.
142
142
00:07:11,830 --> 00:07:13,540
So this new x position.
143
143
00:07:13,540 --> 00:07:16,885
And the y for now we're going to say is
144
144
00:07:16,885 --> 00:07:25,100
transform.position.y; to round it off.
145
145
00:07:25,100 --> 00:07:31,190
So we're saying that we want to
get the change, this is the change
146
146
00:07:31,190 --> 00:07:34,713
based upon what the player is gonna be
doing that we're calling each frame.
147
147
00:07:34,713 --> 00:07:39,022
And then that will allow us to say, what's
the new position that we want the ship to
148
148
00:07:39,022 --> 00:07:41,905
go to, which is the current
position plus the change.
149
149
00:07:41,905 --> 00:07:46,197
And then we need to say,
well your position, your actual position,
150
150
00:07:46,197 --> 00:07:48,715
this frame should be this new Vector2.
151
151
00:07:48,715 --> 00:07:52,919
Which is going to be the x,
which is our new position and then the y,
152
152
00:07:52,919 --> 00:07:58,050
transform.position.y, is just saying
stay where you are right now on the y.
153
153
00:07:58,050 --> 00:08:03,340
Okay, now let's save that,
come back over into Unity, click on Play.
154
154
00:08:05,757 --> 00:08:07,380
Not on Pause, Rick, click on Play.
155
155
00:08:07,380 --> 00:08:08,780
I'm having a bit of trouble
with this today, aren't I?
156
156
00:08:08,780 --> 00:08:13,700
Click on Play, okay, and
then left and right, wow, fantastic.
157
157
00:08:13,700 --> 00:08:15,090
No, up and down, just left and right.
158
158
00:08:15,090 --> 00:08:17,890
This is going very fast, so
what do we need to do next?
159
159
00:08:17,890 --> 00:08:21,410
Well, next,
what we need to do is something that,
160
160
00:08:21,410 --> 00:08:23,620
I'm going to introduce a new concept for
you here.
161
161
00:08:23,620 --> 00:08:26,440
And I've got some
documentation to show you.
162
162
00:08:26,440 --> 00:08:28,620
It's going to be time dot delta time.
163
163
00:08:28,620 --> 00:08:30,220
So very quickly I've also
given you a link to input,
164
164
00:08:30,220 --> 00:08:33,400
if you want to know more about input and
all the different properties and
165
165
00:08:33,400 --> 00:08:36,810
all the different methods
that the input class has.
166
166
00:08:36,810 --> 00:08:40,980
Also into the Input Manager you can
read more about that, if you'd like.
167
167
00:08:40,980 --> 00:08:44,670
We're looking at Time.deltaTime, and
I've got a couple of slides on this.
168
168
00:08:44,670 --> 00:08:48,995
But the summary of why use
Time.deltaTime is to make our game
169
169
00:08:48,995 --> 00:08:51,420
frame-rate independent.
170
170
00:08:51,420 --> 00:08:53,010
That means that no matter how fast or
171
171
00:08:53,010 --> 00:08:57,490
slow your computer runs,
you will get the same experience.
172
172
00:08:57,490 --> 00:08:58,930
So let's have a look at
some slides on that.
173
173
00:08:58,930 --> 00:09:03,566
So on Update, which we know is each frame,
so every frame of our game.
174
174
00:09:03,566 --> 00:09:08,690
We move the whatever, in this case
our player ship, 1 unit to the left.
175
175
00:09:08,690 --> 00:09:10,695
That's what we're saying
in this particular example.
176
176
00:09:10,695 --> 00:09:13,357
We're saying, On Update,
move 1 unit to the left.
177
177
00:09:13,357 --> 00:09:17,900
And on a slow computer, for
example, this is just a guess.
178
178
00:09:17,900 --> 00:09:20,590
We might be getting ten frames per second.
179
179
00:09:20,590 --> 00:09:22,947
So the computer is chugging
away saying [NOISE].
180
180
00:09:22,947 --> 00:09:28,150
Each second it manages to calculate
through ten frames worth of computer.
181
181
00:09:28,150 --> 00:09:31,300
On a fast computer, it can calculate
through a lot more frames.
182
182
00:09:31,300 --> 00:09:32,830
It can render more frames.
183
183
00:09:32,830 --> 00:09:34,610
Or it can process what we're
trying to get it to do.
184
184
00:09:34,610 --> 00:09:38,072
So, it might be getting
100 frames per second.
185
185
00:09:38,072 --> 00:09:44,213
The problem varies if we saying on Update
move 1 unit, then on the Slow Computer,
186
186
00:09:44,213 --> 00:09:50,628
that's 1 unit x 10 Frames per second,
the Distance per second will be 10 units.
187
187
00:09:50,628 --> 00:09:55,124
And on the Fast Computer, it's gonna
be 1 unit per frame to the left,
188
188
00:09:55,124 --> 00:09:58,570
x 100 frames per second,
which is 100 frames.
189
189
00:09:58,570 --> 00:10:04,336
So you get a very different experinece on
a slow computer than on a fast computer.
190
190
00:10:04,336 --> 00:10:06,141
Now, what do we do about this?
191
191
00:10:06,141 --> 00:10:10,505
Well, when we look at the duration
of the frames on a slow computer,
192
192
00:10:10,505 --> 00:10:15,120
if we're getting 10 frames per second,
then each frame is 0.1s.
193
193
00:10:15,120 --> 00:10:20,440
10 frames per second, that means each
individual frame is .1 of a second.
194
194
00:10:20,440 --> 00:10:24,040
And our example with the Fast Computer,
if we're getting 100 frames per second,
195
195
00:10:24,040 --> 00:10:28,380
then each frame is taking
.01 second to run.
196
196
00:10:28,380 --> 00:10:31,060
So every point every .01 of
a second there's another frame, and
197
197
00:10:31,060 --> 00:10:32,540
another frame, and another frame.
198
198
00:10:32,540 --> 00:10:38,570
So the distance per second, if we
were to multiply the 1 unit per frame
199
199
00:10:38,570 --> 00:10:44,630
times by the 10 frames per second that
our computer is capabale of running,
200
200
00:10:44,630 --> 00:10:49,780
times by the duration of each frame,
which is 0.1, we get a value of 1.
201
201
00:10:49,780 --> 00:10:54,420
And that's exactly the same if you use
the same calculation one frame to the left
202
202
00:10:54,420 --> 00:10:59,220
per, so one unit per frame
times by 100 frames per second,
203
203
00:10:59,220 --> 00:11:07,700
times by 0.01 that it takes to run
each frame, then we get a value of 1.
204
204
00:11:07,700 --> 00:11:13,830
So you can see formula, gives us
the same result on both computers.
205
205
00:11:13,830 --> 00:11:17,726
So then we can use
Time.deltaTime because using
206
206
00:11:17,726 --> 00:11:22,400
Time.deltaTime Unity can tell us how
long each frame took to execute.
207
207
00:11:22,400 --> 00:11:23,920
We don't need to sit
there with a stopwatch,
208
208
00:11:23,920 --> 00:11:25,330
cuz we don't know how to do that.
209
209
00:11:25,330 --> 00:11:27,120
It'd be impossible to measure the frames.
210
210
00:11:27,120 --> 00:11:31,607
Unity knows exactly how long
the last frame took to execute it.
211
211
00:11:31,607 --> 00:11:34,179
Knows that it's keeping track of that so
we can use that.
212
212
00:11:34,179 --> 00:11:38,807
And so when we multiply something by
Time.deltaTime it makes our game frame
213
213
00:11:38,807 --> 00:11:40,045
rate independent.
214
214
00:11:40,045 --> 00:11:44,534
So if I scroll back up here ,where
the duration of the frame is
215
215
00:11:44,534 --> 00:11:48,162
what we're getting by
using time.delta time.
216
216
00:11:48,162 --> 00:11:53,600
Time.delta time means how long did
it take for that frame to execute?
217
217
00:11:53,600 --> 00:11:55,400
Okay, and
218
218
00:11:55,400 --> 00:11:59,640
so the end result is the game's behaving
the same on fast and slow computers.
219
219
00:11:59,640 --> 00:12:04,270
So back over into our game,
let's go back to play.cs.
220
220
00:12:04,270 --> 00:12:10,722
And to make this super clear,
I'm just going to print, so debug.log,
221
221
00:12:10,722 --> 00:12:16,620
Not quotation marks,
thats if were doing a string, deltaX.
222
222
00:12:16,620 --> 00:12:19,570
So we will just print this variable and
see what it prints out for
223
223
00:12:19,570 --> 00:12:20,850
us when we're moving.
224
224
00:12:20,850 --> 00:12:23,250
So back over into Unity, you don't
need to do this, this is just for
225
225
00:12:23,250 --> 00:12:24,780
illustration purposes.
226
226
00:12:24,780 --> 00:12:30,220
Play the game and then as I move
to the left you can see the value.
227
227
00:12:30,220 --> 00:12:31,290
I'm going to go off the screen.
228
228
00:12:31,290 --> 00:12:33,490
If I hold down to the right,
it goes all the way up to one.
229
229
00:12:33,490 --> 00:12:35,750
Hold down to the left it goes
all the way to minus one.
230
230
00:12:35,750 --> 00:12:38,000
And there's a ramp up in between.
231
231
00:12:38,000 --> 00:12:41,480
Cuz that's what happens, so
when we jump back into our code here.
232
232
00:12:41,480 --> 00:12:45,743
We're saying that as we're pushing
this Horizontal, this value here,
233
233
00:12:45,743 --> 00:12:50,085
this deltaX, we're getting the axis,
it's going from minus 1 up to 1.
234
234
00:12:50,085 --> 00:12:54,280
So we're gonna times this, this is where
we want to be framerate independent.
235
235
00:12:54,280 --> 00:12:57,764
We'll times this by Time.deltaTime.
236
236
00:12:59,120 --> 00:13:01,440
Okay, that is making this
framerate independent.
237
237
00:13:01,440 --> 00:13:04,720
So I'm just going to get rid of this
debug.log, don't need that anymore.
238
238
00:13:06,760 --> 00:13:09,160
Save up, back over into Unity.
239
239
00:13:09,160 --> 00:13:10,940
Now it's going to give us
a very interesting result.
240
240
00:13:10,940 --> 00:13:14,360
Not exactly what we want for
our final game, but it's a good result.
241
241
00:13:14,360 --> 00:13:17,337
So we'll move our ship to the left,
[SOUND] very slow.
242
242
00:13:17,337 --> 00:13:19,420
And to the right, very slow as well.
243
243
00:13:19,420 --> 00:13:23,170
But this would be exactly
the same on every computer.
244
244
00:13:23,170 --> 00:13:27,860
This would be the exact same slowness of
speed, which is great, that's a big win.
245
245
00:13:27,860 --> 00:13:31,470
And the last step we need to do in here
is to get the actual speed that we want.
246
246
00:13:31,470 --> 00:13:33,670
We need to be able to tune it,
cuz that's way too slow.
247
247
00:13:33,670 --> 00:13:35,333
So I'm gonna create myself a new variable.
248
248
00:13:35,333 --> 00:13:40,114
That I will call what SerializeField
first of all, before I call it anything.
249
249
00:13:40,114 --> 00:13:43,729
It's going to be a float and
I'll call this moveSpeed and
250
250
00:13:43,729 --> 00:13:45,847
we will initialize that at 10.
251
251
00:13:45,847 --> 00:13:49,017
And it's going to be a float so
we will be a f on the end of 10.
252
252
00:13:49,017 --> 00:13:54,897
And now down here now in our delta so
what is the change we say times by
253
253
00:13:54,897 --> 00:14:02,050
Time.deltaTime times my moveSpeed,
cuz we're wanting to speed it up.
254
254
00:14:02,050 --> 00:14:04,320
Save, jump back over into Unity,
255
255
00:14:04,320 --> 00:14:09,600
you can see if I click on my Player
after it thinks, here we go.
256
256
00:14:09,600 --> 00:14:11,480
Our move speed here is ten.
257
257
00:14:11,480 --> 00:14:13,890
If I click on play we'll see how
good that feels at the moment.
258
258
00:14:13,890 --> 00:14:15,420
Might have got it right the first go.
259
259
00:14:15,420 --> 00:14:17,460
Left, right, okay that's pretty good.
260
260
00:14:17,460 --> 00:14:19,730
We can tune it a bunch when we've
got more stuff in our game.
261
261
00:14:19,730 --> 00:14:22,200
But for
now that's feeling In the right ballpark.
262
262
00:14:22,200 --> 00:14:26,570
And we can very easily increase
this to 20 or decrease it to 5,
263
263
00:14:26,570 --> 00:14:30,180
if we wanna double or
half the speed of the player.
264
264
00:14:30,180 --> 00:14:32,520
Okay, so that's us moving left and right.
265
265
00:14:32,520 --> 00:14:34,370
We're using Time.deltaTime.
266
266
00:14:34,370 --> 00:14:37,140
We're getting the axis,
which is Horizontal.
267
267
00:14:37,140 --> 00:14:39,070
And I have a challenge for you.
268
268
00:14:41,270 --> 00:14:43,530
The challenge is to
create vertical movement.
269
269
00:14:43,530 --> 00:14:46,520
So allow the player to move up and
down as well as left and right.
270
270
00:14:46,520 --> 00:14:51,290
Basically, by replicating what we did
just now for the horizontal axis.
271
271
00:14:51,290 --> 00:14:55,220
Do the same thing for the vertical axis.
272
272
00:14:55,220 --> 00:14:57,130
Okay, so jump in and
take on that challenge and
273
273
00:14:57,130 --> 00:14:58,120
I'll see you back here in a moment.
274
274
00:15:02,410 --> 00:15:04,590
Okay, so
we're gonna keep this really simple.
275
275
00:15:04,590 --> 00:15:08,590
I'm just going to highlight
my first two lines, copy, and
276
276
00:15:08,590 --> 00:15:12,400
then paste them underneath
the previous two lines.
277
277
00:15:13,740 --> 00:15:18,560
And go through and
change the reference to from X to Y,
278
278
00:15:18,560 --> 00:15:22,644
and from our axis of
Horizontal to Vertical.
279
279
00:15:22,644 --> 00:15:28,655
And then for our new X position
is actually our new Y position.
280
280
00:15:28,655 --> 00:15:35,274
And then transform.position.y and deltaY.
281
281
00:15:35,274 --> 00:15:41,730
And then the last step here
in our transform.position,
282
282
00:15:41,730 --> 00:15:47,550
the new Vector2 is newXPos,
newY Pos, okay.
283
283
00:15:47,550 --> 00:15:50,480
So let's give it a nice and simple,
just we've got the same thing for
284
284
00:15:50,480 --> 00:15:52,880
horizontal that we're now doing for
the vertical.
285
285
00:15:52,880 --> 00:15:54,690
Back over into Unity, click in play.
286
286
00:15:54,690 --> 00:15:57,430
If all's gone to plan we can fly up,
down, left,
287
287
00:15:57,430 --> 00:16:00,270
right Okay, up down, left, right, cool.
288
288
00:16:00,270 --> 00:16:02,390
So we've got movement in our game,
the player can move around.
289
289
00:16:02,390 --> 00:16:03,250
I can go off the screen, so
290
290
00:16:03,250 --> 00:16:05,190
that's something we'll need
to resolve pretty soon.
291
291
00:16:05,190 --> 00:16:07,050
But we do have movement.
292
292
00:16:07,050 --> 00:16:10,510
And this is framerate independent,
which is very, very excellent.
293
293
00:16:10,510 --> 00:16:15,410
It also means you can plug in a joystick,
and you
294
294
00:16:15,410 --> 00:16:19,610
could do exactly the same thing, without
having to go and write additional code.
295
295
00:16:19,610 --> 00:16:23,917
It just knows immediately, that horizontal
and vertical maps to your joysticks or
296
296
00:16:23,917 --> 00:16:24,663
your joypad.
297
297
00:16:24,663 --> 00:16:27,764
So great work, well done and
I'll see you in the next video.
30098
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.