The simplest way to use Unity "New" Input System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so you decided to give the new input system a try you open the package manager search for input system and install it suddenly the suspicious pop-up shows up blah blah blah doing so will restart the editor and will disable the old Unity engine input apis sounds a little bit scary but you're committed you press yes okay Unity restarts as promised you drag and drop your character into the scene and what now here it comes the hero of our episode the player input component you literally just add it to your character click the create actions button and save the file somewhere and boom the component generated for you input actions short recap before we go any further the new input system is even based that means that instead of checking the input every frame in the update method you respond to the actual events like Mouse click or button press no actions like that are defined by default you define them in the input actions asset which you can create by clicking right Mouse button going to create and at the very bottom you will have the option input actions when you double click it you'll land in this window over here there are several things you can create here but only three are relevant for us at this moment action Maps which are collections of related actions actions which are the actual input events like walk move look short and so on and then there are actual bindings one action can have multiple bindings for example our walk action could have a set keys and arrow keys assigned to it it action has its own type value and pass-through are for continuous actions and the button is for one time action like button press or Mouse click the continuous actions have also control type which is nothing more than return type for example float or vector 2. if you would like to learn more details you can check out this tutorial the actions generated by the player input component are great but they definitely won't be enough for your game you can always add your own ones or modify the existing bindings if needed before you exit the window make sure you save the file by pressing Ctrl s or clicking save as a button on the player input component we have several fields which are pretty important actions field which is nothing more than just a field for our input action asset default schema and default map which enable the selected schema end map by default otherwise you would have to do it by yourself in the code and then Behavior which defines in which way you want to be notified about the input events the player input component can do much more than will explore in this tutorial if you would like to learn more you can always check the documentation I will leave a link to it in the description and by the way I would really appreciate if you could like this video and leave a comment this would definitely please the algorithm gods and maybe even make them show my videos to more people ok let's make our character walk using those four behaviors the character has a rigid body with the gravity set to zero and the player controller script inside of it I just grab the rated body to the Indio egg method and store it in the private field when the player input behavior is set to send messages or broadcast messages you can handle the input by creating a method with a name on and then action name so for the move action the method will be called on move then the method can have no parameters in case you are not interested in the value but only in the occurrence of the event or have the parameter of type input value in case you are interested in the value in order to get the value from the parameter we use the generic method get so to make our character move it's as simple as making the value.get vector 2 and then times for example speed variable so the broadcast messages Works more or less the same way from what I understand the only difference between sent messages and broadcast messages is that for decent messages the player input component and your component that uses the input have to be on the same game object for the broadcast messages the message is sent down the game object here so it can be handled also by the script on the children objects to be honest whenever I work with the player input component I usually work with the send messages Behavior there is however one big problem with that if you misspell your action or method name it will simply not work no errors no warnings or anything like that so at times it can cause a little bit of confusion the third behavior is invoke Unity events and it deals with that problem very well for each of your actions it defines the unity event in order to subscribe to it you have to have a public method with a parameter of type callback context the context doesn't come with the get method so we have to replace it with the read Value method which works identically now we simply press the small plus icon under the event we are interested in drag and drop our object and then we have access to all public methods and variables on that object this is pretty convenient notice that you drag and drop the object that means it doesn't have to be the same object as the one having the player input component that creates quite a lot of possibilities the last behavior is invoke c-sharp events and to be fair with you I think it is terrible don't get me wrong I love c-sharp events but this implementation is pretty strange instead of events for each individual action you have one common event let's have a look at that first we need to reference to the player input we assign it in the awake and then on enable we subscribe to its own action triggered event we do that with a method that accepts the same parameter the Callback context foreign but now because it's one short event for all the actions we have to Branch out inside the method the unit is documentation about it is not really that helpful so I haven't found any better way than identifying the actions by their name and this has exactly the same problem as descent messages rely on strings which can always be mistyped but at the end of the day it works my personal favorites are the unity events and decent messages behaviors but of course you can use whatever you feel comfortable with so now you know the player input is the simplest way of working with the new input system I hope you found this tutorial useful have a fantastic day love you and bye bye
Info
Channel: PitiIT
Views: 18,462
Rating: undefined out of 5
Keywords: unity 3d, unity 2d, unity tutorial, input system, unity input system, input system unity, input system tutorial, how to use input system, new input system unity, how to use the new input system unity, unity, input, send messages input, broadcast messages input, unity events input, player input component, PlayerInput component, PlayerInput Script, how to handle input in unity, input in unity, keyboard unity, mouse unity, controller unity, pad unity, unity input tutorial
Id: Wo6TarvTL5Q
Channel Id: undefined
Length: 6min 39sec (399 seconds)
Published: Mon Jul 25 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.