1
00:00:03,760 --> 00:00:09,190
In this lecture, we're going to get our player moving around the screen using the new unity input system.

2
00:00:10,000 --> 00:00:10,450
Okay.

3
00:00:10,450 --> 00:00:15,200
So in the last lecture, we imported our unity input system via the package manager.

4
00:00:15,220 --> 00:00:20,440
So go ahead and just make sure that's fully installed and up to date and then we can go ahead and close

5
00:00:20,440 --> 00:00:21,670
our package manager.

6
00:00:21,700 --> 00:00:26,680
Now to get the input system working, we need to attach it as a component to a game object.

7
00:00:26,680 --> 00:00:31,930
So let's go ahead and find our player in our prefabs folder and then add a new component.

8
00:00:31,960 --> 00:00:37,750
The input system can be found under input or we can go ahead and just search for our player input system.

9
00:00:38,590 --> 00:00:42,550
And once we've attached this component, we then need to do a bit of set up.

10
00:00:42,550 --> 00:00:46,450
So don't worry too much about all these extra boxes and things or we need to worry about.

11
00:00:46,450 --> 00:00:49,090
At the moment it's just creating some actions.

12
00:00:49,300 --> 00:00:54,700
So let's click on Create Actions and this will allow us to create a new input action asset.

13
00:00:55,090 --> 00:00:58,300
Let's give it a name and I'm going to go with input actions for now.

14
00:00:59,880 --> 00:01:03,060
Go ahead and hit save and then open our root assets folder.

15
00:01:03,060 --> 00:01:05,269
We'll have this new input actions.

16
00:01:05,280 --> 00:01:09,000
We've also got a warning error coming up down the bottom, but don't worry about that.

17
00:01:09,000 --> 00:01:11,100
We can just clear that off when we need to.

18
00:01:11,430 --> 00:01:17,250
So let's go ahead and open up our new input actions asset and this will bring up the asset window.

19
00:01:17,370 --> 00:01:21,660
Now, there's quite a lot going on in this screen, so let's just break it down one by one.

20
00:01:22,050 --> 00:01:24,330
First of all, this is a new asset window.

21
00:01:24,330 --> 00:01:31,080
And unlike when we change things in the inspector, if we make changes in here, they won't save automatically.

22
00:01:31,080 --> 00:01:36,000
So at the very top we can see we've got to save asset button in case we change anything.

23
00:01:36,240 --> 00:01:39,000
And then over on the right we have an auto save option.

24
00:01:39,000 --> 00:01:44,070
So if you're like me and prone to forgetting that you've changed things in your asset files, then you

25
00:01:44,070 --> 00:01:46,560
can always go ahead and enable the auto save.

26
00:01:46,560 --> 00:01:50,490
I'm not going to worry about that for now because we're not actually going to be making any changes

27
00:01:50,490 --> 00:01:51,150
for the moment.

28
00:01:51,150 --> 00:01:54,450
We're just going to have a look around and get a feel for how things work.

29
00:01:54,450 --> 00:01:58,770
So the main bulk of this asset window is broken into three sections.

30
00:01:58,770 --> 00:02:04,080
We have our action maps on the left, our actions in the middle, and then the properties over on the

31
00:02:04,080 --> 00:02:04,770
right.

32
00:02:05,280 --> 00:02:07,860
So let's start by looking at the action maps.

33
00:02:08,070 --> 00:02:14,400
You can see that by default we've been given a player action map and a UI action map and all an action

34
00:02:14,400 --> 00:02:17,790
map really is it's just a collection of related behaviors.

35
00:02:18,060 --> 00:02:23,400
So under our player we have the actions of move, look and fire.

36
00:02:23,430 --> 00:02:28,680
And if we look under UI, then we have things like navigate, point, click, submit and cancel.

37
00:02:28,680 --> 00:02:35,280
So you can create new action maps and this allows you to reuse the same key bindings for different types

38
00:02:35,280 --> 00:02:36,060
of gameplay.

39
00:02:36,060 --> 00:02:40,920
So you might imagine that in some games you'll have your player action map where you can run around

40
00:02:40,920 --> 00:02:42,000
and shoot at things.

41
00:02:42,000 --> 00:02:46,680
Then you may have a separate action map for driving or flying or something else.

42
00:02:47,100 --> 00:02:53,130
Talking of key bindings, let's next look at the actions section of this screen and the actions contained

43
00:02:53,130 --> 00:02:54,570
within each action map.

44
00:02:54,600 --> 00:03:00,270
Essentially take the button inputs or the physical inputs from a device and ties it to an action that

45
00:03:00,270 --> 00:03:02,040
we want to perform in the game.

46
00:03:02,130 --> 00:03:08,550
So if we roll down the move action so we can see the actual key bindings themselves, we can see we've

47
00:03:08,550 --> 00:03:09,950
got a whole bunch in here.

48
00:03:09,950 --> 00:03:14,910
So we've got ones for the gamepads, for the keyboard and mouse and joysticks and things like that.

49
00:03:15,030 --> 00:03:21,030
And if we roll down the WASD bindings for MOVE, we can see the specific key bindings where we've got

50
00:03:21,030 --> 00:03:25,980
the ways and keys and also the up, down, left and right arrows.

51
00:03:26,340 --> 00:03:27,810
And now we've clicked on one of these.

52
00:03:27,810 --> 00:03:33,660
Let's have a look over at the properties and we can see that our waist is considered a composite type

53
00:03:33,660 --> 00:03:35,100
of a 2D vector.

54
00:03:35,100 --> 00:03:38,190
So it will have an X component and a y component.

55
00:03:38,310 --> 00:03:42,780
And this x and Y component is then further broken down for each key.

56
00:03:42,810 --> 00:03:50,370
So the W handles the up part of that composite to DX vector SX controls the down, a is left and D is

57
00:03:50,370 --> 00:03:51,000
right.

58
00:03:51,570 --> 00:03:55,290
And if we wanted to, we could always change these around to whatever we want.

59
00:03:55,290 --> 00:03:58,020
We can also change the key binding itself.

60
00:03:58,020 --> 00:04:03,840
So if we didn't want to use D as our down key, we can go ahead and click on this dropdown and find

61
00:04:03,840 --> 00:04:05,520
an input from this list.

62
00:04:05,520 --> 00:04:11,730
Alternatively, if you have the control device plugged in, we can also go ahead and hit listen and

63
00:04:11,730 --> 00:04:15,810
then you just have to press whatever button you want to find that in the input list.

64
00:04:15,810 --> 00:04:20,640
So I'm going to keep this as D so if I press D on the keyboard, we can see that it comes up in our

65
00:04:20,640 --> 00:04:21,269
list.

66
00:04:21,480 --> 00:04:26,820
Now, like I said, we're not actually going to be changing anything in this input actions asset for

67
00:04:26,820 --> 00:04:27,390
now.

68
00:04:27,420 --> 00:04:32,580
We will of course come back to this later on when we come to start shooting at our enemies because at

69
00:04:32,580 --> 00:04:38,310
that point we may want to either remap our fire buttons that are automatically assigned or potentially

70
00:04:38,310 --> 00:04:40,650
add additional firing actions.

71
00:04:40,650 --> 00:04:46,140
I just want to give you a brief overview for now so that we can start setting things up in our code.

72
00:04:46,500 --> 00:04:51,690
So rather than saving this asset, I'm just going to go ahead and close it down and then back on our

73
00:04:51,690 --> 00:04:52,950
input actions.

74
00:04:53,040 --> 00:04:56,670
We've got another piece of set up before we go through this other stuff here.

75
00:04:57,060 --> 00:05:02,730
Let's go ahead and drag over the input actions asset into this actions field and it's all open up a

76
00:05:02,730 --> 00:05:04,650
whole bunch of new options.

77
00:05:04,650 --> 00:05:09,750
And the great thing about this is that if we hover over any of these particular options, we'll get

78
00:05:09,750 --> 00:05:12,420
a nice tooltip to explain what it does.

79
00:05:12,660 --> 00:05:15,810
For the moment, we're just going to leave everything as default.

80
00:05:15,810 --> 00:05:19,890
And the only thing I really want to cover is this behaviors list at the bottom.

81
00:05:19,920 --> 00:05:23,460
It's currently on send message and we're actually going to leave it like that.

82
00:05:23,460 --> 00:05:29,160
But when you look at Unity documentation, they tend to be written in terms of using the Unity Events

83
00:05:29,160 --> 00:05:29,850
option.

84
00:05:29,850 --> 00:05:35,040
This is a slightly more complicated setup, but it is more flexible and makes full use of the new input

85
00:05:35,040 --> 00:05:35,670
system.

86
00:05:35,670 --> 00:05:40,170
For now though, while we're just learning about this, let's keep it on send message because that keeps

87
00:05:40,170 --> 00:05:41,520
it nice and simple.

88
00:05:41,520 --> 00:05:47,280
And in this box below, send message will see all of the messages that our input system will be sending

89
00:05:47,280 --> 00:05:47,880
out.

90
00:05:47,880 --> 00:05:50,880
These can then be intercepted in our code.

91
00:05:51,000 --> 00:05:55,710
So let's just have a brief aside and discuss how send messages work.

92
00:05:55,710 --> 00:05:59,280
Well, you can think of it like our input system shouting out to any other.

93
00:05:59,360 --> 00:06:01,370
Components that are on the same object.

94
00:06:01,370 --> 00:06:03,020
There's something has happened.

95
00:06:03,050 --> 00:06:08,870
So the general chain of command will be that the player presses a button on their keyboard, so imagine

96
00:06:08,870 --> 00:06:10,880
they press the W on their keyboard.

97
00:06:10,910 --> 00:06:16,730
This is then registered as being the move action from the input system, and the input system will send

98
00:06:16,730 --> 00:06:20,960
the message to all the attached components that on move has happened.

99
00:06:21,350 --> 00:06:25,000
So to intercept those messages, we're going to need a new script.

100
00:06:25,010 --> 00:06:29,750
But before we do that, there's one final piece of setup that we need to do to get our player input

101
00:06:29,750 --> 00:06:30,410
working.

102
00:06:30,410 --> 00:06:36,650
So if we click on open input settings and then in the input system's package menu, all we need to do

103
00:06:36,650 --> 00:06:38,840
is create our settings asset.

104
00:06:38,870 --> 00:06:42,830
This thing gets everything set up for us and we're not going to worry too much about what all these

105
00:06:42,830 --> 00:06:44,120
options do for now.

106
00:06:44,120 --> 00:06:49,370
So let's just go ahead and close our project settings and we'll see that we've now got a new script

107
00:06:49,610 --> 00:06:51,410
object in our assets folder.

108
00:06:51,650 --> 00:06:56,810
With that all set up though, now let's create our new script that will intercept the messages being

109
00:06:56,810 --> 00:06:58,210
sent by the player input.

110
00:06:58,220 --> 00:07:02,870
So let's add a new component and create a new script called Player.

111
00:07:04,540 --> 00:07:06,610
And then we can go ahead and open that up.

112
00:07:07,030 --> 00:07:12,250
Let's start the script off right by removing our start method, because we're not going to need it.

113
00:07:12,250 --> 00:07:15,400
And we're just clear up this note above our update method.

114
00:07:15,680 --> 00:07:19,610
And now let's look at how we're going to intercept that on move message.

115
00:07:19,630 --> 00:07:24,550
First of all, we're going to need to go right to the top of the script and add a new using statement.

116
00:07:24,790 --> 00:07:28,990
And we're going to be using the unity engine dot input system.

117
00:07:29,530 --> 00:07:37,270
Then to intercept that move message, we need a method with the same name so we can write void on move.

118
00:07:37,270 --> 00:07:41,980
And you can kind of think of this like how the unity on trigger enter functions work and things like

119
00:07:41,980 --> 00:07:42,370
that.

120
00:07:42,370 --> 00:07:47,890
So unity will explicitly recognize this method and we won't have to manually call it from elsewhere

121
00:07:47,890 --> 00:07:49,960
in our code, in the parentheses.

122
00:07:49,960 --> 00:07:54,820
We're going to need to accept an input value and we'll just call this the value.

123
00:07:55,480 --> 00:07:59,560
And if you remember, our move action was a type of vector too.

124
00:07:59,560 --> 00:08:07,390
So we can get our value by saying value dot get and then in chevrons or angle brackets we can write

125
00:08:07,390 --> 00:08:08,620
a vector to.

126
00:08:09,400 --> 00:08:11,950
Let's also store this variable for later use.

127
00:08:11,950 --> 00:08:20,620
So I'm going to create a vector too called raw input and then in on move we can just set raw input to

128
00:08:20,620 --> 00:08:21,250
our value.

129
00:08:21,280 --> 00:08:22,750
Don't get vector two.

130
00:08:22,960 --> 00:08:28,990
Now to show how this works, let's go ahead and write a debug log statement and we're just going to

131
00:08:28,990 --> 00:08:35,620
output the raw input if we now save our script and jump back over into unity, let's go ahead and clear

132
00:08:35,620 --> 00:08:37,299
our console and hit play.

133
00:08:38,070 --> 00:08:43,590
If I press the key on my keyboard, we'll see that we get a single debug log message.

134
00:08:43,590 --> 00:08:48,130
And even though I'm still holding down the key, we're not getting any more messages.

135
00:08:48,150 --> 00:08:53,730
However, when I lift my finger off the key, we get another debug log message to say that things have

136
00:08:53,730 --> 00:08:54,900
been reset for.

137
00:08:54,900 --> 00:08:56,970
I then do the same with the key.

138
00:08:57,000 --> 00:09:00,600
I get a single message release and get a reset message.

139
00:09:00,600 --> 00:09:06,930
So our on move method is only being called when we press a key down and when it gets lifted off.

140
00:09:06,960 --> 00:09:12,690
This is great for things like button presses and mouse clicks, but we're going to want continuous movement.

141
00:09:12,690 --> 00:09:16,490
So when we hold down a key, we continue to move in the given direction.

142
00:09:16,500 --> 00:09:19,360
So how do we achieve this continuous movement?

143
00:09:19,380 --> 00:09:25,680
Well, if we come out of play and jump back over into our script, this is the reason why I stored our

144
00:09:25,680 --> 00:09:31,320
raw input in a separate variable, because we can then use that variable to continuously update our

145
00:09:31,320 --> 00:09:33,360
position in the update method.

146
00:09:33,510 --> 00:09:38,850
So in our update method, let's create a new vector three variable called delta.

147
00:09:38,880 --> 00:09:44,250
And this is going to store the delta position for our movement, which we can then apply to our transform

148
00:09:44,430 --> 00:09:45,260
position.

149
00:09:45,270 --> 00:09:52,530
So for now, let's just set this equal to our raw input and then we can set our transform position to

150
00:09:52,530 --> 00:09:54,300
plus equals our delta.

151
00:09:55,080 --> 00:10:01,110
If we go ahead and save this and jump back into unity to test it out, I'm going to go back to our same

152
00:10:01,110 --> 00:10:03,090
view and then hit play.

153
00:10:04,380 --> 00:10:06,200
And we can now move around.

154
00:10:06,210 --> 00:10:12,810
But our movement is incredibly fast because we're moving one unit, every single frame.

155
00:10:12,990 --> 00:10:15,630
And this is going to bring us onto your challenge.

156
00:10:16,020 --> 00:10:19,860
I want you to fix our player movement to make it more controllable.

157
00:10:19,890 --> 00:10:25,380
Start by adding a move speed variable to our script and we're going to want to be able to change this

158
00:10:25,380 --> 00:10:26,610
in the inspector as well.

159
00:10:26,610 --> 00:10:27,990
So keep that in mind.

160
00:10:28,230 --> 00:10:32,170
And then I want you to make the movement frame rate independent.

161
00:10:32,190 --> 00:10:37,650
We've looked at all of this in previous sections, so pause the video now and give that a go.

162
00:10:42,760 --> 00:10:43,240
Okay.

163
00:10:43,240 --> 00:10:44,060
Welcome back.

164
00:10:44,080 --> 00:10:48,070
So hopefully that was some good revision for you and you got on okay with that challenge.

165
00:10:48,070 --> 00:10:51,090
So let's start by setting up a movement speed variable.

166
00:10:51,100 --> 00:10:56,950
We said we wanted to change this in the inspector, so let's make it a serialized build and we'll use

167
00:10:56,950 --> 00:10:59,620
a float for this and call it the move speed.

168
00:11:00,160 --> 00:11:05,860
While we're here, we may as well set it up with a default value and I think five might be a good starting

169
00:11:05,860 --> 00:11:06,550
value for this.

170
00:11:06,550 --> 00:11:07,870
So let's give that a try.

171
00:11:08,140 --> 00:11:15,400
And then down in our update method, let's amend our delta value to be the raw input multiplied by the

172
00:11:15,400 --> 00:11:19,720
move, speed multiplied by time dot delta time.

173
00:11:19,720 --> 00:11:23,560
And remember Delta time is the time the last frame took to render.

174
00:11:23,560 --> 00:11:29,380
So multiplying by that will just help smooth things out and make our movement frame rate independent.

175
00:11:29,530 --> 00:11:34,780
Now, before we go ahead and save our script, the last thing I want to do in here is just to refactor

176
00:11:34,780 --> 00:11:37,000
this out of my update method.

177
00:11:37,000 --> 00:11:43,270
So I'm going to extract the method by pressing control and the period key in Visual Studio and extract

178
00:11:43,270 --> 00:11:43,960
method.

179
00:11:44,050 --> 00:11:47,590
I'm going to get rid of the private declaration because I don't really like it there.

180
00:11:47,590 --> 00:11:52,390
I like to keep things tidy and I'm going to rename my method to move.

181
00:11:52,810 --> 00:11:58,120
Now, if we go ahead and save this and jump back over into unity, let's go ahead and hit play.

182
00:11:59,470 --> 00:12:01,870
And we can now move around nicely.

183
00:12:01,900 --> 00:12:07,060
Now we are a little bit slow, so we may want to speed that up later on, but we've got a much bigger

184
00:12:07,060 --> 00:12:12,040
problem at the moment in that we can completely leave the game screen and fly where we like.

185
00:12:12,220 --> 00:12:15,790
So we've done quite a lot of work in this lecture to get our player moving.

186
00:12:15,790 --> 00:12:17,230
So we're going to stop things there.

187
00:12:17,230 --> 00:12:22,230
And in the next lecture we're going to look at confining our player to the boundaries of our screen.

188
00:12:22,240 --> 00:12:23,590
So I'll see you there.

