CONTROLLER INPUT in Unity!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the things that we get asked a lot is how to do control input and we've been holding off and doing a video on this while we were waiting for your nice new input system but the input system is now here and it actually makes it really easy to set up everything from gamepad buttons to thumb sticks but before we get into it this video is sponsored by Jason Wyman now of course we've been making videos for a while but sometimes it can be hard to stay motivated when just watching videos by yourself I mean I should know I'm on my ninth year of self-taught game development so to solve this problem Jason is offering courses where you work alongside other students and always have access to one-on-one help from Jason himself I think this is a really cool way to learn and Jason has courses to cover everything from the very fundamentals of game dev to highly professional workflows take for example the unity mastery course he will learn the whole process of creating a game from scratch and really get into topics like audio animation level creation adding abilities to your characters and pretty much everything that is to creating a finished game on top of that Jason has a lot of experience in the industry and it's just really easy and fun to learn from right now we offer the first 50 people who sign up a free t-shirt from line of code as was a big discount and other bonuses on the courses simply click the link in description to get started also special thanks to infinity PBR for his support on patreon and with that pick up your controller and let's get cracking so the first thing that we want to do is go to window and open up the package manager he will make sure to select all packages and under advanced we want to show preview packages we then search for input and you should see the input system here I'm gonna be using version zero point two point ten so make sure you're using this version or later simply click it and install it using this button right here when it's done it's going to ask you if you want to switch over to the new input system simply click yes and make sure to restart unity afterwards to apply the changes otherwise your input won't register in the new system so make sure to restart next up you need to connect your gamepad now this process is going to differ depending on the controller you're using some controllers will be plug and play simply connect them to a computer and your OS will install the needed drivers automatically however in some cases most often if you're using wireless controllers or older controllers like PlayStation 3 controllers you need to find the drivers yourself I would looking up your type of controller as well as your operating system and there will be plenty of guides available on how to set up your particular gamepad I have a PlayStation 4 controller here for me it's as simple as connecting it through USB to my Windows 10 stationary computer remember you can always go to settings then devices to see if your device is connected mine is showing up right here once it is you will also see that you Andy prints a lock saying that you connected a joystick finally you can go to window analysis and open up the input debugger if everything is set up correctly you will see all the input devices connected to a computer here and indeed the ps4 controller is showing up yay so now that we have properly connected our gamepad we're ready to start creating our input and with the new input system all this is done using an input actions asset so let's go to the project hit create and let's select input actions at the very bottom here let's call it player controls and it's double click it to open it up in a new window I'm just gonna go ahead and dock this in the center and as you can see it's currently completely empty so the first thing that we need to do is add an action map now action maps are simply used to group together actions that are related to each other in our case we will have very few actions here so we can simply go ahead and create a single one called gameplay which is going to store all of our actions and by default when creating an action map it's also going to create a single action as you can see it's called new action and it currently has no input connected to it so let's go ahead and rename this action here to grow and that's because I've gone it and set up a very very simple example scene here all it has is a camera a light some post-processing and this cube and I thought we could use the inputs to control this cube so let's start by having our cube grow every time we press a button and that's why we're calling this action grow we then need to add a binding to this action which means that we are telling it what we need to press in order for the action to be triggered so let's select the no binding here and to the right we now have a menu for going into all of the different types of input and choosing a button you can see for the gamepad here we have all of the different available buttons and you will notice that these buttons have very generic name this is because they will automatically map to the controller you have connected you can also choose to bind to specific controllers if you want they are at the bottom here and you can see there's a ps4 controller that's an Xbox controller and so on or you can simply select from this list here and everything will be set up for you so in my case here I want to use the button south which is right here however what we can also do is hit this listen button right here we then pick up our gamepad and press the key that we would like to trigger and you can now see that it notices that we've pressed this button the button south and we can then choose it I think that's just a really cool feature and it makes it so much faster to quickly set up your input so now we've set up our grow button let's go ahead and add a couple more actions so let's hit the plus sign here next to action and this one is going to be for moving around our cube so we'll just call it move and I don't want to bind this to a button instead I want to bind it to the left thumbstick so to do that we'll again it's like no binding here and for the path we can again go under the gamepad and we can of course find the thumbstick or we could just hit listen I'm gonna move around the thumbstick a bit and as you can see it shows all the different variants of this input we just want to choose the default left stick for the gamepad and now we've bound the thumbstick really really easy stuff in fact let's go ahead and do this again for the right stick and let's have it control our rotation so we'll create a rotate action let's again go to the path and listen I'm gonna move around the right thumbstick and here we get the right stick that's selected and that's pretty much all we need to do for setting up our input we can now save this input asset it's important that you remember to do this or your changes won't be applied and at this point we're ready to create a script that will do things based on this input and one thing that we can do to make this script writing process much easier is to select our player controls object and check off generate c-sharp class this is going to create a c-sharp script based on the inputs you have specified here this will make it easier for us to program for these inputs because we avoid having to access everything using strings that might sound a bit confusing so let's see this in action let's go ahead and hit apply and you can see on the asset in our project it's now created a player controls c-sharp script now you don't need to edit or worry about this at all just know that it's there so now let's go ahead and create our cube script let's select our cube let's hit add component let's create a cube script and hit create an ad and let's double click this to open it up in Visual Studio so the first thing that we want to do when working with the new input system is make sure that we're using the right namespace so at the top here we'll write using unity engine dot input system we also want to create an instance of the input action acid that we created so we called it player controls so we'll write that here and let's just refer to it as our controls let's also remove our start and update method and that's instead write void awake and remember the awake method is just like start except it's called even before start is then Inc here we can set controls equal to a new player controls object so now we've basically just created a player controls object that we can refer to going forward as controls every time we want to do something with input within this script and the first thing that we want to do is of course make our cube grow whenever the grow action is triggered so let's make some room here and let's go controls dot and if we go into unity now we can see the structure that we need to go through here so first we need to go into the right action map which is game play and we then need to access the grow action so all we need to do through code is to dot then the action map which is game play and then the action which is grow and this is the really neat thing about making sure that we are generating a c-sharp class file because if we didn't do this we would have to go in and for each one of these steps we would have to search for an action map and an action and we'd have to do this using strings which is not at all as solid so that's really cool so now we found the right action but how do we make something happen based on this well first of all we need to understand that each action in the input system has different phases that we can use to trigger input mainly we need to focus on dot started dot performed and dot cancelled in this case we want thing to happen when the player performs the action of pressing the button so we'll use dot performed now this is a callback what that means is that we can add a function to this that will be triggered when the action is performed we do this by writing plus equals and then the name of the function that we want to trigger so let's go ahead and create a function for this so down here we can simply add a function void and let's call it grow and all we want to happen inside this function is we want to go transform dot local scale so we'll take our current scale and let's multiply it so star equals by one point one so this is going to increase our scale a tiny bit and now all we need to do to trigger this function is simply write it up here in other words when this action gets performed we want to make sure that it calls the grow function however you will notice that this is currently giving us a red line and that's because when the performed callback gets triggered it also sends out some information about the context of the event this is great because later when we implement the thumbsticks this will allow us to read a value that tells us what direction they are pointing however in this case here we don't really need this information so to tell you indy how we want to use this context information if at all we can use something called lambda expressions now that might sound really scary but you can basically think of lambda expressions kind of like mini functions so a lambda expression works by inserting a parameter on the left in our case this is the context of our action we can call this anything we want but I'm just going to call it CTX for context then we write equal sign greater than which read says goes to and then on the right we can write an expression and this is why we can do things using our context parameter however in this particular case we just want to ignore it instead we will simply call the grow function so now we're using a lambda expression to tell unity that we are aware that there is some context for this action but we don't really want to use it here we just want to call the function again you will see why performing this step will be useful later when working with some sticks now we're almost ready to test this out we just need to do one thing and that is make sure that we're enabling and disabling these input actions whenever we need them so we can just go ahead and do this inside of an void on enable and as the name suggests this function is going to be called whenever this object gets enabled and here we can just go controls dot game play dot enable so it's simply going to activate all of our actions in this action map and let's do the same thing for disabling so void on disable and here we see me go controls that game play dot disabled and that's all we need to do if we now save this script go into unity let's switch over to our game view and hit play we should see that if I pick up the controller here and press the X button indeed our cube will grow awesome so next up let's implement movement and this really isn't that much harder in fact we're going to start out the exact same way here so we'll do controls dot game play and then instead of using the grow action we'll use dot move dot performed plus equals we'll again name our context CTX goes to and this is where our context gets really useful because we don't just want to know that our thumbstick was moved we want to know in what direction and how far and we can store this in a vector to our movement on the X and our movement on the Y let's go ahead and create a vector to up here and let's call it move then down here we can set move equal to our context dot read value and we're going to read a value of type vector two and let's then just close that off so up here when triggering something we're calling the grow function but down here we don't need to call a function we just need to set move equal to the value of our thumbstick and that's all we need to do we probably also want to reset this value when we're not moving the thumbstick so let's go controls that game play dot move dart canceled so at this point we stop moving the thumbstick and again plus equals context goes to move and this time we can just set it equal to vector to that's zero so just say we're on the X and see wonder why we're not touching the thumbs and now we can use this value anywhere that we'd like so if we want to move our object let's do that inside of an update call so let's go Boyd update and let's create a vector to with the amount that we want to move so let's just call it M here and we'll set it equal to a new vector two which is going to take our movement dot X and our movement dot y and it's multiplied with time that tells a time to make it frame rate independent we then call transform dot translate in order to move our object by our M vector and let's have this be relative to the world space also because I've done this before I know that with my scene we need to reverse the movement on the X so if we save this now and go into unity we should be able to control this cube here up and down and left and right by simply moving around our left thumbstick so let's hit play I'm gonna pick up the controller here and let's try and indeed we can it's working and I can not only control the direction I can also control the speed by only moving the thumbstick so much really really cool so finally let's go ahead and implement the rotation again we'll go controls start gameplay dot rotate this time that performed plus equals our context that goes to and this time we simply want to create a variable for our rotation so let's call it rotate and again we'll set rotate equal to our context that read value of type vector two and the code is so similar that we can simply go ahead and copy from up here so we want to reset it whenever we're canceling it and we just want to change move to rotate and move to rotate over here as well and now inside of our update we can again create a vector two with the amount of rotation we want so let's call it R let's create this based on a new vector to where we'll input our rotate dot Y again we need to reverse this and our rotate dot X and again I'm using the Y here in the X here and I'm inverting some of them just because that's the orientation of my scene if something is flipped for you simply go ahead and remove a minus here or flip the two coordinates we then multiply this by a hundred to make it a bit quicker and also multiply again by time that dealt a time to make it frame rate independent then we're going to write transform that rotate we're gonna rotate based on our R vector and again we'll use world space and that should be it if we now save this go into unity and hit play we can pick up our controller and we should now also be able to rotate and indeed we can awesome so now we can move around we can rotate we can do both at once and we can of course grow the size of our cube by pressing this button yay let's pretty much it for this video if you liked it make sure to subscribe and bring that notification bell so you don't miss the next one also don't forget to check out jason's courses and become a unity wizard in no time simply click the link in the description to get started on that thanks for watching and I will see you in the next video thanks of the awesome patreon supporters who donated in May and especially thanks to infinity PPR Dennis Sullivan Chris Cosentino ski Shane Cleveland face Samara Phi Leela set Ronan Justin Palmer Daniel - sanic Constantinus Corrine says no kirisaki quicker Pierce Erasmus Timah for the park and cool Swedish key you guys Rock
Info
Channel: Brackeys
Views: 358,650
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, asset, assets, model, beginner, easy, how, to, howto, learn, tutorial, tutorials, tip, game, development, develop, games, programming, coding, basic, basics, C#, input, system, controller, gamepad, xbox, ps4, ps3, dual shock, nintendo, switch, sony, joystick, thumbstick, pad
Id: p-3S73MaDP8
Channel Id: undefined
Length: 16min 43sec (1003 seconds)
Published: Sun Jul 14 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.