What is Unity's New Action Based input system? And why should you use it? And how does it work
with VR? Well, in this video, I'm gonna show you guys how to use the new input system with just
a keyboard at first, and then we're going to add VR and we'll show you guys how easy it
is to use VR and the new action based input system together and why it's definitely the
preferred way of doing input systems. So Unity released this new input system package back in
2018, as a preview package, but now as of 2019, it is fully implemented into unity and can be
installed from the package manager, the new action based input is different than the old device based
input in two very big ways. First off, it is now event based, so the event only fires when certain
buttons are performed. And you don't have to continuously check for updates to the value in the
update call. So it's much more efficient. And then it's also action based, which is like the main
part of it. And that means that you don't have to write code for every single controller that
you want to use in order for your game to support that particular controller, which makes it super
easy for your players to reconfigure their button inputs and easy for you to port your game to PC
and to console and anything else you want to do. So let's jump into the computer. And
I'll show you how this works. We're in a brand new project, I'm using 20 20.2. This will work
for any version of Unity from 20 19.4, or any of the later ones. So first, I'm just going to set
up a really default scene with a ground plane and a cube just so that we can implement some
movement in here. And I'm not even going to add any VR stuff just yet. We're just gonna do basic
3d Character Movement. And then I'll add in some of the VR stuff and show you guys how to
use specifically VR input with this new system. In order to use this, you'll need to go into Window
Package Manager. And then in your unity registry, we're going to look for input system right here,
install that and it's going to ask if you want to go ahead and use this system as the default going
forward in the project, we're gonna hit yes, this is going to restart your project. So now
if we go into Edit Project Settings player and then scroll down, we should see that the
act of input handling is now set to input system package new as opposed to the old one,
or you can select both if you want to have both kinds of controls, but the old one is
obsolete and it's going to get removed at some point. So we're using the new system.
Now instead of just being able to access the controls through the code by calling gamepad or
input or something like that, we're going to need to create an input actions object. So
down here at the bottom input actions, and this can just be called default, controls.
Double Click to open that up, and it'll bring up an action map. And first thing we want to do
is check this auto save checkbox. And then in here is where you're going to map all of the different
properties that you want, we're going to call this map default. This is where all the different
button layouts can go. So if you want like the default layout, and then you want the bumper
thumb or whatever the different layouts are, and then it'll give us an action. And then the action
has an action type. And we're going to set up move, so we want a value. And we want that value
to be a vector to two dimensional vector. And then inside of this action, double click on it,
call this move it automatically give us a binding in this, but we actually want to remove the
binding. And we're gonna go in here and add a 2d vector composite. And that'll give us a 2d vector.
And we're going to call this WASD. And then for the app, we want to go to keyboard, the W key down
there we go, we set up W ASD and then we can also add another one if we want and call this
arrow keys. So we've set up the arrow keys and then WASD. But now instead of having to call each
different key press for that action we want to do we can just call the move in and go ahead and give
us a 2d vector of all the keys that are being inputted to give a move action. And then if we
wanted to make another action, for instance, let's do a jump action, make sure this is set to button.
And then the path can be spacebar.
Now if we want to have our character jump, we just call jump and I'll show you guys how the code
works. And since we have auto saved checked, all this is going to be saved so we can just exit out.
Now from here. There's two different ways you can go one is a default player input company unit that
unity already has set up. All right, let's get this cube moving, we're going to rename this to
player, just so nobody gets confused mainly me on the player, we're going to add a character
Controller component so that we can move it. And then we're going to add a player input
component and this player input. And we'll go and look for the actions and the actions
of the default controls that we set up. And then instead of behavior being send messages,
which is what it is default, we're going to switch to invoke unity events. And that will give us this
events drop down. And then inside of default, we have our move, and we have our jump actions that
we set up inside of the input actions. And then inside of here, we can put any method we want to
be called anytime that the movement is updated, or anytime the jump is updated,
anything like that. So to do that, we're going to create a new component. And this
one is going to be called player movement. And this is going to be a script. So let's go into
that. And then to start off, we want to go ahead and grab that character controller.
And then grab it from the start. And then we're also going to require it. Now we're also going to
want a movement vector that tells us the direction the player wants the character to move. So this is
going to be a v3, we'll just call it move vector. And then we also want a speed that the character
can move, this is going to be serialized so we can change it. So be a float speed, speed,
speed. And we're gonna set this to something like 10. And now every time the move action happens,
we want to update this move vector, we're gonna create a new method, void,
on, movements changed. And then this is going to take a input action dot callback context.
This will be underlined, so you can either hover over it, and click on this little light bulb,
and you'll get an option or you can click on it and press Control period. And that'll bring up
your options to fix as well. And we're just going to include this USING statement up at the top.
And now we can take this context and update our move vector. And so from the context,
we're going to get a two dimensional vector, and then we're going to
convert that to a three dimensional move vector. So we're going to say direction
is equal to context dot read value vector two, because that's the input value that we get. And
then we're going to take the direction converted into a vector three and put it into move vector.
So here, we just go direction dot x, then a zero for the Y and then direction, dot y for the Z
axis of the three dimensional vector. Okay, so we have the vector being changed, but the character
is not actually going to be moving at all at this point, it's just the direction that it
wants to move will change, but it won't actually be updating or moving. So we need one more call
in here. And this is going to be character dot move, this move isn't our input action, it's the
character controller, it's a native method to the character controller, and it will actually
just move the character controller wherever it's different than our input action move. I
named that poorly. But inside of here, we're going to say we want the mu vector times the speed times
the time delta time. And we can actually do fixed delta time in here. That might be a little better.
Change that to fixed update as well make sure to save this and then one more thing
this on movement change will need to be public. So we can call it from the event.
Let's go back to Unity. And then inside of player go to player input,
events, default, and then move, hit that plus option and then drag the player into it. And
then the function we want is player movement. And then you should see this on movement changed
method that we created. And now every time move is updated, the on movement change will be updated,
and then the character will be constantly updating the movement. So let's try this
and see what happens. So now when I hit W, A, S and D, we move and then also the arrow keys
will move and we can add joysticks we can add any kind of controller input that we want to move
and we'll just list those into the action items and it's super easy to add and remove
and customize what the player input is going to be. So this is a whole lot easier than the other
device database system. Now there's also a player input manager. And this is used for split screen.
And you can do multiplayer stuff with this, but it's out of the scope of this video and
project. So we're going to remove that. And we're actually going to remove the player input now,
and I'm going to show you guys how to do all of this just from the code. And it's really easy,
actually, we're only changing a couple of lines here. So we still want to have on movement change
to be called every time movement is updated. But in order to get that movement updated,
we're going to want to have a input action asset, we're gonna call this one player controls.
And then we want an input action. And this one's going to be called movement.
And then just inside the start method, we're going to call a new variable called
Game Play Action map. And this is going to equal the player controls,
find action map, and player, actually, this is gonna be called default. So this, whatever this
text is here should line up with, instead of default controllers, this action map, so
this is called default. So inside of the script, this needs to be default. And now movement can
equal the gameplay action map, find action move. And this lines up with whatever this action
text is. So that's how you get the movement. And then that's how you get the action map.
So now we want to subscribe to some of the events that happen when movement is changed or
performed or canceled or anything like that. So we're going to call movement dot performed.
And then plus equals will subscribe, a method to whenever movement performed
happens on movement changed. So this syntax is a little weird here movement performed.
So every time the movement happens, anytime a key is pressed, update the movement action,
this means we're going to subscribe the method afterwards. But then you don't add
the parentheses in here, which is kind of odd little weird, but you have a method here. And
then this plus equals means we're adding it to the list of subscriptions that are already engaged
with the movement performed. And then I'm going to press Ctrl D to duplicate that and change this to
cancelled as well. So every time the button goes down, and every time the button comes up,
we want to call this on movement changed. And then just for good measure, we want it to be
enabled just to make sure that you can actually indeed, move. So just to recap really quick,
we're grabbing the action map based on this text here. And then inside of the map, here, we have
a list of actions. And so we're going to grab the move action. And then for the move action,
we can subscribe to certain events that happen. So whenever it's performed, and whenever it's
canceled, we're going to call the en movement changed method, and it will automatically pass
in this variable and do the same thing we did with the player input, then we also need to serialize
the input action asset so that we can define where those actions are coming from. So now inside
of player controls, we're going to drag this default controls in there. And now we can run it.
And we get movement. And it's all scripted in the backend, the C sharp, which makes it
super easy to like this can be changed private. Now we can have a lot more control over when
these actions occur. So like only when the button is pressed down and not when it's lifted up, or
any other options that we might have with movement, we can easily update which action map
is being used for all this and all that kind of stuff. So using the code is much more modular,
and I think easier in my head to keep up with now the last question is how to use this and
VR. Does it still work all that so yeah, so yeah, it does work, we can go to Edit Project Settings,
go to package manager and enable preview packages. And then over in window package manager,
we can scroll all the way to the bottom, go to XR Interaction Toolkit and install that as well
as the open XR plugin. And you'll notice if you download open XR plugin, without the input system
installed, it will automatically update you to the new input system. And then from here, you do the
basic Edit Project Settings XR plugin, check the ones you want. And now we can go to XR,
add a room scaling, you'll notice that says action based and action based means it's using the new
input system and then this device based tab means they're all using the old system. So you want
to use one of these new system rigs. So let's say we want to just move this cube we're just
gonna stand there and then we're moving the cube from the perspective of being on the ground and
we just want to move the cube With the joystick of the one of the controllers, how would we go about
doing that? Well, we could easily go back into our assets, this Move controller, and then we're going
to add a binding instead of a 2d vector composite 2d vector composites are keyboards and that kind
of stuff. bindings are more for joysticks, we can go to XR controller XR controller,
let's just do optional controls joystick. So now any controller with a joystick that you have
attached to this project or going on this project will automatically be able to move the cube.
Let's go back in here, hit play. And now we can move this little cube around with the joystick.
And that's actually kind of fun. There he go. As you can see, using any kind of XR controller
with a new action based input system is super simple. It behaves just like any other gamepad or
joystick or anything like that, you just add in the controller input in the action that you want.
And there you go. And you can go a little bit deeper into this if you want. There's an XR
Interaction Toolkit that is in preview right now. And it goes pretty well with open XR,
and I'm going to end up doing a more in depth tutorial about that later. But as of right now,
you guys can just use this for your games, and it works perfectly fine. I'm super excited about the
next couple of videos that I'm making. The next one is how to get into VR as far as what kind of
VR headsets do you need? What kind of specs does your computer need? How much is it going to cost
for you to get into VR, that kind of stuff. And then the video after that is how to make physics
based hands. So the last video we made your hands can just flip through walls and all that kind of
stuff. And this next video, we're going to begin making a realistic type hand that has physics.
And then in the next videos, we're going to build up from the physics hand to a procedural grab and
then a full body with inverse kinematics. The goal is to ultimately get you a Player Prefab
that is built specifically for XR that you can just drag into your project and you don't have
to do any setting up it's already set up and that it's going to be super nice. I'm really
excited for this. So if you're into this kind of stuff and you're excited about the next videos,
hit that subscribe button and I look forward to seeing you in the next one.