Unity's NEW input system in 13 minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so you're making your first game or your second or whatever you have this amazing ID you just have to bring it to life and chances are that on some occasion your player has to do some input things like swinging your sword moving around jumping crawling everything like that you have to use input for it in this video we'll be covering a couple of examples how we can do that if that's something that interests you be sure to stick around so why would you use the new input system well personally I really dislike the old input system it is very easy to use however it's very easy to make mistakes in this system you have a lot more freedom because you're working action based and you can easily create control map set map to other things while in the old system it was I mean I'm not a fan and you probably shouldn't either I think in time the old input system is being replaced with the new one but you can still use the old input system I highly recommend you start transferring all of your projects that you want to continue into the new input system the old input system always pushed me to inputs managers like rewired or if even in control however with the new input system I have the feeling like I don't really need them anymore they might still offer benefit to you but I really really like the new input system and I think you will too first things first if you're starting a new Unity project at least at the time of recording this video the new Unity system is a new Unity input system is not default in a project so we have to install it the way we do that is we go to window package manager in the top here you can see packages and in this case there's in Project we want to make sure we select the unity registry now we have a very big list let's just scroll down and search for input system and hit install now just wait for a brief second while it's installing once it's installed you will see a warning pop-up from Unity saying hey you're using new input system you have to restart basically just hit yes now that Unity has restarted we are ready to go I set up a basic scene where we basically have a little player right here on the screen our little blue dots that we are going to move around a couple of things that we need for this is first of all we need to detect input Unity already provided a very basic thing for this called the player input and we can just add it to our player this is very important otherwise well unity's input system won't detect any of our buttons the new Unity input system works on something called actions so we need to make some you can create the actions by pressing this button over here and it will create a template version but what I prefer to do is go right Mouse click create and then the bottom input actions we can now give it a name for example player controls and hit enter now we can just open it up and we get a new window this is where we basically configure everything that our player can do first things first there's nothing in here let's cover some things one at a time on the top left you can see that we have no control schemes we can add a control scheme this can for example be for keyboard or joystick things like that so in this case since I'm on a PC I'll be using a keyboard example and I'll just call it keywords I can give it a list of optional or things or requirements so if I just hit the plus here you can see there are a lot of options and I just select keyboard and I say requirement is that you have a keyboard so if I for example publish this for PlayStation this control scheme would not be active can hit save great we made our control scheme but we can't really do anything yet this is where actions and action maps come into play first let's create a new action map and we can call it for example gameplay a great example of where action Maps can be used is Imagine Grand Theft Auto where you walk around but then go into a vehicle and your control skate a change you can make a action map for for example driving your car and for player movement in general another common use case is when you have player movement and your wire interaction on a separate action map next we have to create an action there are a couple of different actions you can do in this video we will be covering a simple action by pressing a button and a more quote-unquote complex one where we do player movements so let's start with the simple one and just say it is a button and we call it for example fire this is a very common use case in unity where they use a fire as an example so we will as well so this fire action we have to make a binding for it so when we click on The Binding part we can see a path and in this case I'm just going to press listen now I can press any button on my keyboard and it will detect what to do so what I do so let's do it with f for fire and just select F on keyboards now we basically configured our first action so we can start using it in our code however let's just quickly add the second one already so how we can do this is with a little plus up here and we just click create let's call it move and this is a bit different since we want movement to be bound to one action we don't just say it's a button but we say pass through with this pass through we basically can map very easily a vector 2 to movement as a control type we can just say vector 2. and the cool thing is now is we can just click add binding and then add a up down left right Composites this means we can easily map wasd to a vector 2. so that we can easily move our character around so hit that 3D factor is fine and now we just have to bind all our actions so up listen w down listen s left listen a and right listen the the cool thing is also you can add multiple of these bindings to one action for example if you want to do movement with the arrow keys as well you can just add another up down left right composite and then both of these will work as movements if you don't have autosafe enabled here in the editor just hit save asset to be sure that you have your control setup now we can close this window and go back to our player and we have here in actions property in our player input let's just quickly drag our player controls in there so it is configured now we need to select our default scheme well we only configured one remember the control scheme for a keyboard so just select that one and we see our default map is also gameplay so this is now already configured now let's do a little bit of coding so I already set up a very basic script where we will use a rigid body and a movement speed variable to control our player however we still need to add the input to control this the first thing we want to do is get a reference to the action we want to use now there is a very easy clause for this already and it's called the input action reference so just create a public input action reference and we can import it as you can see writer automatically filled in using Unity engine.input system depending on your editor you might need to add this import yourself so this input action reference let's call it move since that's the first one we will be doing now how do we check this the value of a vector 2 composite well we can for example in our update just say hey our movement Direction equals and then say R move action dots action because we want we have a reference and we want to access the thing itself Dot read value what value are we going to read that's right a vector 2 like we discussed earlier and that's it now every update we will see what button is being pressed if it's a move action we will put it in a move Direction and we can apply it after that we you can see I already added some code to move the character around using a rigid body now let's set it up in unity so let's go to our player and we add our little scripts and it is our player movement script so you can see we need a couple of things first of all we need a rigid body 2D for the movement so let's add that real quick drag it in there we can give it a movement speed let's just say 5 in this example and then we need our reference so you can see here if we open it up here you can see we have our gameplay fire and our gameplay move actions so let's just drag our move in the move slot hit save and if we run the game now we should basically be able to move the character around so you can see we can move it around and the input is working great now we also added the other thing remember the fire so what will we do with this one well let's go back to our script copy and paste another input reference and call it fire now something different about a button is we basically USE c-sharp events to listen when we want to fire so how I usually do this is we have the lifecycle event for the on enable so in this unenable we are going to add a c-sharp event so we know whenever it's triggered so we just do fire dot action dots there are also other life cycle events for this if you want a more detailed explanation on that one do let us know in the comments and there we can just add a listener to it and call it fire for example now this is a method basically that you want to call however it doesn't exist yet so let's just create our method important here is we take in a input action callbacked context this is something Unity requires to basically do it in this example let's just do a debug.lock to say that we fired however we basically start listening to this event the moment the game object becomes enabled great it is very important however that you also stop listening when you disable it because every time you re-enable it it will add a new one so to do this it's just on disable to disable this action once again when we disable the game object we just say fire dot action dot started and minus equals in an R fire method it is very important like I said to do this because otherwise if you disable your game object enable it again press one button your you will fire twice now we've set this up let's go back to Unity and you can see we have a second input action reference here so let's just copy it right in here and we see our fire input action has been bound hit save real quick so if we start Unity now you can see that we can still move around but if we now press f we are spamming fire in the console every time we press it and this is a very basic setup of how the input system can be used now if you want a more Deep dive into the input system or if any specific things like you want to see like action rebinding for example letting the player configure which key he should press for the fire action do let us know in the comments I hope you learned something and that you can now add input to your game so you can basically find everything you want the fire we use here can easily be your spacebar key to jump and things like that so if you like the video do make sure to hit the like button and if you like this kind of content just hit that subscribe button because we do make content like this twice a week we also make other things like Vlogs General Game Dev tips or even the more business related side of indie game developments so do make sure to hit that like subscribe button if that's something you fancy and that's all for today thank you very much for watching and I hope to see you guys in the next one bye
Info
Channel: BiteMe Games
Views: 14,027
Rating: undefined out of 5
Keywords: gamedev, indie, indie developer, game development, unity, c#, blender, game design, game studio, devlog, development, startup, forge industry, flega, steam
Id: ONlMEZs9Rgw
Channel Id: undefined
Length: 13min 2sec (782 seconds)
Published: Tue Jul 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.