Top Down Movement with The New Input System - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it would multiply the speed together from the two values unless you changed it manually with code so we have a fresh new unity project so the first thing we're going to do is go edit project settings i'm going to pull this one out under the player tab we're going to scroll down make sure we have active input handling set to input system package new now we want to apply that awesome so once it's restarted i also want to quickly set our api compatibility level to net 4.x we'll just wait for that to load now we need to go to window package manager we'll wait for the packages to load these are all the ones that have been installed but we actually want all of the ones in the unity registry so once we've selected that we're going to find input system and click install once that's installed we can close this set this back to the inspector now i'm going to right click create input actions down the bottom and we can just call this our player input actions so if we double click on this it's going to open up an input actions window now we have an action map an action and a properties so our action map can be used for stuff like what sort of environments we want to be using so this one can just be for our movement next we have the actual action so if we click on that we can see on the properties that the action type is a button we want to change the action type to a pass-through value and the control type is going to be a vector 2 so now in this action if we click the drop down we have no binding so we can just delete that binding by right clicking and deleting and we can click add and we can add a 2d vector composite and we'll just call this our uh now moving move and our action can be called move as well so our up binding we want to use whatever path we want to go up so in this case i want to use the w key on the keyboard for down i want to use the s key on the keyboard for left i want to use the a key on the keyboard and for right i want to use the d key on the keyboard make sure once that's done that you click save asset that's crucial now we can just drag this somewhere and leave it for the moment so once we've saved the asset if we go into our assets and click on our player input actions on the inspector we can generate our c-sharp class and we can also change what sort of c-sharp file we want to call it so player input actions is fine class name can be player input actions we can just click apply and this will generate our c-sharp class so as you can see we've now got an automatically generated input action script now let's create a player to use our input actions so i'm going to be using this sprite pack link in the description to set it up i'm going to set it to multiple for the sprite mode pixels per unit i'm going to set to 16. we want the filter mode to be point no filter make sure you click apply then in the sprite editor we can pick whatever sprites we want i'm going to be using this little knight here so we can just drag a box around and once we've got that all selected i'm just going to click apply close that and now you can see we've got this little character so we can click and drag this character in now we have a little character on the screen i'm going to name this to our player we're gonna add a few components the first one being a box collider 2d and we can edit this so it's just around the player and if it's top down we probably don't want it to be over the head we also want a rigid body 2d we want to set the gravity scale to zero and in the constraints we want to freeze the rotation we also want to add component and click new script this one's going to be called player controller press enter it's going to create this new script and if we double click on the name here it'll open it up in your code editor of choice the first thing i want to do is grab a reference to our player input actions so we can grab a private player input actions we'll call this player input now we want to change this start void to an awake void and inside the awake void we're going to set our player input equal to a new player input actions we're going to have a new input manager for our player and you can create multiple inputs if you have maybe a multiplayer game on the same keyboard or multiplayer game on different controllers the next thing i'm going to do is get a reference to our rigid body so i'm going to have private rigidbody2d we'll call this rigidbody2d rb next we want to get a reference to our rigid body so we're going to say rb equal to get component rigid body 2d this is going to grab our rigidbody component that is on our player so now i'm going to create a new private void on enable and on enable we want our player input dot enable next we want to create a private void on disable and we want our player input dot disable so we're going to disable our player input the next thing we're going to do is change this update to a fixed update because we're going to be using physics the first thing we want to do is get our vector2 value from our player input and store it in effector2 so we can use later so i'm going to say vector 2 move input going to equal to player input dot movement dot move dot read value vector 2 next we want to set the velocity of our rigid body so i'm going to go rb dot velocity is going to equal to our move input vector i'm going to multiply that by a speed value now we can set our speed value by typing in serialized field which is just within square brackets and private float speed and by default we can set it to something like 10 f now what is this serialized field section this allows us to see our speed in the editor and even change it within the inspector so once we've finished typing this code in we can click save and we can go back into our project so now if we hit play we press up we go up down we go down left we go left and right we go right as you can see if you press a n s or w and d we go the same speed so that was a issue that was in the default unity input manager is that it would multiply the speed together from the two values unless you changed it manually with code but now it's all the same if you found this video helpful please leave a thumbs up also please subscribe for more content coming soon like to thank my patreons for supporting me on patreon please support me on patreon if you can also join my discord server and with that i'll leave you to it thank you [Music]
Info
Channel: Chillehh
Views: 10,057
Rating: undefined out of 5
Keywords: unity, tutorial, chillehh, game development, new input system, new input system unity, unity3d, unity 2020 tutorial, top down movement unity, top down movement unity 2020, top down movement tutorial, unity tutorial, top down unity input system, top down unity game tutorial, how to make top down movement in unity
Id: A1AFhxQ4fJY
Channel Id: undefined
Length: 10min 15sec (615 seconds)
Published: Mon Nov 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.