Unity's New INPUT SYSTEM | Unity Beginner Tutorial 2024

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I know there's many of you that have felt deep despair just as I have when it comes to thinking about setting up inputs and using the new input system in unity and people have said to me well it's not the new input system anymore so there's no other good name to give it so I'm just going to call it the new input system for the basis of this and I've worked countless hours putting this tutorial together and do be sure to comment down below if you have any thoughts or improvements on this because you can share it with everybody else and make sure they get the best details possible possible because you can program this UI system in so many different ways just because I show you it this way doesn't mean you can't do it in 10 other ways too leave a like because it really helps me out and do subscribe to me speed shoor if you find this helpful I'm going to show you a basic reference to looking at the old system against the new system I'm going to show you how to install it how to set up your first control map with all your actions writing your own manager script writing your own 3D and 2D controller taking into account dead zones and how to stop your your character from moving even if you're not touching the stick how to invert controls through a processor through code scaling sensitivity so you've got Mouse and Gamepad which share the same sensitivity how to check when devices are connected and how to know when one's been lost and being reconnected and then looking at how to navigate uis looking at how to rebind controls and looking at all the sample assets and everything Unity give you and various other controllers which would be helpful to you and I'll leave loads of annotations in the timeline so you can Skip Along and I'll put it down in the description with all the resources that I've got for this video too and do be sure to check out my patreon to get access to all these scripts this entire project and everything you'll need and unity currently have their Unity award sale on at the very minute and I'll put all the links below now this is a really simple reference when we look at the old input system against the new input system is that the way that we do basic button presses let let's say we used get key down but when we know that usually in the old input system if we press the space bar when we use get key down it's when the space bar is pressed down we make an action happen that's great for the vast majority of button actions that you do and in the old system to say do sprinting we use get key and that just understands when we're holding the key down we make an action happen but how do we translate this to the new system if you just want to use a get key down you can make sure that you've referenced the input action that you want to use which in my case I've got an input action called jump action and I make sure at the start method I find that in the player controls I find the map and I find the action that I want to use now I can do something as simple as jump action. triggered that means if I've pressed the button I must have triggered this button to press and away we go now forget key we make sure that we also have the action that we want so an input action which in this case could be Sprint action we make sure that we found it in the action map and found the action so if it's anything above zero we must be holding the button down because the unity input system recognizes it Sprint action do readv value and then in angle brackets float then just two brackets at the end and if it's greater than zero then we can make our action happen so first of all I'm going to show you how to install the newer input system and I am using the newest version at this current time which is unity 202236 and I'm using Unity urp and it does work across most of pipelines and previous versions too go to window package manager and once package manager is open you can navigate to the top Corner go to Unity registry and just type in on the top right that you want input and you will get the input system and mine is a version 1.7 at the very moment doesn't matter if it's newer or older then you want to click install and it will bring up this error message which will tell you that you need to restart the unity Editor to make this work and this is also important if you install assets from the unity asset store you may need to restart your Editor to make the new input system take effect so just hit yes to restart Unity now if you want to change between the new input the old input or use both you can go to edit project settings and if you go to player and then scroll on down to where it says in configuration and shows active input handling you can choose whether you want to use a new one explicitly the old or you can use both I like to use both in case I want to use the old input system just to make some quick inputs for debugging and I just wanted to say before we start creating our player actions map this could be useful for other people too on your binding of Your Action that you've created when you want to choose the path to choose what type of control you have if this is broken and you can't listen for input all the navigation is looking weird and it's just not working correctly in the dark theme it can't find a particular toolbar now I did some Googling and I found a fix for this thankful for all the people on the unity forums because for some reason Unity hadn't fixed this simple issue there is one letter of a spelling mistake deep in the unity files that you need to fix so if you navigate to see users your computer name appdata local Unity cach packages packages. unity.com com. Unity input system at whatever version you're using input system editor internal Advanced dropdown and open the advanced dropdown GUI I know that was a hell of a lot to take in but once you navigate to the Advan dropdown GUI you can open that up in any editing program you have you can open up a notepad whatever you want visual studio and here search will be spelled without an R so make sure you add an r in there save that file and then if it's not reflected or changed in your editor make sure to navigate to the root of your project and delete the library folder then open your project again and it will rebuild the entire folder and it should be fixed and you should be able to listen for the controls and it will make your life so much easier so now we're going to create an input map to control all of the actions that our game will use I'm going to right click the project panel go create and choose input actions at the very bottom and I'm just going to name this to player controls double click that to open out the editor and you'll have three sections one which is action Maps one which is actions and then the settings for the actions that you have now action maps are just names of groups of actions so you could have for UI you could have for vehicles you could have for multiple players so I'm just going to name this one to my player and these are all going to be my player particular actions so what I'm going to have for player actions is I'm going to have move Look jump and Sprint so it gives you some ideas on movement to be able to do basic interaction on a button press and sprinting to be able to hold the button so these are probably the most use cases you're going to have with an input system so of our actions we've already got an action we can rename that to move because we're going to specify that in code later so it's good to give it a name which you're going to be able to refer back to and remember and then on the right side you've got the action type so you've got pass through button or value now for a move it's going to be a value because you can move in multiple directions left right up and down so you want to make sure that it's a value and then you want to make sure that it's a vector 2 because in Vector 2 you can move left and right and you can move up and down you don't move in the y axis because most likely not going to be doing something like flying so we don't need to worry about that and then from there we have bindings and if you need to add new bindings you can click the little plus and click add a binding Now The Binding is something where we can attach a control scheme to it so say we want something on your game pad or something from your keyboard now on The Binding here what I'm going to set I want something on my Gamepad in this case I'll cycle to Gamepad and I'll probably want this as my left stick because that's going to be the default action in most games to move around now we might want to use something on the keyboard so WD type of scenario now you can click on the add a new binding and you can add an up down left right composite and that just means that it's created a vector to for up down left and right so as you can imagine up on the keyboard is W so you can see W keyboard we can select down and when we search in s and left and of course you can listen and you can press the key and I've searched for a and then right will be D so I can do listen and D so there'll be the bindings for that and you can obviously add more if you want but what we're going to do is I'm just going to duplicate move because it's very similar for look so this one's going to be our look action it's going to be a value and Vector 2 but in this case it's not going to be a left stick anymore it's going to be a right stick and then then this time it's not going to be a vector 2 we're going to add a new component which is just a generic binding and I'm just going to search for Mouse and it'll be Mouse Delta so it's the position of the mouse at any time so this is it for two actions which have multiple values depending on where we're looking now one which is just two buttons so in this case I'm just going to set a new action as Jump by clicking the little Plus in the corner to create it then my binding will be on my game pad we can have button South which will be using ARX on the respective game pads then we can add a new binding which in this case keyboard usually the space bar which is nice and perfect and remember that the action type can be just left on button because it's something that we only want to usually do once or when we're actually pressing it it'll only have one action then we'll click another one for the Sprint Sprint is exactly the same it's still a button The Binding is going to be whatever you want and I will usually use the left shoulder on a game pad I'm going to listen to and I'm going to press left shift or you can have shift just generically on your keyboard if you want to use all shift keys to take into account I'll keep as left shift for this because I only want it to that side of the keyboard and then one thing to mention when you do this it's good to have auto save enabled so when you make changes in here it will be automatically saved so you don't have to then close it and save out it will just so you don't forget it's a big thing to remember actually because some of the times I was making some edits and I didn't save it and I was thinking why is this not reflecting in my game because auto save wasn't enabled and of course if you close this and you hadn't saved it it would ask you to save it because it's just a prefab in itself so now we've created our set of actions we're going to create ourselves a manager script which is a way to centralize the controller for all our different inputs so we never need to sort of do long references in code anymore we're going to control it in our manager and then we can do shorthand nice easy refer references within our scripts and we can use these across multiple multiple scripts as well so we'll right click in the project panel go create and I'm going to choose a new C script and I'm going to call this my player import Handler I'm going to open that up in Visual Studio I need to make sure that we're using Unity engine. inputs system so I'm going to start off as a header and I'm just going to create and make this nice because this will be all up on my Patron to get all the projects and all the scripts just so that you you never miss anything out but we're going to keep this nice and neat so we can keep it organized so I'm going to have square brackets serialize field private and I'm going to call this an input action asset and we're just going to call this our player controls so that's the inut action asset we just created with all those actions and what I like to do to make things easier is to create a reference to the action map you know what the action map could been when if we wanted to control a vehicle player one player two being able to fly whether it was UI remember when I talked about it but in this case I'm going to to write square bracket serialized field and say private string and we're going to call this the action map name and we're going to set that equal to player because you saw that in our action Maps here and we Nam this player I'm going to create string references to the names of each of my actions just so that we can edit this in the inspector if we need to update it more easily in the future you don't have to do this but I like to do it just to make it nice and easy for you to read and if anybody else comes to read it it's nice to use so we're going to say private string move equals in quotes move or whatever you name direction again we're going to have private string look set equal to look in quotes and these are the two are just jump and Sprint and now we need to actually reference our input actions that we're going to find at the start just so that we'll be able to use them in script so we're going to write private UT action and we're just going to name this my move action new UT action as the look action and nicely Visual Studio knows what I want so I want the input action for the jump action and the Sprint action and these are all private references to those so I'm going to create some properties now for the value of each of our actions that when we create them so we'll know when something is pressed your controller is moved and what direction it's moved in or if we're sprinting and it equals a specific value and this is instead of just doing a public reference to the thing would use a getter and Setter to make it more protected to be able to nicely use it within script so in case for our moving action you want to set this as a vector 2 and I'm just going to call this move input then what I'm going to do in curly brackets is write get col on and then just have a private set and I'm just going to copy that line I'm going to paste three below and then have this one set as a vector 2 and my luck input then I'm going to have instead of a vector 2 I'll have it as a ball whether my jump is triggered or not and then in this case we're going to have a float value to be able to tell whether we're sprinting and now mentioned in the reference that Boolean is true or false and the sprinting is is it greater than a particular value now I want to use this script as an instance or a Singleton so I'll be able to reference this from other scripts nice and easily and it'll be the only one that exists in my game so I'm going to make sure public static and I'm going to reference the script name have it as capital instance and have this as a get set again then we're going to have void awake so in my awake method we need to make sure that we actually set this instance to make sure that we can't have duplicates of it at any time so I'm going to say that if the instance is equal to null then we'll say instance is equal to this so we can only use one then we'll use don't destroy on load game object which means that it won't destroy this object if this already exists and if in any other case that it doesn't exist we'll make sure that we'll destroy the game object in case it makes a copy of the script again because we only want one to ever exist now still within our W method we need to be able to reference our actions and find them exactly where they are because we need to be able to use them so we'll say move action equals the player controls doind action map and then in Brackets instead in actually specifying the string name I don't like to do that so I like to make it easier CU we created the action map name and then we'll find the action in there and then in Brackets instead of specifying in quotes we can just write the name that we'd already created which was move as it above is the action map name move and we found that action that we want to be able to use and obviously we need three more and as you can IM action look action equals play controls then we find the action map and we find the action of look and then as I say in here we can specify the names if they ever change in the inspector without having to run into into our code into our manager to be able to change this then a really important thing to do is use on enable and on disable because when we're using this input system it uses events so we want to enable and disable events when we don't need them so we can reference every single action and make sure that we use the action do enable in all of these cases and as you can imagine on disable we need to make sure that every action disables when the game ends or the script or we move to another scene if we get rid of this script we need to be able to register these actions that when they happen we need to be able to read the values in them so to keep this nicely organized in our work method I'm going to create a new method called register input actions so just below here I'm going to void register UT actions just like that because something needs to happen these need to happen in a way to be able to register the actions so we're going to say move action. performed plus equals context text equals greater than the move input equals context do read value in square brackets Vector 2 and then add brackets on the end and a semicolon so this just means we're looking for whether the action is performed anytime we're just giving it an intermediary method and we're looking at the move input which is a vector 2 we're reading the value at that time when we're looking for it and then we know exactly where it's going to be and with this you have a performed action and you have a canceled action in the canel section so we have the move action do canell plus equals the context and then equals greater than move input equals Vector 2.0 because in the canceled it shouldn't be doing anything we shouldn't be moving because we're not pressing key so nothing should be happening and we'll do this extremely similarly with the look action so the look action. performed where they've got the context we're looking for the Lo inut and then we're going to read the value and when we're canceling and we're not doing it with there's nothing to happen and then in an example if we're doing a Boolean like a just a get key down then we have the jump action. performed we've C the context we're looking for jump trigger but in this case was a Boolean and all we need to do this time is it was set to true if it's performed because we're just pressing the button down once and if it's cancelled it will be false and we won't be pressing a key so we shouldn't be doing anything and then in the case of our sprinting we're looking that if it's performed we're looking at the Sprint value and we're just using the context read value and instead of Vector 2 we're reading the flow because that was the that was the property type and then if it's not doing anything Sprint value would equal to zero so it just means that the button wouldn't be pressed it wouldn't have any value so with this now nicely set up we can reference these inputs by just using any of these here whether we want to reference the Sprint value whether want to see if the Jump's triggered whether we want to use the look input or the move input to see what position the character is in at any point we can use this and of course if you add any other actions you can add a new string you can add a new input action you can check or create a new property to be able to look at what value that needs then you can find the action that you need the action map with the action make sure you register that action with whatever you need and then being able to make sure that you enable and disable that action and once we've got this set up we've done all the leg work with setting up all of our actions so now with all our input controls set up and the input handle manager script created we're going to look at creating our own firstperson controller from the start I've just got a empty game object and this has just got an character controller object on it so you can go to add component and make sure you add character controller and with the character controller on you will see it has its own collider this is just a built-in component by unity which lets us set the height and slope amounts and have some buil-in functions so we can create our own firstperson controller I've got a main camera attached to that firstperson controller which is just on top and of course I will have this all on my patreon here to get hold of it so you don't have to create it yourself if you don't want I do have a more complex version on my channel which is about adding footsteps and things we won't add that today we'll just add the basic components to a firstperson controller so I'm going to create a new script by right clicking going create and choosing C and then I'm open the script called FPS controller of which I've just named it I'm going to add a header which is just for my movement speeds and then I'm going to have serialized field private float walk speed set that equal to 3.0 F then I'm going to have one called float Sprint multiplier so I'm going to set that to two at the start and this is going to be that when we start sprinting we're going to move twice as fast then I'm going to have some chump parameters which is just two floats for the jump Force which are set to five and the gravity and normal gravity has a value on Earth of 9.81 then I'm going to have two floats for my Mount sensitivity and the range of which we can look up and down so we might want to limit that so we can't look all the way around and break the game then we'll start by writing an awake method and we want to find our character controller this is the thing we're going to use to force us to move the character rather than using a rigid body or anything like that so we need to create a private reference to the character controller component I'm going to have that as lowercase character controller so we're going to have the lowercase character controller is equal to get component then we're going to look and find the main camera because we're going to be able to move this when we come and do our Mouse rotation so we'll say private camera and call this main camera so we're going to make sure that we create or find our main camera so we'll say main camera equals camera. main then we're going to have another field which is just the input Handler for the input handling and we're going to set it equal to input Handler equal to the player input handler do instance which is the object that we created which would exist only once in our scene then we're going to have ourselves an update method and within this method I'm going to have two methods which control our movement and our rotation as two separate things so we can keep this nice and organized so now we'll have a method called handle movement so first of all I want to show you how to use the input for doing sprinting or to be able to detect that key that you hold down so we're going to calculate our speed based on what our walking speed is and then timesing it if we're sprinting so I'll say for this time is I'm going to create a local variable called speed and then set that equal to walk speed Times by the input Handler that we've created the reference for do Sprint value so that value of whatever the key was at any time if it's greater than zero at any point we must be pressing that key down question mark Sprint multiplier and then colon I'll just say 1 F then because the question mark just means that if it is above the value we must be sprinting if not it's just going to be a value of one so walk speed its normal is Times by one so anything times by one is the same and the Sprint modifier by default is two so it will double our walk speed now I want to get the direction that we're looking at in local space because it's where our character will be looking and then we need to transfer that to World space so I'm going to create a vector 3 have this as our input Direction and have that equal to new Vector 3 then open up brackets and say the input handler do move input. X comma 0f comma input handler. move input doy so we've got x y and Zed so we're moving in the X Direction here we're not moving up and down in the Y and we use the Y value because that's just how Vector 2 Works in this but it's translating to The Zed and then we create another Vector 2 to transfer this from local to worldspace so we'll say that our world Direction just so we can use it in game we'll say transform do transform Direction which is a method built in which allows us as it suggests to transform from local to World space and input our input Direction then it's good to normalize that if we're pressing one key we move a certain speed but sometimes if values go above accidentally let's say you press W and a at the same time sometimes we might be able to move faster than we mean but we need to normalize the value so they never go above one so we can then say that our will Direction dot normalize so this is just a method which will do that for us without us doing any extra calculations it will just make sure that it's limited so now we need to take the movement directions and apply our movement speed to make sure that we can actually move so I'm going to create a local variable at the top and it's called private Vector 3 and just have this as current movement because we may need to use this in other places we'll go back down into our handle movement method and say that current movement dox is equal to World Direction dox Times by our speed and then the current movement dot Z is equal to the world direction do Z Times by the speed so we're always moving in the X and the Z axis because we never need to move up and down cuz we're not doing anything like flying then I might want something to handle our jumping which we will do a little bit after and then under there we need to translate that to fully moving our player controller and we can use in character controller that we referenced above do move and then in Brackets our current movement is to move that character controller with the built-in method that unity's character controller component has and we'll times that by time do Delta time so we move that in real time so if I want to give you an example of handling the jumping so we can just call this method handle jumping and then in this case if character controller dot is grounded and this is good because it's a buil-in component into the car controller so we can see if we're on the floor or colliding with anything then we'll say that current movement doy is equal to minus 0.5f and then we'll want to say that if input Handler dot jump triggered so if we've ever pressed the jump key we'll say that our current movement dot Y is equal to the jump force and then if anything else underneath that where whether it's grounded or not we'll say that our current movement doy minus equals the gravity Time by time. Delta time so which this means is if we're not grounded at any point we must be in the air so we want to bring the character back down to the ground where it should be now we'll create a method to handle our rotation we'll create a private field which is just called Mouse X rotation set that equal to the input Handler dot look input dox Times by our mouse sensitivity and then we'll say that transform. rotate Open brackets Z comma Mouse X rotation comma 0 this just means that it'll rotate based on the EXP position of the mouse based on the look position Times by The sensitivity so we can make that happen so then we need to be able to set our vertical rotation and be able to limit that to so we want to go back to the top want to create a new field here and say that Prim float vertical rotation we'll scroll right back down and we'll say that our vertical rotation minus equals the input handler do look input doy Times by the mount sensitivity in this case and then we'll say that our vertical rotation equals math F do clamp Open brackets the vertical rotation comma minus up down range comma up down range and end that so we'll make sure that the vertical rotation never goes above or below that 80° that we'd set so we can never go upside down or something like that main camera transform. local rotation equals querian do Ula Open brackets vertical rotation comma 0 comma 0 so then we can make sure we save that out so as you can see here I've got my FPS controller with the character controller component on it we can add FPS controller script and you can see that we've got all our variables set up but on our main camera you need to make sure that you have got a tag of main camera cuz that's what we're going to need to use and we also need to add our manager or input Handler that was going to manage all the inputs so we'll just create a new empty game object I'll reset by right clicking the transform and clicking reset and then just renaming this to our player import Handler because this is the only one that we're ever going to have so we'll add it to our scene and you can see that it's now it's looking for the player controls asset which you can just drag in you can make sure that it's got the correct name of player and all your different actions you can see I'm moving around and walking with my mouse and I can switch to my controller and you can see that I can move much more smoothly with a controller and I can walk around with a left stick I can press X to jump and the left shoulder to Sprint and still Jump Around move around but you'll see that sometimes we are a little bit slidy using the controller like that but we can set the dead zones on these and I'll show you how to do that compare the sensitivity of my mouse when I look around this is just me being really soft with the mouse and then me looking around as quickly as I can with a controller and they're not the same so we'll look at scaling these so they are the same in either case as I'm sliding around this scene so first of all I'm going to show you how to stop that slidy movement when you might have a controller which could be older like mine mes an older Xbox 360 wi controller now say it on your movement and on your look these are the two things that are causing that issue and say I want to add dead zones to my actions which are the things that might move my controller sticks so I can apply the processor overall to both actions or all bindings underneath but I might want to just select my left stick which is for my move click on add to add a processor and add stick dead zone now from there I can remove the default because the default was too low for me I'm going to have it about nor .2 something around that value you can see now I can still walk around with my mouse and keyboard just fine and now I can walk around with my Gamepad and I can move even my left analog stick a tiny bit and I'm not now sliding in any direction so now we get a much more even character controller which doesn't move around and this can be different based on the different types of controllers you use and you can set this too and again if you need to set this for say your Mouse look which has a slight deviation on mine you could set the processor on the right stick you want to keep this as close to that minimum value as possible without getting an issue you lose some semblance of sensitivity if you're not careful now you might ask me how do we adjust the dead zone sensitivity in code now I'm going to give you a really quick example in the player input Handler and we'll just put this in the awake method just so that you can see it happen as soon as the game starts we'll do input system Dot settings. default Dead Zone Min and then equals to whatever you want your new value so you could create a field at the top so in this case I've just created a new float which is the left stick Dead Zone value and then if we go back down to input settings. settings default Dead Zone value equals our left stick Dead Zone value and now on our input Handler we could set that to in this case 0.3 because it was different to what I have if I open my player controls again go to my left stick and you can watch the Dead Zone Min here now you can see my dead zone that when I press play when I don't have the default ticked you can see that the dead zone never changes now if I leave default ticked and then I press play you can see that the default dead zone does change that default value and this is actually the easiest way to change that dead zone if you're leaving the default value as it is and want to change this and S like some settings now I said I'd show you how to limit the sensitivity between The Mouse and the game pad so we can select on either of the bindings we'll start with the right stick I'll add a new processor and choose scale Vector 2 now I'm going to scale this X and Y by about two then I'm going to look at the mouse Delta and also set a scale and I'll set this to maybe 0.3 on both of the axes because you want to test this out and see what works for you because remember on our FPS controller we and we can set the sensitivity this is classes mouse sensitivity but it's just generic sensitivity so you can adjust that between the two but you want these to be as close as possible so when I'm moving with my mouse it's looking less crazy and then if I grab my controller and move around we've got a very similar looking combination between scaling these two things and of course you could adjust the sensitivity so they're both are not as crazy or create yourself a menu which just adjusts this value so you don't ever have to adjust the processor at all the processor is just to scale the two I said I'd show you how to do inverting now there's two ways to do inverting one which we just adjust the FPS controller and one which we can do in the processor now with the processor it doesn't allow us to change this up run time so it's not always the best way but if you wanted it by default you could either select your bindings for looking or apply this to the entire action itself so for look we can add a new processor and choose the invert Vector 2 so I don't want to invert the X I'll invert the Y now when I play you can see Up Is Down and Down is up and I'll I'll do it with the controller so Up Is Down and Down is up just to confuse things maybe if you're wanting to create accessibility or say a flight simulator but I would say that's not the best way and we can do this in code so I'll show you it in the FPS controller because this should only be specific to your character itself create another serialized field which is Inver ya AIS and set that equal to false cuz this is just going to be a camera setting that we might want to use we'll scroll down to handling our rotation and at this point here we need to take into account and what our invert is going to look at so we'll create a local variable here we'll call this mouse y input set that equal to the invert y AIS have a question mark and then I'm going to say that our input handler do look input doy colon and we'll do the same again the other side and add a semicolon on the end but in this case it will look at y input both of the same value but actually we so we need to add a minus between the look value to do in negative values now in the bottom bit here here where we handled the vertical rotation and we were looking at the Y input we've already found this like if you see here it's exactly the same piece of code is just comment that out we'll say the vertical rotation minus equals the mouse y input because we found that local variable and then times that by our mouse sensitivity so it'll either be the minus value or the positive value depending on our Boolean so you can see here that I've got camera settings and I can say yes I want to invert my control and you can see now down is up and up is down you could adjust the X but I don't think that's quite as common now if I untick the invert y you can see that now up is up and down is down as it was normally you can keep this to your controller so you could set this in a settings menu or something I know have a full tutorial on creating a settings menu so it might be something that really helps you out for that and I'll put the link down below to that too I wanted to show you how to quickly print out a list of all the devices that you have connected and to be able to understand that when a device becomes disconnected what you can actually do about it so let me create a new method in our wake method and this is just going to be called print devices this is going to be inside the player input Handler for each so we're going to create a for each Loop I'm just going to press tab to create that for me but I'm going to write variable device because this is going to be inside the input system then we're going to say in input system. devices because this will look through all the devices that are currently registered and then we'll say that if device do enabled so if the device is available we'll say debug.log active device in quotes and then add a space and I'll put plus device. name in the start we expect to see a log of all the devices that are registered so now you see when I've pressed play here you can see the active devices keyboard mouse and X input controller for Windows which is all the devices as I said add a controller a mouse and a keyboard and it's perfect for sometimes you might need to check if they they're actually around now I'm going to give you the example of how you can detect when a device has been disconnected or reconnected and then show a popup or something like that in your game to make sure that sometimes if you're developing for console you need to know when something on your controller is disconnected so I will add this to my input Handler and we need to do this in the on enable and disable and we need to subscribe to some events so we can use input system do on device change and then plus equals the on device change method which the difference is one's lowercase and this is just a method that we're going to specify and then on disable I'm going to copy and paste it in and it'll be exactly the same except it'll be minus equals because we want to unsubscribe from the event when we don't need it anymore so we'll create ourselves a new method to be able to take this so we'll say private void on device change and then we're going to write some parameters to take into account exactly what we need look for an input device call that device then have the input device change and have that as change and then we're going to write in here a switch statement but instead of detecting the advice in the switch statement we're going to actually just look for change and then in the case here the first case we'll say the input device change do disconnected and there's a load of different ones if it's disabled if it's enabled reconnected configuration changed there's lots of different ones but the most common ones are disconnected in case you need to pop a message to say it is gone or you need to pause your game then we'll add a debug log which says that the device is disconnected and then we'll add the device plus name just like we did before and then we'll add a break and then in here we can add some code to handle disconnection you can pause the game here and bring up a popup on the screen or in our next case we need to say the input device change do reconnected we can write almost the same debug line and statement below say that the device is reconnected going to make sure that I don't print all my devices now so now I'll press play and I'm just going to unplug my controller and you can see there that my controller has been disconnected because it says controller Xbox controller is now gone so like I said I could pop up something in the corner to say that it's disconnected and I just plug that controller back in and you can see that reconnected is my Xbox controller so say that's reconnected we can then play get rid of the popup and res resume the game again and for this part of the tutorial I'm just going to use the 2D simple character swordsman which does already have its own character controller set up but it's just literally a base I'm going to remove all the scripts and we'll write it ourselves to create the basic input and actions that we need now I'm going to give you a smaller example of how to set up your own 2D controller and use inputs with that as another example of how to use the input system cuz I know a lot of people will want to use 2D as well because we'd already set up our player controls for a 3D character and please refer back to that if you want more details about it but what I'm going to do is I'm just going to right click my action map for player duplicate that and I'm just literally going to call this my 2D player because you can have multiple like I said now 2D player looks almost the same but in our case we don't need look anymore cuz we only need to go left and right and probably jump cuz most 2D games are just sides scrolling so on the Move we can still use the left stick and we can still use the 2D vector and it still has an ction type of value and it's a vector 2 but on the vector 2 we don't need W to move up because we'll be jumping so we can just delete that we don't need s cuz we're most likely not going down so we actually only need a and d now because A and D would be left and right jump would still be space bar and Sprint you could still have it with the exact same things with the left shoulder button and the left shift so it's the exact same setup as what we had before and for those that might be looking at just this 2D player for the first time I'll quickly run through the overall bindings just so that you make sure that you'll have everything so I've got an action and if you need to add an action you can press the little plus I've named it to move and it's just a value and a vector too you can add bindings by clicking on the plus and add binding or and add an up down left and right composite if you need to use different keys for A and D so the left stick is just the path for left stick you can go to game pad and find left stick the 2D composite is you want left and right and you can listen on the keyboard and press the key that you want and add that jump is just a button and that has a new binding of button South and a binding of space Sprint is also a button which is the left shoulder on the game pad in my case and left shift on the keyboard so now we have that set up in my scene with my 2D controller I'm just going to create an empty I'm going to reference my player input Handler that we created before because we can add this between different controller types but in in this case the action map should be just 2D player in this case as an example and you make sure that you add your player controls you can open that back up you won't need look anymore so we can remove look the look action the look input in the awake remove the finding the look action and then registering through the vent we can take off the look make sure we also take it from enabled and disabled just run down the script and get everything related to look because we don't actually need it anym and also we don't need anything to do with handling the rotation at this point then from there what I'm going to do is choose right click C and I'm just going to name this my animated controller and then I'm going to add that to my swordsman and my swordsman's set up in a way where it's got a rigid body and it's got a captal collider 2D just so that it can take physics into account and we don't clip through other objects I can add my animated controller here I'm going to start by writing our controller script very similarly how we did with with the firstperson controller if you watch that so we're going to have the movement speeds that we need so I'll write serialized field private float as the move speed set that equal to five as an example you can adjust all these again you can go serialized field private float Sprint multiplier set that equal to 1.5 F then we'll add something for the jump which is a private float jump force and then we're going to need a reference to our rigid body which is a rigid body 2D and we'll give it a short hand of RB then we're going to have a reference to our player input Handler which is just lowercase player input Handler because we're going to then create a work method to find these so private void awake then we'll say RB equals get component in angle brackets rigid body 2D we find that and I'll just make this inut handle it in lower case just so it's consistent with my other script so I'll say input Handler equals player input handler do instance so then we're going to have an update method so we can start checking for some of the inputs that we need at the top I'm going to create a float for the horizontal input and then in my update method I'm going to say that the horizontal input is equal to the input Handler dot move input dox because we only need to know where the character is moving at any time whether it's left or right so we're going to need to use the x axis then we might want to check something for whether we should be jumping to make sure that we check that and check the ground and I'm just going to add something in this script to flip my Sprite just when it comes to moving left or right so it makes sure the Sprites looking the right way then because we're working with physics we want to use void fixed update this will help us keep this in a fixed interval to be able to update physics I'm going to create a method called apply movement and then to do with our jumping because we need to make sure that we can control how the jumping works and we need to make sure because it's using the physics of the rigid body we need to make sure it's fixed that new method of apply movement float speed is equal to our move speed Times by and if you saw my other part of the tutorial this is very similar input handler do Sprint value is greater than zero question mark Sprint multiplayer colon 1 f and end that bracket this just means that our speed will be equal to our movement speed which is Whatever It Is by default and then we'll look for the button or the Sprint value that button that we're pressing and if the value is greater than one on our button press we must be pressing a button and if that's the case we'll be sprinting if not we must not be pressing the button cuz it's less than zero so then we'll be just doing our move speed Times by one which will always be the same as move speed cuz anything times by one is the exact same then to apply this to our rigid body we'll say rigid body do velocity equals new Vector 2 and then we'll say horizontal input Times by our speed comma rigid body do velocity doy because we need to push the character in that direction now if we create another method called apply jump in our case here cuz we want to take the jump into account we'll say the RB do add Force Open brackets Vector 2 do up Times by The Jump Force comma Force Mode 2D do impulse which just helps us apply the false to the rigid body we'll say grounded equals false just for now just so that we know and should jump will be equal to false as well because we don't want to be able to jump again now we want a quick way to detect whether we're in a to check whether we're grounded because we don't have anything built in to check for this so we'll write a new method called void on collision enter 2D it's already filled out the parameters Collision 2D and then we'll say that if the collision. game object do compare tag is in Open brackets quotes ground where you can set these tags yourself then we'll say that is grounded is equal to true now I'm going to quickly add a method just for flipping my Sprite just transforming the local scale to make sure that it works fine and then we need to create ourselves a is grounded Boolean right at the top I'm just going to add is grounded and I'm going to create another called should jump so if we go back to where we were before to check for jumping we'll say that to detect the input we'll say that should jump is equal to input Handler dot jump triggered and and is grounded for the horizontal input we're just looking for the move input X and for jumping if the buttons triggered then we'll do it and of course for the sprinting we look for it in times it by the move speed in our method which was the fixed update we need to say that if should jump is equal to True at any point we should apply the jump method and now we can say that is grounded is equal to false and should jump which should be equal to false and before we've got is grounded so we know that we're on the ground we can apply the jump and the force and we've used our new inputs directly in here referencing that input manager that we' created above the player input so we can go go to the swordsman and we you make sure you've add the animated controller script here and one thing to mention if you do get a sort of choppy looking character controller you could change interpolate to actually interpolate just to make it slightly more accurate make sure you have your player input Handler game object with everything set up and I will say if you ever get any null reference exceptions about certain bits of the input not working you can actually add your input Handler reference finder into the start method because sometimes Unity doesn't build fast enough and when this gets created for its single turn the player input Handler it sometimes doesn't create it fast enough so if you put it in start it will be after the original work method so then you will solve that issue so now you'll see when I press play I can move left right I can jump and I can also pick up my controller move left and right with my analog stick junk with X and then also Sprint so I can move faster when I hold my top button so I can jump around and do everything that I want and make sure to set the tags of your Sprites just to make sure that you are grounded at any time to make sure that you don't have any issues and I remember it'll all be up on my patreon to get this entire project all the scripts and everything that you need to get yourself started so I wanted to go into how to navigate uis with a controller now I'm just using a mouse here and it works normally by default when you add your own canvas I'm trying to use my controller now and it doesn't navigate anything now you might ask how we do this if you think about it when you right click and you choose UI and you choose to create yourself a new canvas you will get an event system that goes along with it now the event system was used originally to be used with the original Legacy input system you can see the standard alone input module is the old one and it needs to be replaced with the input systems UI module now you can see here if we click to replace it as long as the input system is installed we get a whole list of different actions that we can perform whether that's point at left click middle click move submit and cancel now if you see what the actions asset is currently linked to it will be linked to one that's in your packages that that's the default one by unity that they've created which has just some basic stuff for player and it's got some basic things for UI with everything set up where you could see the the navigate you could see the submit button cancel and it's got everything set up as we would want and you can look at these more deeply yourself what we can do to make life easier is just right click and copy that action map from the unity's default one that they created and go to our player controls option and right click on our action map area and just choose paste and now we've pasted in an entire action map for UI already set up and ready to go so as long as you've saved that out you can go back to your event system and you can look for another actions asset and I'm going to use player controls and because we've added that UI it will look for UI Point UI click and of course you can choose between the different maps with what ones should be relevant to these if you you so need to change them but if we press play it still wouldn't be able to navigate them because here you can see there's a button in the event system called first selected and you always need a button here when you open a new menu so let's say this is our first scene here we need to open up and I need to select the new game button and I'm going to add that to the first selected slot you can see that I've set this highlight to when it's selected to Blue now if I grab my controller and I navigate with my directional keys or my analog stick I can move up and down however I want and I'm just going to quickly make a mention about navigating buttons as well and I do have a full main menu tutorial showing you the intricacies of navigation and setting up me menu which I'll put in the description so if we select this new game button you'll see that it's on automatic by default and you see on the visualize you will visualize little yellow arrows in between which will show you how it will navigate from moving up and down down so you can see the automatic menu game will let you move to load game and back up again now if you want to be more specific you can set this to explicit and make sure you want take everything else and then you can say from my main menu what do you want to do when you select up so realistically we could add our exit button to the top so you can see now this Arrow knows that if we press up we can go all the way down to the bottom because it's quite a nice UI feature and if you select down you could move to the load game and you can see that it connects that Arrow and if you move left and right it doesn't need to do anything so you can just leave those by default and this just allows you to navigate them more easily so you might ask me that when we can select a button how do we know when we switch to a new menu where we disable a panel and enable another one how can we update this first selected box in code to make sure it happens so I've just created a script called menu navigation and I'm going to make sure that I am using Unity engine. Event Systems at the top that namespace and they'll just write this in a start method and I'll say that the event system do current. set selected game object and then we can specify a just a new game object here which is going to be the default selected button and then I'll make sure that I save it out and this is a way that you can apply every time you navigate to a new menu and as I said this is fully explained in my full main menu tutorial so you can use this even in the new input system I'm just going to add this to my event system I'm going to choose my first selected object this is just an example of what we can use in this case I'm just going to set it to the load game button so even though we've got the new game button in the event system at the start the script will override that so when we press play we expect to see the load game button highlighted on selected so as you can see the load button was selected cuz we specified that in script now we've got one problem which you might have noticed let's say we're navigating nicely with our UI but because we're on PC this is not so much a problem with console development because you'll only be using a controller but let's say we can take control with our Mouse and start selecting things when we go back to our controller input we can't actually do this anymore so but we set that new game Button as the first one but Unity forgets which one was the first selected in this case so we can add a little bit of script to make sure that we can always navigate back if we use the controller again so if I go back to my menu navigation script I'm just going to write an update method to look if we've got a controller Gamepad connected and then we're going to update the button for ourselves so in this case if we go if Gamepad do current is not equal to null so we need to make sure that we've got a gamad connected and then we'll say gamad do current dot was updated this Frame and and event system. currentcurrent selected scene current selected game object is equal to null then we'll make sure we close that off and then do exactly like we did above we'll do the event selected and up to there and make sure that if you were navigating your menu you make sure you keep track of what page that you're on at any time and you make sure you update both of these in your scripts so now when we press play we can see that we're on load game you can see that I'm selecting and I'm navigating with my controller now if I grab my mouse I can navigate and I can click but I can navigate to other things now you can see if I let go of my mouse and start interacting with my gamad it will see that I can start unload game again so we always remember where we were just so that we make sure that we can use it again so I think this is really important to know and it's a lot of things that's not really covered you can always comment down below if you've got any other suggestions to fine-tune this make it better and share with everybody else and make sure that they can get the best tips from you guys because I know there's a million ways to doal it everything I wanted to show everyone something very useful that not many people know exists and now it's Unity samples for the input system we will look at one of these in the tutorial but these might help you out so if you navigate to window and package manager and when you're on the package manager if you go to Unity registry is if you were installing the input system search for input and you can see at the top you've got description version history dependencies and samples click on the samples and there's loads of different samples which have got various different demos that Unity have created themselves for you to be able to get different inputs so you could use a gamepad as a mouse cursor and that's got its own example you can have in-game hints an input recorder onscreen controls a rebinding simple demo with a controller simple multiplayer you've got touchscreen samples UI versus game input Unity remote and all you have to do is click to import one of these assets and then it will be added to your samples for Unity input system for whichever version you're using and it will have a demo scene you may well need to upgrade materials if you're using a different version like I'm using urp here if we take a quick look at the UI hints demo you can see that we have got examples you can see the text in the corner these are really really basic demos really some of them are a bit thrown together quite quickly and it does have a ways to show hints and stuff when you're using this system these come fully with their own actions so you can look at how they've been set up and the entirety of their own scripts which will show show you how these work in their examples so we're going to take a look at the rebinding package by unity in their samples and like I said before you go to window package manager and if you make sure when you're on package manager you go to Unity registry then make sure you search input go to the input system like you're going to install it if you navigate to samples you can go to their rebinding UI and you can just choose to import that package then you will see it on the right hand side in your project panel under UI or rebinding UI they come with a bunch of icons which are actually quite useful because you could use those in other instances of your game depending on the controller that you're choosing to use and it comes with just the example scripts it's got some readmes and it even comes with save and load logic it's got a rebound UI prefab so you can add it to your own scene it comes with its own example assets so it's got some gameplay of just move look and interact we'll open up the demo scene I'll give you a look at it it doesn't look particularly pretty but as you can see here it has the ability to rebind the controls and this is a simple rebind and we'll go through the look at how it's set up so you've got move here and you can select any of the actions and from there you can say what binding you want for the up Axis so I could use the directional arrows and then for down is the down arrow left left Arrow right is the right arrow so you can see it's up down left right because it understands which one interact on my key keyboard could be F and then on the game pad we could select something weird like the right un log stick for moving left unlog stick for looking that would really confuse people and then maybe an x on the Xbox Gamepad to switch it to that and it does say in practice you probably don't want to be able to let people remap your the sticks because that's going to really break things and I just exited it and opened it again and you can see that it's saved my change is exactly what I had and you can click reset if you need need to go back to the originals and you can see it's set up with a canvas and on the canvas you've got Gamepad and you've got keyboard so let's go to the game pad in as example you can see them on this side so we'll just look at the interact because that's easy to understand the interact there it's made up of an image and a rebound action UI which they find which actual binding it should be based on which one you've got for that particular type you could choose to emit it if you don't want it to to be viewed and then you can set the labels that they should be to each of the different prefabs and then it's got a trigger and a reset button for each of the buttons so you can clict that the rebind action UI does the start interactive rebind and if we go back to the object and we open up rebind action UI and you can see here that the start interactive rebind as finds the action and The Binding index searches through the different Bindings that we have and then we'll perform the interactive binding so that we can change what should happen and you can see this you could just duplicate the button if you wanted to create more then you could just add that below if you have other actions and because this action here because this was from the game play action that we'd already created and this does have other ones that I've got in this project you could specify something like the game play drop if you had this available you could select that and you can change the binding depending on which one is specific to say that if it's normally a game pad B you can see that it automatically fills in and we'll be able to reset this and apply this to our scheme and it does have saving and loading and you can add this entire prefab into your scene to get a nice startup on using controller rebinding and I did also want to mention the unity have got some brand new AA collection for urp the starter asset character controllers which are two sets of character controllers for Unity 2022 and above using cinem machine and the new input package and it's got a full starter package for a first person and a third person controller full UI overlays for joysticks and touch screens it's got a playground scene and basic implementations of two controllers that you can use and look at how it's set up and I'll leave the link to that in the description too I do also have a couple of tutorials which are about remapping this basic third person character controller and using your own player models whether use CN assets or something else you can retarget the animations really easily and I'll put the links down below too and after this long tutorial if you stuck around with me I want to say a big thank you for everybody for coming to watch and do be sure to leave a like and a comment because it really helps me out and leave any thoughts down below if you've got any improvements anything that you could share with the community because I always love to hear it and do be sure to check out all the links below to check out all the savings across Game Dev and the new Unity sale and do be sure to check out my patreon which will have every single one of these assets all my projects and all these scripts that we've created for today big thank you to all my patrons special thank you to Peter Steiner and everybody else who comes to watch the video so don't forget to like comment and subscribe cheers
Info
Channel: SpeedTutor
Views: 6,970
Rating: undefined out of 5
Keywords: unity3d, unity3D, unity3d tutorials, unity input system tutorial, unity new input system, how to use controller in unity, unity new input system 3d movement, unity new input system 2d movement, speed tutor unity, speedtutor unity, unity, new input system unity, unity deadzones, new input system, gamedev tutorial, unity tutorial, invert controls unity, gamepad unity, gamepad mobile unity, unity2d controller, unity2d tutorial, unity3d tutorial, new input system unity 2d
Id: lclDl-NGUMg
Channel Id: undefined
Length: 62min 44sec (3764 seconds)
Published: Sat Jan 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.