Add Gamepad RUMBLE with Input System | Easy Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome a really nice way to add some juice into your game is to add controller Rumble if you don't know anything about unity's new input system don't worry I will walk you through the whole process so here's our end result we hit a button on the controller and we get some rumble I know you can't see it but hopefully you can hear it let's get started okay and here's my very simple scene you can do this with a completely blank scene I really just have this stuff in here so that it's a little less boring for you to look at so I'm going to walk you through this from start to finish so we are going to start by installing the input system package let's go over here to window package manager let's search for input system find input system and let's click install all right [Applause] okay so there we go so let's get the input system actually up and running so to start off I'm going to go to my root assets and create a new folder called input actions let's go in there and let's create a new input action and I know this is cut off but it's called input actions it's right at the bottom here let's call that controls okay so we want to select this generate C sharp class here and click apply you can see once you did that that there is now a controls C sharp class here so now to open this up let's click edit asset and let's dock it up here so depending on your game setup you might want to create a whole bunch of action Maps like one called movement one called jump etc etc you can set up as many action Maps as you want and then you can have specific bindings inside of each action map so I will come back and explain this a little bit more once we are able to access this stuff through code it's a little bit easier to understand what's going on here when you're able to actually access it through code so for now let's set up two control schemes click here and click add control scheme let's say keyboard and click save and let's create one more click up here click add control scheme and let's type in Gamepad okay so let's go over here and select all control schemes now let's create a new action map and call it Rumble let's rename this action to rumble action let's change the action type to a pass-through control type of button is just fine let's click over here and click add binding so we're really just setting up some test buttons right now that will allow us to when we press these buttons when our game is running it will Rumble the GamePad so I'm just going to go ahead and make this our keyboard control scheme and let's say number one on our number pad and for Gamepad I'm just going to select the right trigger and add that to our Gamepad control scheme let's click on Save asset and now let's create an actual input manager so that we can actually detect when we press these buttons in our game let's go to scripts create a new folder called managers and inside of there let's create a new c-sharp script called input manager let's create a new empty and call it input manager let's reset the transform I'm going to move that up here and let's add input manager to that and let's open that up let's get rid of all the startup stuff now I almost always set up every type of manager that I put under managers here as a Singleton when I am coding it and if you don't know what that means I'll show you how to do it but basically if you set up a class as a Singleton class we will be able to call public functions from this script without having to grab a reference to it anywhere else we'll just be able to call them directly you do want to be careful when you set up Singletons because you only ever want to have one instance of this script and you're seen anywhere ever that's why it is a perfect use case for managers because something like input manager where we are detecting our input we're only ever going to need one of these in the scene ever so in order to make this a Singleton let's say public static input manager let's call it instance and then let's create an awake function and inside of there if our instance is equal to null then instance equals this and that's it this is now a Singleton class now we want to grab a reference to this input actions control trolls script that we just set up so let's say public controls let's call it controls and we don't actually want this to be showing in the inspector so let's go ahead and say hide in inspector that will make it so you can still access this from other scripts but we won't need to actually set it inside of the inspector if we're not setting it inside of the inspector we will need to set it in our awake function here so we'll say controls are equal to new controls now we need to properly enable and disable this as well and this is just the way that unity set this up behind the scenes so you'll need to do this every time you set up new controls so inside of our on enable function let's say controls dot enabled and inside of our on disable let's say controls dot disable and that's actually all we need from this script now that we can actually properly detect our input from this controls input actions here now let's create a new script in order to actually call a rumble function on our Gamepad so let's go into scripts here let's go into managers and create a new script called Rumble manager let's create a new empty and call it Rumble manager let's reset the transform let's move it up here let's add Rumble manager to that empty and let's open that up let's get rid of all the startup stuff now this is another manager and this is another script that I would like to be able to call from anywhere and I will never need another Rumble manager in my scene anywhere so let's make this a Singleton as well there we go now let's set up a new public method called Rumble pulse and we're going to keep this very very simple but basically the way that a rumble works is most game pads have two Motors inside of them there's a left one and a right one the left one is usually set to a low frequency and the right one is usually set to a high frequency so we're going to need float values in order to set the frequency on both of those Motors so we'll pass in a type float let's call that low frequency and one more float called high frequency let's pass another float called duration so that we can directly control how long we want this Rumble to last so first we need to get a reference to our gamepad and in order to do that we need to go up here and add the unity engine.input system namespace let's add it up here we'll say private Gamepad let's just call it pad down here we'll say pad is equal to Gamepad dot current and before we call any kind of Rumble function let's make sure that we actually do in fact have a reference to a Gamepad and we'll do that by saying if had does not equal null then if that's the case we want to actually start the rumble and on top of that after our duration is done right here we want to stop the rumble because if we don't do that if we don't stop it ourselves then it's just going to rumble forever and it's going to kill your controller battery really fast so how do we start the rumble well this is super easy we can just say over here pad dot set motor speeds what does it want it wants two floats our low frequency and our high frequency so let's pass in our low frequency that we set up right here and our high frequency that we set up right here and in order to stop it after a certain amount of time I like to use co-routines for this kind of thing so let's go up here and create a new reference to a co-routine called stop Rumble After Time co-routine now let's actually create that method down here we'll say private I enumerator stop Rumble so let's set up a float called elapsed time we'll default that to zero and we'll say while our elapsed time is less than some sort of time so let's pass in a float duration up here so while elapsed time is less than our duration and we want to say elapsed time plus equals time dot Delta time so this will keep track of the amount of time since we called this function exactly and then say yield return null so what this here essentially means is as long as this statement is true as long as elapsed time which starts at zero is less than our duration it's going to keep running this over and over and over and over so it's going to update our elapsed time and yield return null just means wait until the next frame so each and every frame is going to go in here and update our time once our duration is actually over what do we want to do well we want to stop our Rumble so we need to get a reference to our Gamepad so we can add that as a parameter up here say Gamepad called pad now down here we can say pad dot set motor speeds 0 0 and now we need to actually call that so let's grab this up here copy that go down here and say that is equal to start Co routine called stop Rumble pass in our duration and our pad okay so we have our Rumble manager all set up so now we just need to actually call this from somewhere so back in my scripts folder I'm actually going to go ahead and create a new folder called test and in there I'm going to just create a new script called Rumble test and I'll add that onto an empty as well make that and drag that on there and let's open that up now remember how we set up our input manager as a Singleton well here's why that is so cool so in our Rumble test in our update function we want to be looking for an actual button press so what we can say is if input manager which is this class here dot instance dot controls dot Rumble dot Rumble action dot was pressed this Frame then we want to call our Rumble function so we're grabbing a reference to this input action asset here where are we looking we're looking inside an action map right there that we called Rumble and then we're going into our actual action which is called Rumble action and then it's checking if either of these were pressed this Frame so this is why the new input system is so awesome because we have a keyboard and a Gamepad set up so we can use both and no matter which one we press this is going to get called so in order to actually call the rumble let's say rumble manager dot instance dot Rumble pulse and let's just say 0.25 in low frequency and one in the high frequency in my testing you can put these as high as you want but one seems to be the max so it doesn't matter how much higher you go it's the same as one so really if you want full power you set this to one if you wanted something less then go with something less and now we need to pass in our duration as well let's say a quarter of a second let's go ahead and test that okay I just connected my Xbox 360 controller so if we press play now you can't see but you'll be able to hear is fine if I press the right trigger on my gamepad we got a little Rumble going and if I press the number pad 1 on my keyboard then we also get a nice Rumble now I set it up this way just for testing purposes so that you can kind of see how to go about setting up new input actions and how you can do it with multiple control schemes but this is just an example and in a real game type example this doesn't really make a whole lot of sense we would not want to be rumbling our Gamepad based on a keyboard input so how do we deal with that because right now the way that we have it set up it doesn't matter what control scheme is currently active as soon as we call this it is going to rumble the GamePad so let's say for example I call this whenever I damage an enemy I definitely don't want Rumble pulse to be called if I don't have a Gamepad hooked up that's just weird so how do we deal with that well we need to add some checks in here and in order to do that what you are going to want is to go up here to your input manager and you're going to want to add a player input this is a script that is included with the input system package so this has a whole bunch of functionality but what we are going to use it to do is I want to know when do we change our actual control scheme this is going to allow us to do that very very easily so to get that working let's first go over to our actions here and let's find our controls that we set up the default scheme here we can pick between any of the ones that we set up I'm going to leave it at any auto switch let's leave that checked and default map you might have dozens and dozens in here so let's just select none we don't really want to default and you can leave UI input module and Camera blank we don't need those so in order to detect when we change our control scheme and what that literally means is when we change between Gamepad or keyboard control schemes over here I'm going to change Behavior to invoke C sharp events now let's open up our input manager and just grab a reference to this because we already set this one up as a Singleton which is really nice and convenient so let's just make it so that we can call player input from this script very easily so what we'll do is we'll do another hide and inspector and we'll say public and actually in order to access it we need to go up here and add the using Unity engine.input system namespace and now we can go down here and say public player input let's call that player input and since we just attached it to the same game object we can just go into the awake function and say player input equals get component player input okay let's go back to our Rumble manager now in order to detect when we are switching controls I'm going to go down here and create a new method called private void switch controls we are going to need to pass in a player input let's call that input now let's throw a debug.log in here just for testing purposes let's say device is now plus input dot current control scheme okay so how do we actually call this so we set Behavior to invoke C sharp methods which means that we need to subscribe to the on controls changed delegate so I'll explain that in just a second first let's go back to our Roma manager and go up here so what we're going to do is down here at the bottom of our wake function we grabbed a reference to player input inside of our input manager so we're going to say input manager dot instance dot player input dot on controls changed plus equals switch controls okay so what we are doing is as soon as awake is called which is right at the start of the game we are going to subscribe on controls changed to switch controls every single time Unity detects that our control scheme has changed it is going to call this function which we just set up down here delegates come in really handy when you don't want to be searching for something in update every single frame we don't need to search every single frame in our game whether we have switched controls or not what we can do is subscribe to this delegate that Unity has already set up for us and it will just say hey our control scheme just changed do we have anything subscribed to this delegate if yes it will call that method so as soon as you press a button on a new control scheme it is going to switch to that control scheme so if I have a player and I'm moving them around with my keyboard and then I plug in my Gamepad and I start moving that character with the GamePad instead then we have changed our controls and this should get called so let's hit play so this is actually something worth noting I just made a mistake we're getting a null reference exception here that is because in my wake function in the rumble manager I'm trying to subscribe switch controls to on controls changed in the awake function but in our input manager I'm only just grabbing the component player input also in the awake function so what might be better is if I added this into the start function which always runs after the awake function in the rumble manager so let's do that there we go that message is gone so now that we have subscribed switch controls to this on controls changed delegate event we want to make sure that we unsubscribe it if this Rumble manager script ever gets disabled as well that's very very important so let's go down here at the very end and say on disable let's just copy this and paste that accept plus equals we're going to change to minus equals so if this script is ever disabled then we will unsubscribe switch controls to the on controls changed delegate event so I'm pressing some keys on my keyboard which means we are using the keyboard control scheme and now I'm picking up my Gamepad and it should be displaying a debug message but it is not why well actually if you look over the inspector here you can see in your player input there is a debug that opens and it's actually showing keyboard control scheme over here and it doesn't seem to matter whether I'm pressing buttons on my Gamepad or not it is not changing the control scheme the reason for that and I did this on purpose so that you remember because it's very easy to forget is when you're setting up your control schemes inside of your input actions asset over here what you want to do and let's let's go to Gamepad here and let's say edit control scheme you have to make sure that it is a required control scheme it has to be required or this is not going to work over here so for our Gamepad let's click Gamepad and click Gamepad and we'll say that is required let's save and do the same thing for our keyboard go there edit control scheme keyboard required save make sure you save your asset now let's try that so if I press the button on my Gamepad you can see devices now Gamepad and if I press anything on my keyboard devices now keyboard if I press another button on my Gamepad device is now Gamepad if I press something on my keyboard etc etc you get it so that is how we actually determine which control scheme is being used so we don't need this debugged log anymore so let's create a private string so that we can hold a reference to what our current control scheme is so we'll say private string current control scheme and let's just set this to whatever the current control scheme is whenever we call switch controls so let's say current control scheme is equal to input dot current control scheme now over here in our Rumble pulse function let's actually do a check to make sure that our current control scheme is actually the GamePad so to do that let's just say if current control scheme is equal to what does it need to be equal to well what did we type over here we called this Gamepad make sure you type it exactly as it is over here if current control scheme is equal to Gamepad then we will run all of this so let's press play and even though our controller should be rumbling based on these inputs it now should not work when I press number one on the keyboard because we want to make sure that our current control scheme is equal to the GamePad control scheme okay so if I press the right trigger still working just fine but when I press number one nothing happens so that is working as intended awesome there are other ways of doing this as well if you do not like the delegate method then you can go over here to behavior and you could choose say invoke Unity events and if you expand events over here you can directly assign something to this controls changed event here which also makes it very easy and if you don't want to invoke Unity events you can also try broadcast messages what this will do is this will broadcast a message to this game object here so if we create a method called on controls changed just like it is spelled right here which I have set up right here on my input manager which is right here and that's fine as long as it's on the same game object as the player input so I made that a public void on controls changed and I just threw a debug.log test in there so if I wanted to use broadcast messages instead there's your test and if I use my Gamepad we get another one if I go back to my keyboard there you go so they give you lots of options in terms of how you want to go about detecting your control scheme being changed I'm just going to put this back to invoke C sharp events that that's all I got for you guys thank you so much for choosing this video to learn how to rumble your Gamepad in unity it means the world to me and please let me know in the comments down below if you have any ideas for future tutorials you'd like to see thanks and I hope you have an amazing day
Info
Channel: Sasquatch B Studios
Views: 6,343
Rating: undefined out of 5
Keywords: unity tutorial, unity 2d, unity gamepad input, gamepad input in unity, gamepad rumble, controller rumble, unity gamepad vibration, controller vibration, unity gamepad rumble, unity gamepad rumble tutorial, rumble tutorial, unity rumble tutorial, unity controller rumble tutorial, make controller shake in unity, make controller rumble with unity, make gamepad rumble with unity, make gamepad shake in unity, unity controller shake tutorial, unity gamepad shake tutorial
Id: SmmBC-yCJ28
Channel Id: undefined
Length: 18min 15sec (1095 seconds)
Published: Thu Jan 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.