Unity's New Input System for Beginners - Unity Tutorial (+BONUS: how it works in VR)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 going to 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 to 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 2020.2 this will work for any version  of unity from 2019.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 gonna 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 gonna 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 gonna restart your project so now if we go   into edit project settings player and then scroll  down we should see that the active 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 kind 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 the first thing   we want to do is check this auto save check  box 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 two two dimensional vector and  then inside of this action double click on it   call this move it'll 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 up we want to go to keyboard the w key down there we go we set up WASD 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 and it'll 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 so 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 save 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 component 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 it'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's 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 this will be a float speed-- "spleed"--   float speed and we're going to set this to  something like 10 and now every time the   move action happens we want to update this move  vector we're going to create a new method void   on movement 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 ctrl 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   it's equal to context dot read value vector  2 because that's the input value that we get   and then we're going to take the direction convert  it into a vector 3 and put it into move vector so here we just go direction dot x and 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's   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 move vector times the speed   times the time dot 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 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 based 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 gonna 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 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 going  to 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 gameplay action map and this is going to equal  the player controls find action map and player   actually this is going to be called default so  this whatever this text is here should line up   with inside of default controllers this action  map so this is called default so inside 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.performed" and then plus equals we'll  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 to  update the movement action this means we're gonna   subscribe the method afterwards but then you don't  add the parentheses in here which is kind of odd   a 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  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 on  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 back end 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 do you use this in 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 OpenXR plugin   and you'll notice if you download OpenXR  plugin without the input system installed it   will automatically update you to the new input  system and then from here you know you do the   basic edit project settings XR plugin check  the ones you want and now we can go to XR   add a room scale and you'll notice that says  action based and action based means it's using   the new input system and then this device base 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 going   to 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 you go  as you can see using any kind of xr controller   with the 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 OpenXR   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 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 clip 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 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
Info
Channel: Justin P Barnett - VR Game Dev
Views: 9,686
Rating: undefined out of 5
Keywords: Justin P Barnett, unity xr, unity new input system, xr interaction toolkit, unity vr tutorial, indie game devlog, devlog, beginner, new input system unity, unity input system, input system unity, new input system, input system, unity input, new input system unity 2020, game dev, c#, input manager unity, input manager, gamedev, unity input manager tutorial, unity tutorial, unity input system tutorial, unity input system 2021, vr unity tutorial, unity new input system tutorial
Id: SAij67HrgAM
Channel Id: undefined
Length: 17min 29sec (1049 seconds)
Published: Tue Feb 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.