1
00:00:03,040 --> 00:00:07,210
In this lecture, we're going to be changing things so that our player can push forward, move forward

2
00:00:07,210 --> 00:00:12,280
backwards, move backwards and rotate left and right using the left and right arrow keys.

3
00:00:12,280 --> 00:00:14,650
So let's add input into our game.

4
00:00:14,680 --> 00:00:16,960
Let's start by understanding what an input system is.

5
00:00:16,960 --> 00:00:20,530
So an input system is a way of converting the player's physical action.

6
00:00:20,530 --> 00:00:25,900
So in other words, a button press or a key press into information for the game so that it can understand

7
00:00:25,900 --> 00:00:27,370
it and know what to do with it.

8
00:00:27,370 --> 00:00:30,790
Unity is used a few different input systems over the years.

9
00:00:30,790 --> 00:00:36,550
They're constantly evolving and improving, and currently in the version of unity that I'm using, there

10
00:00:36,550 --> 00:00:41,260
is an old system and a new system, and it's actually called the old system and the new system.

11
00:00:41,260 --> 00:00:45,520
We're going to use both of these systems in this course, and I'm going to, in this current project,

12
00:00:45,520 --> 00:00:50,740
use the old system because I personally think the old system is a lot easier just to understand and

13
00:00:50,740 --> 00:00:53,470
to get started with and say, let's just make a thing happen.

14
00:00:53,470 --> 00:00:57,400
But we will use the new system later on in this course.

15
00:00:57,440 --> 00:00:59,110
We'll start that process of having a look at it.

16
00:00:59,110 --> 00:01:04,150
If you go to edit and then find project settings, you'll see a whole lot of settings in here.

17
00:01:04,150 --> 00:01:06,430
One of these is the input manager.

18
00:01:06,430 --> 00:01:08,380
You might have this closed up on your axes.

19
00:01:08,380 --> 00:01:14,980
So input manager axes and axes is the plural for the word axis, which is basically meaning from one

20
00:01:14,980 --> 00:01:17,050
side of something to another side of something.

21
00:01:17,050 --> 00:01:24,790
So that's an axis going from say 0 to 1 or minus one to plus one and all the steps along in between.

22
00:01:24,790 --> 00:01:30,040
So the axes that we've got in here, a whole bunch that are already within unity, one is horizontal

23
00:01:30,040 --> 00:01:31,570
and another is vertical.

24
00:01:31,570 --> 00:01:37,990
So horizontal is going to be a way for us to push left or right and have our vehicle go left or right.

25
00:01:37,990 --> 00:01:44,110
Or you could also use a and so if you look at left and right, the arrows on your keyboard or and is

26
00:01:44,110 --> 00:01:50,710
also correspond to if you say using the WASD controls as you do in a first person shooter, the A would

27
00:01:50,710 --> 00:01:52,420
be left and DX would be to the right.

28
00:01:52,420 --> 00:01:56,590
And there's a couple of other settings that come along with this, but mostly I'm going to leave those

29
00:01:56,590 --> 00:02:01,150
as the default, just knowing that we need to know the name horizontal and we know that that's going

30
00:02:01,150 --> 00:02:03,760
to correspond to left and right and also vertical.

31
00:02:03,760 --> 00:02:10,479
Similarly, we need to know the name vertical and that will correspond to up and down on the keyboard.

32
00:02:10,720 --> 00:02:16,900
S And W interestingly, if you scroll down, you'll see that there's also another horizontal and vertical

33
00:02:16,900 --> 00:02:18,700
roundabout here, horizontal.

34
00:02:18,700 --> 00:02:24,190
And this is going to correspond to using a joystick or a game pad where you can do similarly.

35
00:02:24,190 --> 00:02:28,030
You can move up on the joystick or down on the joystick and likewise with vertical.

36
00:02:28,030 --> 00:02:34,570
So it's already set up within unity that you can use keyboard and mouse as well as joystick or joy pad

37
00:02:34,570 --> 00:02:35,710
to be out of control.

38
00:02:35,710 --> 00:02:39,490
We're just going to worry about keyboard for the moment, just because I know that not everyone has

39
00:02:39,490 --> 00:02:41,410
a joystick that they have access to.

40
00:02:41,440 --> 00:02:46,750
Now, the one nasty thing of using the old system is that we need to get the name exact because we're

41
00:02:46,750 --> 00:02:48,970
going to use a string reference and I'll show you that in the moment.

42
00:02:48,970 --> 00:02:52,870
But just remember, if you have any issues, it might be because you don't have the name typed out.

43
00:02:52,870 --> 00:02:53,500
Exactly.

44
00:02:53,500 --> 00:02:58,210
And the last thing I'll mention in here is that our horizontal and our vertical axes are going to go

45
00:02:58,210 --> 00:03:00,940
from minus one to plus one.

46
00:03:00,940 --> 00:03:03,490
So from minus one through zero to plus one.

47
00:03:03,490 --> 00:03:08,290
So within rotate, we've currently got stair speed, which is cool, but I want to replace that or I

48
00:03:08,290 --> 00:03:14,080
want to add to that a way that we can access our input, access our horizontal input.

49
00:03:14,080 --> 00:03:17,890
So within our update method, this is going to be a little bit messy for a moment, but we'll tidy it

50
00:03:17,890 --> 00:03:18,610
up later on.

51
00:03:18,610 --> 00:03:27,100
I'm going to add myself a new variable which will be of type float and I'll call this steer amount equals.

52
00:03:27,100 --> 00:03:31,300
And you know, we'll finish this in a moment because I first want to touch on the fact that, well,

53
00:03:31,300 --> 00:03:36,430
we've got a variable down here in our update method, whereas before we put our variables outside of

54
00:03:36,430 --> 00:03:38,500
the update method, what's going on with that?

55
00:03:38,500 --> 00:03:40,450
Well, the there's a few reasons.

56
00:03:40,450 --> 00:03:46,270
The one important reason at the moment is I need to be able to calculate this steer amount every single

57
00:03:46,270 --> 00:03:49,870
frame because the player is going left and then going right and then doing nothing left and right and

58
00:03:49,870 --> 00:03:50,560
left and right.

59
00:03:50,560 --> 00:03:53,290
So I need to have that within update that we're calculating.

60
00:03:53,290 --> 00:03:54,700
What is the sticker amount?

61
00:03:54,700 --> 00:03:59,800
How much of my steering every single frame, whereas these other variables I only need to be knowing

62
00:03:59,830 --> 00:04:04,810
what they are at the start when I click on play and I'm not changing them every single frame, so I

63
00:04:04,810 --> 00:04:06,580
don't need to put them down in updates.

64
00:04:06,580 --> 00:04:10,270
So that's one of the reasons why we've put our variable down in here an update because it needs to be

65
00:04:10,270 --> 00:04:11,650
updating and changing.

66
00:04:11,650 --> 00:04:13,420
Okay, so sticker amount.

67
00:04:13,420 --> 00:04:16,390
And then here I'm going to call a method.

68
00:04:16,390 --> 00:04:17,620
Remember we're calling methods here.

69
00:04:17,620 --> 00:04:20,200
When we say transform, rotate, that's calling a method.

70
00:04:20,200 --> 00:04:22,089
I'm going to access input.

71
00:04:22,089 --> 00:04:24,550
And this is a class of things to do with input.

72
00:04:24,550 --> 00:04:28,480
So input dot, what do we have access to within the input class?

73
00:04:28,480 --> 00:04:33,460
Well, a bunch of things in here, one of which is going to be get access.

74
00:04:33,490 --> 00:04:35,800
Get access is going to get a particular access.

75
00:04:35,800 --> 00:04:41,020
Which access while we add our parentheses and here's what we put in a string reference and string references

76
00:04:41,020 --> 00:04:45,850
are a bit yucky because if you spell them wrong, it doesn't work and it often doesn't tell you why

77
00:04:45,850 --> 00:04:46,420
it didn't work.

78
00:04:46,420 --> 00:04:47,800
So we need to be careful here.

79
00:04:47,800 --> 00:04:53,950
We're steering, so we need to add horizontal and make sure you type it exactly as I have done there

80
00:04:53,950 --> 00:04:57,070
or you can jump back over into your edit project settings.

81
00:04:57,070 --> 00:05:02,470
Fine horizontal double click on that control, see for copy and then back over in.

82
00:05:02,590 --> 00:05:05,770
To your code and paste it straight up in here.

83
00:05:05,770 --> 00:05:11,080
So that's the way that you can make sure you don't mess it up and then semicolon down the end as a starting

84
00:05:11,080 --> 00:05:11,320
point.

85
00:05:11,320 --> 00:05:12,610
We're going to add to this in a moment.

86
00:05:12,610 --> 00:05:17,470
So instead of stair speed, I'm going to add in here the amount and see what happens.

87
00:05:17,470 --> 00:05:18,550
We'll update this in a moment.

88
00:05:18,550 --> 00:05:23,110
I just want to start so we've got steer amount, which is basically just, hey, what's the player pushing

89
00:05:23,110 --> 00:05:28,960
from minus one to plus one on the left and right arrows will save that jump back over into unity.

90
00:05:28,960 --> 00:05:30,130
Click on play.

91
00:05:30,130 --> 00:05:33,670
Now, we should have the first controls within our game.

92
00:05:33,940 --> 00:05:36,760
Click on play left and right.

93
00:05:36,760 --> 00:05:38,680
Well, first thing I notice is it's back to front.

94
00:05:38,680 --> 00:05:42,070
It's backwards, but it's actually working, working, working, working.

95
00:05:42,070 --> 00:05:43,300
Lovely and it's not too bad.

96
00:05:43,300 --> 00:05:46,330
So I'm going to get out of the play mode, jump back over into here.

97
00:05:46,330 --> 00:05:47,980
I want it the other way around.

98
00:05:47,980 --> 00:05:52,000
So instead of having steer amount, I'm just going to say negative steer amount.

99
00:05:52,000 --> 00:05:54,820
Actually, I'll grab my pen tool and just quickly draw that on the screen.

100
00:05:54,820 --> 00:05:55,810
So that makes sense here.

101
00:05:55,810 --> 00:05:57,730
So we're talking about steer amount.

102
00:05:57,730 --> 00:06:03,130
And if we have a value of minus one which is saying left and plus one which is to the right, zero is

103
00:06:03,130 --> 00:06:03,910
doing nothing.

104
00:06:03,910 --> 00:06:06,700
So this is left arrow and right arrow.

105
00:06:06,700 --> 00:06:09,850
If we push down the left arrow, we're going to get a value of minus one.

106
00:06:09,850 --> 00:06:12,850
But currently that's making us rotate in the wrong direction.

107
00:06:12,850 --> 00:06:19,660
So just basic maths is if we say minus, minus one, that actually equals plus one, minus minus one

108
00:06:19,660 --> 00:06:20,440
equals plus one.

109
00:06:20,440 --> 00:06:27,010
And over here, if we just say add a minus to this, then minus plus one is going to equal minus one.

110
00:06:27,010 --> 00:06:29,320
So it's one of those rules that I think many of us know.

111
00:06:29,320 --> 00:06:34,360
But if you try to subtract a negative, then what you end up doing is actually editing, adding.

112
00:06:34,360 --> 00:06:43,930
So if I try to say, for example, zero take away minus one ends up actually saying zero plus one,

113
00:06:43,930 --> 00:06:48,700
because when you take away a minus, you end up adding and if that doesn't quite click for you, then

114
00:06:48,700 --> 00:06:53,140
you can just look at it from the point of view of saying Adding a minus will say, do the other thing,

115
00:06:53,140 --> 00:06:54,070
do the opposite.

116
00:06:54,070 --> 00:06:57,910
So instead of doing a minus, we'll do a plus instead of plus or minus.

117
00:06:57,910 --> 00:07:00,130
That's another way you can look at it since it's doing this.

118
00:07:00,130 --> 00:07:04,180
So we're doing that stellar amount is now heading in the direction we want it to head in, which is

119
00:07:04,180 --> 00:07:04,750
cool.

120
00:07:04,750 --> 00:07:09,100
However, we've lost a little bit of our control in terms of how fast we turn.

121
00:07:09,100 --> 00:07:14,110
So what I'm going to do in here, my input, get access, I'm going to multiply.

122
00:07:14,110 --> 00:07:21,730
So we use this asterisks which is above eight on your keyboard, multiply by steer speed and now whatever

123
00:07:21,730 --> 00:07:25,150
we put in steers speed, which is currently one, but we might have changed it.

124
00:07:25,150 --> 00:07:31,900
Now Inspector is going to be multiplied to the horizontal value and we're going to be applying that

125
00:07:31,900 --> 00:07:38,800
as our rotation value in this z part of our rotate method.

126
00:07:38,800 --> 00:07:40,540
Now jump back over into unity.

127
00:07:40,540 --> 00:07:43,420
So click on play and make sure I'm clicked into my game window.

128
00:07:43,420 --> 00:07:48,130
When I hit left and hit right, it's going the same speed it was going before because I've just added

129
00:07:48,130 --> 00:07:50,860
one into my steer speed.

130
00:07:50,860 --> 00:07:55,270
If I pop in here say 0.5, click on save, click on play.

131
00:07:55,300 --> 00:07:59,200
Now have a look at what we're going to do in here, but stop moving left.

132
00:07:59,200 --> 00:08:00,820
Okay, we're turning more slowly.

133
00:08:00,820 --> 00:08:07,390
So I now have the ability to read in the player's input on the left and the right, and also by multiplying

134
00:08:07,390 --> 00:08:12,700
that by 0.5 or the steer speed, I can change how fast my player is turning.

135
00:08:12,700 --> 00:08:16,120
I put that up to say three and then click back in my game window.

136
00:08:16,120 --> 00:08:21,010
Look, I'm a little bit off the screen here and then you rotate in a way that's super crazy and fast.

137
00:08:21,010 --> 00:08:24,730
Now, what you might have noticed there, I didn't mention this before, but if you're in play mode

138
00:08:24,730 --> 00:08:29,920
and you make a change to any of these variables, the values to the variable, then when you click out

139
00:08:29,920 --> 00:08:33,070
of play mode, it will return back to what it was before.

140
00:08:33,070 --> 00:08:36,730
So if you make some cool changes, then you want to remember it or write it down.

141
00:08:36,730 --> 00:08:42,490
Or you can also click on right click and copy and then you can paste it back into the value that you

142
00:08:42,490 --> 00:08:43,120
had before.

143
00:08:43,120 --> 00:08:48,670
So this is what we've got for our rotate it steer amount and we're using the horizontal times by steer

144
00:08:48,670 --> 00:08:49,240
speed.

145
00:08:49,240 --> 00:08:54,280
Your speed might be a little bit different to mine, and that is because of the speed with which your

146
00:08:54,280 --> 00:08:56,920
computer runs in order to get the same result.

147
00:08:56,920 --> 00:09:01,420
We're going to cover that in the next lecture, but for now, we'll leave it as that nice basic formula

148
00:09:01,420 --> 00:09:02,050
just there.

149
00:09:02,050 --> 00:09:06,250
The other thing we need to do is to add the same sort of logic for movement.

150
00:09:06,250 --> 00:09:09,610
So we're taking in the vertical axis and that's going to be your challenge.

151
00:09:09,610 --> 00:09:15,100
So use the vertical axis to allow the player to drive forward and backwards using the arrow keys.

152
00:09:15,100 --> 00:09:16,060
Just a little bit of a hint.

153
00:09:16,060 --> 00:09:17,890
You can add it as a line just here.

154
00:09:17,890 --> 00:09:23,260
Whatever new line you're going to add, you can add it just underneath the sticker amount and there's

155
00:09:23,260 --> 00:09:23,740
your challenge.

156
00:09:23,740 --> 00:09:24,340
Pause video.

157
00:09:24,340 --> 00:09:24,880
Take that on.

158
00:09:24,880 --> 00:09:25,990
See you back here when you're done.

159
00:09:28,100 --> 00:09:32,810
So hopefully that made sense because we're going to do a very similar thing as what we just did to our

160
00:09:32,810 --> 00:09:35,240
sticker amount we're going to do for our move amount.

161
00:09:35,240 --> 00:09:44,340
So float and then we'll call it move amount equals input, get access, parentheses and then vertical.

162
00:09:44,360 --> 00:09:47,360
Make sure you spell it exactly like that or else you'll get some issues.

163
00:09:47,360 --> 00:09:49,670
And then we're going to multiply it by what?

164
00:09:49,700 --> 00:09:55,970
We'll multiply it by move speed, and then we need to, instead of using move speed, we want to use

165
00:09:55,970 --> 00:09:58,610
move amount in here.

166
00:09:58,640 --> 00:09:59,260
Cool.

167
00:09:59,270 --> 00:10:00,460
We'll save that up.

168
00:10:00,470 --> 00:10:02,280
Jump back over into unity.

169
00:10:02,300 --> 00:10:04,520
I want to make sure that I can move forwards and backwards.

170
00:10:04,520 --> 00:10:06,180
This move amount multiplier.

171
00:10:06,200 --> 00:10:08,300
That'd be interesting to see if that is enough.

172
00:10:08,300 --> 00:10:09,530
So click on play.

173
00:10:09,560 --> 00:10:12,020
It shouldn't go anywhere when I click on play.

174
00:10:12,350 --> 00:10:13,090
Okay, good.

175
00:10:13,100 --> 00:10:15,230
Now when I push forward, it goes forward, backwards, backwards.

176
00:10:15,230 --> 00:10:17,690
And can I steer and move at the same time?

177
00:10:17,690 --> 00:10:18,910
Indeed I can.

178
00:10:18,920 --> 00:10:20,060
Wonderful.

179
00:10:20,060 --> 00:10:20,450
Excellent.

180
00:10:20,450 --> 00:10:21,050
So there we go.

181
00:10:21,050 --> 00:10:24,670
We've added input so our player can drive their car.

182
00:10:24,680 --> 00:10:25,880
I'll see you in the next lecture.

