In this lecture, we're going to be changing things so that our player can push forward, move forward
backwards, move backwards and rotate left and right using the left and right arrow keys.
So let's add input into our game.
Let's start by understanding what an input system is.
So an input system is a way of converting the player's physical action.
So in other words, a button press or a key press into information for the game so that it can understand
it and know what to do with it.
Unity is used a few different input systems over the years.
They're constantly evolving and improving, and currently in the version of unity that I'm using, there
is an old system and a new system, and it's actually called the old system and the new system.
We're going to use both of these systems in this course, and I'm going to, in this current project,
use the old system because I personally think the old system is a lot easier just to understand and
to get started with and say, let's just make a thing happen.
But we will use the new system later on in this course.
We'll start that process of having a look at it.
If you go to edit and then find project settings, you'll see a whole lot of settings in here.
One of these is the input manager.
You might have this closed up on your axes.
So input manager axes and axes is the plural for the word axis, which is basically meaning from one
side of something to another side of something.
So that's an axis going from say 0 to 1 or minus one to plus one and all the steps along in between.
So the axes that we've got in here, a whole bunch that are already within unity, one is horizontal
and another is vertical.
So horizontal is going to be a way for us to push left or right and have our vehicle go left or right.
Or you could also use a and so if you look at left and right, the arrows on your keyboard or and is
also correspond to if you say using the WASD controls as you do in a first person shooter, the A would
be left and DX would be to the right.
And there's a couple of other settings that come along with this, but mostly I'm going to leave those
as the default, just knowing that we need to know the name horizontal and we know that that's going
to correspond to left and right and also vertical.
Similarly, we need to know the name vertical and that will correspond to up and down on the keyboard.
S And W interestingly, if you scroll down, you'll see that there's also another horizontal and vertical
roundabout here, horizontal.
And this is going to correspond to using a joystick or a game pad where you can do similarly.
You can move up on the joystick or down on the joystick and likewise with vertical.
So it's already set up within unity that you can use keyboard and mouse as well as joystick or joy pad
to be out of control.
We're just going to worry about keyboard for the moment, just because I know that not everyone has
a joystick that they have access to.
Now, the one nasty thing of using the old system is that we need to get the name exact because we're
going to use a string reference and I'll show you that in the moment.
But just remember, if you have any issues, it might be because you don't have the name typed out.
Exactly.
And the last thing I'll mention in here is that our horizontal and our vertical axes are going to go
from minus one to plus one.
So from minus one through zero to plus one.
So within rotate, we've currently got stair speed, which is cool, but I want to replace that or I
want to add to that a way that we can access our input, access our horizontal input.
So within our update method, this is going to be a little bit messy for a moment, but we'll tidy it
up later on.
I'm going to add myself a new variable which will be of type float and I'll call this steer amount equals.
And you know, we'll finish this in a moment because I first want to touch on the fact that, well,
we've got a variable down here in our update method, whereas before we put our variables outside of
the update method, what's going on with that?
Well, the there's a few reasons.
The one important reason at the moment is I need to be able to calculate this steer amount every single
frame because the player is going left and then going right and then doing nothing left and right and
left and right.
So I need to have that within update that we're calculating.
What is the sticker amount?
How much of my steering every single frame, whereas these other variables I only need to be knowing
what they are at the start when I click on play and I'm not changing them every single frame, so I
don't need to put them down in updates.
So that's one of the reasons why we've put our variable down in here an update because it needs to be
updating and changing.
Okay, so sticker amount.
And then here I'm going to call a method.
Remember we're calling methods here.
When we say transform, rotate, that's calling a method.
I'm going to access input.
And this is a class of things to do with input.
So input dot, what do we have access to within the input class?
Well, a bunch of things in here, one of which is going to be get access.
Get access is going to get a particular access.
Which access while we add our parentheses and here's what we put in a string reference and string references
are a bit yucky because if you spell them wrong, it doesn't work and it often doesn't tell you why
it didn't work.
So we need to be careful here.
We're steering, so we need to add horizontal and make sure you type it exactly as I have done there
or you can jump back over into your edit project settings.
Fine horizontal double click on that control, see for copy and then back over in.
To your code and paste it straight up in here.
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
point.
We're going to add to this in a moment.
So instead of stair speed, I'm going to add in here the amount and see what happens.
We'll update this in a moment.
I just want to start so we've got steer amount, which is basically just, hey, what's the player pushing
from minus one to plus one on the left and right arrows will save that jump back over into unity.
Click on play.
Now, we should have the first controls within our game.
Click on play left and right.
Well, first thing I notice is it's back to front.
It's backwards, but it's actually working, working, working, working.
Lovely and it's not too bad.
So I'm going to get out of the play mode, jump back over into here.
I want it the other way around.
So instead of having steer amount, I'm just going to say negative steer amount.
Actually, I'll grab my pen tool and just quickly draw that on the screen.
So that makes sense here.
So we're talking about steer amount.
And if we have a value of minus one which is saying left and plus one which is to the right, zero is
doing nothing.
So this is left arrow and right arrow.
If we push down the left arrow, we're going to get a value of minus one.
But currently that's making us rotate in the wrong direction.
So just basic maths is if we say minus, minus one, that actually equals plus one, minus minus one
equals plus one.
And over here, if we just say add a minus to this, then minus plus one is going to equal minus one.
So it's one of those rules that I think many of us know.
But if you try to subtract a negative, then what you end up doing is actually editing, adding.
So if I try to say, for example, zero take away minus one ends up actually saying zero plus one,
because when you take away a minus, you end up adding and if that doesn't quite click for you, then
you can just look at it from the point of view of saying Adding a minus will say, do the other thing,
do the opposite.
So instead of doing a minus, we'll do a plus instead of plus or minus.
That's another way you can look at it since it's doing this.
So we're doing that stellar amount is now heading in the direction we want it to head in, which is
cool.
However, we've lost a little bit of our control in terms of how fast we turn.
So what I'm going to do in here, my input, get access, I'm going to multiply.
So we use this asterisks which is above eight on your keyboard, multiply by steer speed and now whatever
we put in steers speed, which is currently one, but we might have changed it.
Now Inspector is going to be multiplied to the horizontal value and we're going to be applying that
as our rotation value in this z part of our rotate method.
Now jump back over into unity.
So click on play and make sure I'm clicked into my game window.
When I hit left and hit right, it's going the same speed it was going before because I've just added
one into my steer speed.
If I pop in here say 0.5, click on save, click on play.
Now have a look at what we're going to do in here, but stop moving left.
Okay, we're turning more slowly.
So I now have the ability to read in the player's input on the left and the right, and also by multiplying
that by 0.5 or the steer speed, I can change how fast my player is turning.
I put that up to say three and then click back in my game window.
Look, I'm a little bit off the screen here and then you rotate in a way that's super crazy and fast.
Now, what you might have noticed there, I didn't mention this before, but if you're in play mode
and you make a change to any of these variables, the values to the variable, then when you click out
of play mode, it will return back to what it was before.
So if you make some cool changes, then you want to remember it or write it down.
Or you can also click on right click and copy and then you can paste it back into the value that you
had before.
So this is what we've got for our rotate it steer amount and we're using the horizontal times by steer
speed.
Your speed might be a little bit different to mine, and that is because of the speed with which your
computer runs in order to get the same result.
We're going to cover that in the next lecture, but for now, we'll leave it as that nice basic formula
just there.
The other thing we need to do is to add the same sort of logic for movement.
So we're taking in the vertical axis and that's going to be your challenge.
So use the vertical axis to allow the player to drive forward and backwards using the arrow keys.
Just a little bit of a hint.
You can add it as a line just here.
Whatever new line you're going to add, you can add it just underneath the sticker amount and there's
your challenge.
Pause video.
Take that on.
See you back here when you're done.
So hopefully that made sense because we're going to do a very similar thing as what we just did to our
sticker amount we're going to do for our move amount.
So float and then we'll call it move amount equals input, get access, parentheses and then vertical.
Make sure you spell it exactly like that or else you'll get some issues.
And then we're going to multiply it by what?
We'll multiply it by move speed, and then we need to, instead of using move speed, we want to use
move amount in here.
Cool.
We'll save that up.
Jump back over into unity.
I want to make sure that I can move forwards and backwards.
This move amount multiplier.
That'd be interesting to see if that is enough.
So click on play.
It shouldn't go anywhere when I click on play.
Okay, good.
Now when I push forward, it goes forward, backwards, backwards.
And can I steer and move at the same time?
Indeed I can.
Wonderful.
Excellent.
So there we go.
We've added input so our player can drive their car.
I'll see you in the next lecture.