New Input System! - 2D Platformer Player Controller - Part 20

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up guys welcome to barton my name is heinrich and today we're going to start taking our player controller to the next level by using a finite state machine instead of the single script that we've been using so far here's a demo of what we're going to be making throughout the next couple of episodes we're essentially going to rebuild the player from scratch i'll reuse a lot of concepts from the previous episodes now before we start building the finite state machine we need to talk about unity's new input system this is not going to be an in-depth tutorial on the new input system but more so a quick start guide that should contain all the information you need to get started okay let's begin okay so the first thing we're going to do before we start making any changes to our project is make sure we are on a new branch within our git repository that way we keep our project safe we can then go ahead and upgrade our project 2019.4 if you're not using that yet and then we should be ready to get started okay so the first thing we need to do is come up to window package manager and then install the new input system once everything loads just go ahead and search for input click on input system as you can see it's currently in version 1.0.0 and just click install it's going to give you a warning that it's basically asking if you want to change the input system from the old one to the new one if you click yes unity will restart and the old input system will no longer be working for you but we can make it so that both work at the same time if that's what you want so i'm going to go ahead and click yes this will restart unity and there we go everything should be installed now so we can go ahead and close out of the package manager now if you still want to be using some of the old input system just while you're busy migrating your project over to the new input system you can come up to edit project settings and then go to the player tab scroll down until you see active input handling as you can see it's currently set to input system package which is the new input system if you click on that you can change it back to use the old input system or you can select both i'm going to leave it on new as i plan on only using the new input system going forward so now we can go ahead and go to our project files come to our scripts folder player folder and then let's create a new folder that we're just going to call input you can go ahead and open that up now let's really talk a little bit about unity's new input system the new input system is a action-based system in the documentation they define action as a logical input such as jump or fire that is an input action that a player can trigger through one or more input devices and then it runs a piece of game logic so the idea is that we define actions our code does things based off of those actions and our input devices trigger those actions and what makes this really cool is we can set up different key bindings for different devices that all trigger the same action so it's easy to swap between keyboard and mouse and controller so we'll get into that in just a second so to start off with using the new input system we have to create an input action asset to do that in the input folder just right click go create and it should be around the bottom of this list here is a input actions go ahead and click that and let's just call that player we're calling it player because we're going to use all of these input actions for our player go ahead and double click on that to open it up and this opens up a little window where we can now define all of our actions we can drag this window and dock it wherever we want so i'm just going to dock it next to game and now let's get started so the first thing we need to do is create an action map now an action map is a collection of assets so for example let's create a new action map and i'm going to call it gameplay like that my reasoning for calling it gameplay is because i can create another action map that i can for instance call dialogue and so gameplay will hold all of my actions for example with regards to movement and fighting and my dialogue action map can hold all of the input associated with dialogue so clicking enter to continue to the next line etc so now that we've created an action map just to the right of it you can see we have actions when you create an action map it comes with a default new action if you click on the action to the right we can see the properties of said action we first of all have the action type which is currently set to button and if we click the little drop down you can see we have three different options value button and pass through so we use value when we want to track continuous changes in the state of a control so for example using wasd for movement input or the left stick on the controller button we'll use more so when we want something to get triggered so like clicking to attack we'll get into using both of these in just a second our last option is pass-through which is very similar to value of some slight differences we're not going to be using that today you can go read the documentation if you want to understand more about what it is so let's just go ahead and rename this action to movement like i said for movement the action type we're going to be using is value so go ahead and set action type to value and that gives us the option to set a control type click the drop down for control type and set it to vector 2. so the value we're going to be passing through with this action is a vector 2. we now need to go and define our bindings for this action so what keys or controller input do we want to be linked to this action so to do that under the action on this little plus sign to the right of it click that and we can now add a binding so first let's go ahead and add the keyboard bindings and for that we need to click add a 2d vector composite so this is going to be one binding that has four different keys this means it's going to build us a vector two based on these four different keys let's just go ahead and rename this binding to wasd and now we can specify what key we want to be associated of up down left and right to do that just click on the key and then back in the properties you now see we have an option for a path the path is the input we want to use for this binding so we can go ahead and click this drop down and here we have all the different input types we can use we can also go ahead and search for an input but the handy little feature that we have is this listen button we can now just use any of those keys and it'll pick up what key we're pushing so currently we're looking at up so let's press w as you can see we're getting two options w on the keyboard or any key on the keyboard we want to choose w let's go ahead and do the same for down so listen s choose that one left is going to be a oops and then right is d there we go and now we have our bindings set up for keyboard now let's also do one for controller if you have a controller i'm just going to go ahead and close up wasd now right above wasd you can see that movement came with a default binding as well so let's just go ahead and click on that and then under path listen if you have your controller plugged in you can again give the input so i'm going to move my left stick and you can see we again come up with two options we have left stick gamepad and left stick xbox controller now it's giving the xbox controller option because that's what i'm using but seeing as many gamepads are so similar we can go ahead and choose the gamepad option this guarantees that for any gamepad that we use the leftmost stick will be associated with this action there we go and that's basically it let's go ahead and create a jump action as well while we're at it so just go ahead and click the plus icon in actions and let's call this action jump hit enter now for jump we want the action type to be button because it's just we press the button and the character jumps so we can just leave that as button let's go ahead and click on the binding and let's set the path to spacebar let's go ahead and add another binding it's just a normal binding go ahead and click on the path listen and this time i'm going to push the a button on my controller as you can see we again have two different options we have button south and a xbox controller so seeing as a playstation controller does not have an a button it has a different button at the bottom i'm not quite sure which one it is so if we choose button south out of the four buttons it'll guarantee that the southernmost so the lowest button of the four is the one associated with this action so i'm just going to go ahead and click on that one and there we go that's basically all set up now another thing you want to make sure you do is click save if auto save is not ticked but i recommend just ticking auto save that way you don't lose any of your work now the last thing we need to do in this window is set up control schemes so control scheme allows you to define what input controllers you can use in your game so up here at the left you can see we currently have no control schemes so if we click on that and say add control scheme we can now create a new control scheme let's call the first one keyboard and now we need to add what devices we want to be part of the keyboard control scheme so hit the plus icon and choose keyboard and then hit the plus again and choose mouse so now that means when we're in the keyboard control scheme we want to use keyboard and mouse go ahead and click save and now let's go ahead and click on the drop down again and add another control scheme let's call this one gamepad now we can go ahead and add devices so just say add to list and we're just going to choose gamepad as you can see we can choose xbox playstation 4 switch controllers all of them or we can just choose generic gamepad go ahead and click save again so now the last thing to do is just say which of these bindings for these different actions belong to which control scheme so if we click on left stick over here we can say okay this binding belongs to gamepad we can then click on the dropdown for wasd and you have to do this for each one of the keys separately so w belongs to keyboard s belongs to keyboard a belongs to keyboard and d belongs to keyboard as you can see those bindings have now disappeared because we are currently looking at the gamepad control scheme so let's go ahead and click on the spacebar and set that to keyboard and button south to gamepad so now if we change the control scheme between keyboard and gamepad we only get those control scheme specific bindings okay so that's everything we had to do in our player input actions asset so we can go ahead and go back to our scene view doesn't really matter now we can take a look at how we actually use this in our code so one thing to note is there is a whole bunch of different ways that you can implement this in your code i'm going to show you guys the one that i found easiest to understand and the one that i'll be using until it no longer works this also seems to be the recommended way of doing it they just wanted to give us lots of flexibility so like i said we're going to be making our player from scratch so instead of messing with our current player let's just go ahead and disable him and let's create a new player game object make sure you reset your transforms okay so now on our player game object we want to go ahead and click add component and we want to add the player input component just make sure you chose player input and not player input manager that's a whole different thing so now the first thing this player input needs is a actions asset or an input actions asset that's the thing that we just created so we can just go ahead and drag that in there next we have our default scheme so we can say okay by default we want the game to run using keyboard or gamepad leaving it on any is fine there's also an option here for auto switch which means if it detects input from keyboard or input from controller swap between the different control schemes we can just leave that as it is as well default map we can just leave as gameplay and the rest of the stuff doesn't really matter yet the important thing now is our behavior so how the input system works is when we give a certain input the input system is going to trigger something to let us know hey you've got an input and here's the value of that input so behavior is the method which the input system uses to let us know by default it's set to send messages but we're going to change this to invoke unity events so that means whenever we get input it's going to invoke an event and we can tell those events to call a certain function so that's what this events drop down here is over here we have a list of different events there are these three default events which is device lost device regained and control changed event those aren't important right now we'll get into those later in the series but above all of this we have gameplay this is the action map that we created if we click on the little drop down you can see we have movement and jump are two different actions so if we click the little plus sign over here it allows us to drag in an object and then choose a function on that object to call when the event happens this is how we subscribe to those events so what we need now is a object with functions that we can call so if you had a movement script with a move function you could drag that script from your player into this slot over here and then choose your move function whenever you get input it'll call that function but there's a little bit more to it we'll get into that in just a second so in our input folder let's just go ahead and create a new c sharp script i'm just going to call it player input handler seeing as moving forward we're going to be working with a finite state machine for our player we're going to have a whole separate script that's going to deal with just input go ahead and open that up and let's just go ahead and delete all of this pre-generated code so now we need to do is write functions to assign to those events so let's declare a public void and the first one i'm going to call on move input and the second one i'm going to call public void on jump input like that now an important thing that we need to add to these functions is as an input parameter we need to take in a input action dot callback context and this is just a data type defined by the new input system that allows the input to pass through important values this is how we're going to get what value our vector 2 is and what value our jump trigger is and by default it seems the standard is to just call it context so let's just go ahead and add that to our other function as well and yeah now we're ready to hook up the events so let's go back to unity and then click on our new player and now we need to drag our player input handler onto the player as another component we can then go ahead and open up our events again and we have our movement event up here so now we can just go ahead and drag this component from here into this slot this will now allow us to choose a function from this script so go ahead and click on the function drop down choose player input handler and then choose on move input for the jump event we can go ahead and push the plus sign drag in our player input handler again go to functions say player input handler and on jump input so now if we go back to code just to demonstrate this let's put a debug statement in each function just to show that they're getting cold so now when the movement event gets cold this function should get cold and when the jump event gets cold this function should get called so let's go back to unity and let's test that okay so i'm getting an error just because i need to disable this player after image pool as well so after disabling the player after image pool game object let's go ahead and run the game if we go to console if i push w a s or d all of these calls move input to get printed to the console if i push spacebar we get jump input if i pick up my controller and start moving the left stick you can see we have move input and if i push a i get jump input perfect okay so let's go back to our script and now let's talk about this context variable that we have so there's four important pieces of information that we can get from context context allows us to get the value of the input like i said that is if we want to get the specific vector 2 from our movement input or whether our jump input is true or false and then it also has a phase [Music] so what the phase allows us to do is find out when the input was started when was it performed and when was it cancelled this is the new input systems equivalent of input.getbuttondown input.getbutton and input.getbutton up so let's quickly go ahead and demonstrate that let's just go ahead and create a new private vector2 and i'm just going to call it movement input now in onmove input let's go ahead and get rid of the debug statement and let's just say movement input equals context dot read value then for read value we need to specify the type of value that we're reading in this case it is a vector 2. like that so that should now store our input vector 2 in movement input let's just go ahead and debug that vector 2. now in jump input let's just go ahead and say if context dot started then debug dot log so if context has started we're going to log that the jump button has just been pushed down as you'll see this will only be printed out once the instantly push down the button next let's say if context dot performed then we'll debug jump is being held down and then finally if context dot canceled debug.log jump button has been released and then we can just go ahead and get rid of this debug statement so if we save this and go back to unity and then run the game you'll see nothing is popping up once we start giving movement input i'm just going to use my controller you can see it starts printing out a vector 2. so it's currently 0 0 because the stick is not doing anything if i move the stick all the way to the left you can see it starts printing out negative 1 and 0.1 the 0.1 is there because the stick is slightly upwards if i roll the stick around you can see the vector 2 changing if i swap over the keyboard and mouse and i hold a you see we have negative 1 0. if i hold down a and w we have negative 0.7 and 0.7 so you can see the input system takes care of normalizing the vector for us which is pretty cool now let's look at the jump input i'm going to go ahead and clear the console now if i push down spacebar make sure you click on the game tab if you push down spacebar as you can see jump button push down now and then jump button is being held down now if i let go of spacebar jump button has been released now an interesting thing to note about this is that context that started and context.performed is essentially the same thing the difference is context that started is set to true and then immediately set to false within the same frame so you're only going to use this in an if statement like this where you want to do something the instant the button gets pushed down and as you can see if we go back to unity if we look at the log jump button is being held down is only printed once meaning this unity event only gets invoked when the input changes it doesn't continuously invoke while the input is being given but with this context information it's enough for us to do what we need to do there's no need for us to continuously invoke an event for a button the way we do with our movement input so just to give an example in the code i would have something like this where if context got started in the case of the jump input here i will apply the jump velocity and then if i have my variable jump height multiplier that i have in the other code which only happens if i let go of the jump button i will have that code here in context.canceled so yeah that should get you started with the new input system of course you can just write these functions in your normal script if you have a simpler controller but we're going to be moving on to a finite state machine as i've said which is going to be a little bit more complicated on our input handler side but it's going to be a lot of fun and we'll get started creating the finite state machine in the next episode i hope i've managed to enlighten you on the new input system so far and i managed to show you how cool it is with the ability to change between different devices so easily without having to do anything extra in the code and like i said this is just a quick start guide there isn't really that much information on the input system itself if you guys would like a dedicated input system video please let me know if there's enough support for that i might dive a little bit deeper into it but of course first there's more i need to learn about it so yeah let me know so yeah that's it for the introduction to the finite state machine player controller we'll get into the meat of the finance state machine in the next episode and i would just like to thank all of my supporters and wonderful people on patreon and a huge special thank you to triac cody lee pyro says and miguel for your support on patreon it really means a lot to me and i hope you guys all have a wonderful day
Info
Channel: Bardent
Views: 13,866
Rating: undefined out of 5
Keywords: Unity., tutorial, player, 2D, platformer, walljumping, wall sliding, jumping, Unity, Animation, ground, check, physics2d, castcirlce, variable, jump, height, Wall, Jumping, Movement, improvement, user, friendly, code, 2019, 2019.2.0f1, Ledge, climb, dead, cells, system, easy, beginner, animation, Dash, Ghosting, After, Image, After Image, Unity combat, Comabt, Melee, Melee Combat, Basic, Enemy, Patrol, State, Machine, respawn, hit, finite state maching, state machine, enemy, behavior, Archer, FSM, New, Input
Id: 78PLsHnRXiE
Channel Id: undefined
Length: 22min 30sec (1350 seconds)
Published: Sat Jul 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.