In this lecture, we're going to set up our new input system so that when we push left or right or up
or down, we're printing to the screen the -1 to 1 on the x and y axis.
For our controller input, I'm going to jump over to Package Manager.
So making sure I'm on Unity Registry, I'm going to search for input system.
If you don't have it installed already, then click install.
That will give you this fancy warning click.
Yes, we're okay with all that stuff and that will cause unity to reboot, restart itself and go through
a setup process.
Now with the input system set up first step, click on player, add component input player inputs.
What we're looking for here, why have we got all this noise going on?
It feels like there's a lot of words on my screen at the moment.
Let me just shuffle things around a little bit.
Okay.
So the first thing we need to do here is create actions.
Click on that.
That will ask, where would you like to save it?
I don't have a photo for this at the moment.
So new folder, I'm going to put this into input system.
I think so.
Nifty place for that double click on the folder.
Save.
We'll get an error.
That's okay.
That'll go away shortly.
Let's have a look and see if we can find where that end up.
So we have our input system.
There we go.
We've got our tail.
Vania, I didn't do a very good job of renaming it, did I?
I just clicked enter.
But Tony is okay.
It could probably rename it input actions, but I'm just going to have the one here so it doesn't matter
so much.
Back over to the player now.
We need to make sure we drag that over into the action so it knows what we are actually doing.
I'm going to double click on that to open it up to see what's going on here.
The action is already set up.
Move, look and fire.
We're going to be using move a bunch in the next couple of videos.
And then after that, we'll also be wanting to to be able to use jump.
We have a fire in here.
I'm not going to use fire for jumping because it's well, you know, fire is fire and jump is jump.
We might wish to do fire at some point later on.
So let's set ourselves up a jump action.
I'm going to click on the plus button here.
I'm going to add in the word jump and you can see it's of action type button.
That's what we want.
We don't want to have a value for our move.
Let's see here we have a value which is a vector two on the left, right up here, down here, which
is X and Y.
But here, jump.
That's cool.
It's a button.
Let's change the binding to let's see here on the keyboard and mouse scheme, I'm going to click on
the pull down selector thing here.
So one thing that's really cool, you can click on the listen button.
It's not literally listening for what you're saying spacebar.
It's not quite like that.
It's listening to the keyboard input.
So if I tap on the keyboard, it will show me that I just push space and I have space as the option.
So I'll add that there.
It's also let's see if we go next to jump and add another one, add a binding by clicking the plus button,
click on the binding.
We will make this the gamepad set up.
We'll click on our little select thing here.
Listen, and I don't have my controller turned on, but if I did have it turned on, then it would also
listen for that.
But I'm going to find GamePad and then just find buttons south.
That's the button down the bottom of the four buttons on our gamepad.
So now we've set up jump, which we'll use later on in this section when we're setting up jump for the
character.
Okay, we can close that out.
We don't need it anymore.
Back over to Player.
I need to also open input settings and create a settings asset and that should be good to go with all
the settings as they are in there.
Now we have this input system settings asset that can sit down there and do what it's going to do.
And you'll see we're using send messages and here are all the messages that are available to us, very
similar to the way we've used on Trigger Enter or we've used on collision.
These are the same sorts of things you can see on move is in there already and because we've created
Jump, it's sitting here as an on jump.
Now just to illustrate this super, super clearly if I was to double click on my actually while I'm
here, I'm just going to grab my input system and pop it into the input system folder.
So there we go.
That was the input system.
Dot asset.
No, yes, dot asset.
It is great.
I'm going to open up my input actions again and input actions in here.
If I was to go and create a new action and I called this donkey, blah, blah, why donkey?
Well, you tend not to have donkey anywhere in your programming, in your coding.
So if you search for donkey, you'll be able to find this pretty easily.
It's a Ben Tristram thing.
Thank you, Ben, for giving me that habit of using Donkey as your word to search for.
Now, when I click back on here in Behavior, you can see that we have on donkey blah blah that is now
an option that if we had that bound to something in here.
So currently it's not bound to anything.
But if it were to bind that to something, then when that particular binding is executed, when a button
is pushed or a keyboard key is pressed, then it will send the message that we want it to send over
and here to send the on donkey blah blah message.
And then we can set up whatever code we want within that.
So I just wanted to illustrate for me, when I was first playing around with this, I was thinking,
how do I, how do I access jump if there's no on jump?
But it gets created for you when you create that new action, which is pretty nifty.
Let me just very quickly get rid of Donkey Delete, right click delete just so it's not lingering around
there.
Okay.
Now the other step is to create our script.
So I'm going to click on assets, give ourselves a little bit more space here.
It's getting running out of space, isn't it?
These animated windows.
We need a scripts folder.
So right click, create a C-sharp script.
Oh, I did that in the wrong order.
Okay, well, fine.
My brain's not quite working.
We'll make this.
Player movement script.
Okay.
And now we'll create the photo create folder.
I was too hasty with my clicking, wasn't I?
And I'll call this scripts.
Scripts and drag my play and movement into scripts.
It's a little bit of back to front with my order there.
We'll find the player and I'm going to drag over the player movement script onto the player.
While I'm here, I've been working in the instance, so I need to make sure I jump up to the prefab
under overrides, apply all and then the player input and the player movement script will be applied
to the prefab.
Now let's open up our player movement script.
We'll just do one or two very quick things here to get ourselves set up and ready.
And then in the next lecture we'll actually be making him move.
But for now, let's just remove our comments.
And because we're using the new input system, we need to first of all access the namespace using unity
engine dot input system semicolon.
Now the cool thing we're trying to do here is within update.
We want to say move the character, make the character run based upon what input the player is pressing
and how do we know what the player is pressing?
Well, as I pointed out before, we've got a message over in here, message, which is hopefully you
can see that.
Okay, on, move on.
Move is going to be called when the player changes what they're what they're doing on the keyboard or
keypad based upon our bindings.
So if you're pushing left or pushing right or if you let go of pushing left or let go of pushing right.
But because it's a message, it's saying the player push down left, there's a change, the player let
go of the left arrow.
That's a change.
The player pushed right on the gamepad.
So we need to grab that input at the moment that it happens and then store it into a variable that we
can then use within update to say on every single frame, whatever is currently the setting or currently
the input value, then continue to use that to update our player's action.
Okay.
So that's a bit of a wordy explanation.
Hopefully it helps a little bit.
I'm going to start with a vector too, because we need the X and the Y.
We're looking at the character movement and I'm going to call this move input and this corresponds to
whether you're pushing right or left up or down.
We want to store it in to move input.
How do we find what move input is?
Well, we can put some information in our on move method.
That's a message and is going to be getting cold when the player makes a change to to the key binding
that we've got.
And we need to get some information when that is called.
And the information we need is an input value.
We need a particular value and we'll just name this value.
So the type is input value.
We're giving it the name of value, and then we want to store our value that we're receiving from the
input and store it in our vector too that we can use throughout our class in any of our different methods
within update, etc..
So the way we're going to do that is very simply move input equals value, dot get.
And then what do we need to get here?
We need to get the vector to parentheses semicolon so there'll be a particular value and we want to
assign that value to our move input variable.
Now there's the last step in here.
Let's check that this is working.
I'm going to just do a debug log and what do you think?
We're going to log out.
We're going to log out, move input and save that up back over into unity.
Let's see how that worked.
We'll see if it did or didn't work.
Clear some errors here.
There's always going to be something that Rick has done wrong.
Method must have a return type.
Oh, Rick.
Oh, Rick, what have you done here?
We need to have void in front.
My apologies.
If you were yelling at the screen while I was doing that a little bit rusty today, not paying attention.
That should clear things up.
Okay, we'll click on play back into our game and let's see if that's going to work.
Left Arrow Yeah, right.
Arrow Cool.
So it's showing that there's some change when we lift up or push down, lift up, push down.
As we've done in previous lectures, we now have our input for up, down and left, right, and in the
next lecture we can get our character running left and right.
So good stuff.
I'll see you in the next lecture.