Enhanced Input Unreal Engine - Simple WASD Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so let's take a look at the enhanced input uh system in Unreal Engine uh the enhanced input system is the the new way to handle input uh and it's a really really cool and flexible way of of doing interesting input things uh but it can be a little bit intimidating get started with it and the the documentation that appic has on their site uh even though it's pretty good uh it took me a couple of tries to actually understand how to do something practical with this so in this video I'm going to walk you through uh basically how to create an action how to create an input mapping context and basically at the end of it we're going to have set up the WD controls for a simple Pond uh and you should be set to uh continue on uh making your own cool and interesting inputs let's start setting up the uh kind of the fundamentals for this example so I am going to start by creating a few blueprint classes uh we start with a player controller this is where we're going to going to be accepting the inputs BP player rer uh I'm going to create a character which is a type of Pawn that can accept input so we're going to be creating the inputs at the uh um for a character using the enhanced input system BP character and then I'm going to create a game mode uh which is going to allow us to spawn into into a world where we have our character and our player controller set up so I'm calling this BB game mode all right so I'm opening up the game mode and here you can see that uh there are the the class defaults here I'm setting the player controller class to our blueprint uh the BP player controller here and I'm setting the default Pawn here to the BP character and then lastly I'm going to be setting the game mode for this level I'm going to set that to the bpk mode and now that I press play uh you should see that I spawn into the world and here in yellow you can see that we got the BP character blueprint added uh the BP game mode and and the BP player controller so now we have everything uh when I try to move around pressing the the arrow keys nothing happens or the vast keys so the next thing is we add the uh the enhanced inputs so now that we have everything set up uh let's go ahead and create the two uh the assets that we need for the inputs we need the input mapping context so let's create one of those the input mapping context is uh a data table uh or data asset that links together uh it links together actions that we're going to create earlier and then inputs so let's call this input mapping um and I'm also going to go ahead and I'm going to create an input action and I'm going to call it AC uh for Action uh and I'm going to call it move so it goes over here so uh let's just start by adding the uh in the input mapping uh we're going to add one entry of uh of mappings we're going to say uh it's going to be this action so the AC move action is going to be linked to uh in inside of this input mapping table and if we take a look we can see we open this up here and we can see that this is uh letting us pick a letter or Pi pick an input so we could have a game pad or a keyboard or a mouse so we're going to uh link this to W because we're making the V uh kind of inputs and here we have two things we have the triggers so if we add a trigger we can say when does this trigger do we want this to trigger when the a button is pressed or when it's held pressed or if it's down or do we want it to pulse so this is one of the uh kind of the more cool things about the new enhanced system you can do uh things like that much easier than you could in the old system so we're going to say when the button is down we want this to trigger uh and we don't don't need to add any modifiers uh to this just yet so uh now what we've done is we've created a link we've hooked up this uh We've created an entry in the mappings list inside of the input mapping and we added the input action AC move uh and we've added the W button to uh to trigger when the W button is down so uh all we have to do now is we need to make this uh make this do something so we go into our player controller and uh let's turn this off we go on to our player controller let's take a look at the event graph and on begin play uh we need to get the local player input uh input subsystem so uh so we what we're going to be doing now is we're going to be registering the input context so all of the inputs that we've uh all of these inputs that we've uh defined and in our case this is just the W uh the W input uh that triggers the input action uh AC move uh we want to register that so to make it available so we do register uh no so it's not called register so this is something that I uh uh yeah I run into problems with sometimes it's called add input context add mapping context so here basically what we want to do is we want to say uh that when the game starts the player controller triggers its begin play logic we want to add a mapping to context and add mapping context is a function on the enhanced input local player subsystem and we want to say that the input context that we uh are going to be kind of registering or adding is the uh is the input mapping asset that we defined over here and if we take a look at that again we can see that it has one action AC move and this action gets triggered uh when the W button is down so now if we wanted to test to see if this actually uh Works uh we can go and we can right click here and we can just type in acore move so here we can see that uh I'm going to just do a print string here uh and we're going to print when the action gets uh triggered here we go and it's going to say hello when it's triggered so now we press play and I press the W button and you can see when I press the button and I hold it down uh you can see that the Hello message is being printed on the screen so now what we've done is we've added uh We've added this uh a mapping context which we just created we created an action and we defined how the action should be uh how it should be triggered and we printed a string with the uh yeah hello world string uh to test it so the next step is going to be expanding our input action the move action uh so that it can actually do a couple of more things uh we want the input action to be triggered when we uh touch all of the the was keys and we can actually do that with the same input action so if we take a look at the action here uh the first thing that you see is that the value that uh is being that is being generated by this action so whenever we uh we press the button you can see it's giving us a Boolean value so it's going to tell us is the button down or not uh so this is uh this is pretty useful for for many things but what we want to do is we want to uh we want to do a positive or A negative number we want to see are you moving forward or you moving backwards and then we want to map that both to the W and the S key and then eventually we're going to do in the A and uh and the D key uh for left and right so let's switch this over to a 2d axis and you can see that the moment that I did this uh we we can see some changes here in the uh in the uh the event here where we're kind of responding to the action you can see that uh we're not let's just take a look at this again so if I go back to the to the Boolean value you can see that the action value here that's getting outputed is a Boolean uh a Boolean value but now that I move it over to the the 2D Vector you can see that the data type here for the action value has changed and I can go in here and I can break this because now this is a this is a vector two so uh but pressing the W button doesn't really uh isn't really it's just generating a a a Boolean basically just generating a Boolean value so let's just see what happens if we plug this into the print statement uh and we just run this as is so now you can see that whenever I press the W button I'm getting one on the screen uh so this is great uh I am going to uh so I know that I'm getting a positive number here uh whenever I press the the forward button but what I want to happen is when I press the the backwards button or the S button I want us to get a a negative number so we're moving on the x axis so you can see down here uh we're eventually going to create a character that uh goes back and forth you can see that uh when I press the W button I'm getting a positive x value so now uh let's jump in here again let's add another input for the move action and we're going to say s and look for the S button here uh we're going to do the same thing we're going to make the trigger B when the button is down or where the action is down but in this case we're going to go ahead and we're going to add a modifier so the modifiers are pretty cool so they allow us to take the input action or take the input uh the value from the action so in our case it's a vector uh it's a vector two uh and it's going to allow us to uh do things like just basically modify it like this is a modifier so the modifier that we want is that whenever we press the S key we know that by default if we don't do anything we're getting a positive number we're getting a one but we want to add a modifier called negate and negate is basically inverting it so once we do that we go ahead and uh now we have the input action mapped to two buttons we have the W button and the S button uh when the button is down uh this is the default behavior and when the uh when the S button is down when the W button is down we do the default behavior when the S button is down we do the same behavior but we negate the result we modify the result and just as a reminder we take a look again into the action here the action returns a value of an uh of a 2d Vector which basically means that now that we uh we play now that I press w we get uh one and when I press S I get a negative number so now we're actually ready to um uh implement the other uh the other two uh the other two values but before we do that let's uh do a little bit more work and add add in the the character so that we can actually see what we're doing so uh we were going to we were going to do a little more work on the on the character just so that we can see it so I'm going to open up the character blueprint here you can see that it has a collision and uh it has this character movement component which uh which we'll use to uh to move our character but I'm going to start by adding a sphere uh just so that we can visualize it a little bit and I'm also going to give it a camera so uh and I'm going to move the camera back a little bit and I'm going to move the rotation of it a little bit like this so now without having made any any changes uh when I press play you can see that I have this uh like my Pawn is now visualized with this sphere and the camera is a little bit uh littleit behind behind it so now uh we're going to forward our uh our input into the uh into the movement of the character so uh all we have to do then is within our player controller we can call the function get controlled Pawn which is going to give us the uh the pawn that uh that that's actually kind of controlled by this player controller and earlier we set up the game mode so uh so the game mode itself sets up the uh uh kind of the relationship so we know that the controlled Pawn is going to be the pawn that uh uh that we want the character and all we need to do is we do add movement input here movement input takes a vector three so it has an X and Y value forward and left and right and then has a z value which is up and down uh we don't want that uh so we split this one here and now we can feed in the x value so that's going to be the the movement Direction here uh so now we don't we shouldn't really need the uh print statement anymore so now with this wired up we have uh we should be able to see our character move forward and backwards it's kind of cool so now that we have the uh the visualization of this setup uh it's time to add the uh the left and right values so now that our character is moving back and forth uh it's time for us to uh finish the uh the vast or the WD control scheme so let's take a look again into our input mapping and we can see that we've already mapped the uh the W button and the S button so let's add two more let's add the uh the d uh so we find out here that's going to be D and let's add the a button as well so that's going to be our left movement so uh we do the same thing we map this to the down action or the down trigger and I'm going to do it over here as well so the uh if we do it like that uh now you can see that the W and the D values are the same uh and by default what's going to happen is when you press this button uh you're going to get a positive value uh the positive value on the X uh uh kind of XIs because by by default because behind the scenes uh we're creating this uh we're taking a Boolean value is the button down or not and we're mapping it over to a a vector an X and Y Vector so what we need need to do is we need to create a new modifier here and the modifier is called uh ssle Swizzle input actions so uh if we create this modifier what this does if we go into the uh to the inputs it's going to take the regular x y and C uh inputs and it's going to reorder them so the X by default which which is the thing that comes out of our movement becomes y the Y becomes X and the Z stays the Z so basically basically we're moving the uh the input that we get from the from the actual trigger we're moving it over to the Y value of the uh uh of the vector that gets created and we do the same here for the uh let's see modifier spizzle and we make sure that this is at to YX C uh and let's take a look at what happens here we go so nothing happens right now we still can go back and forth and nothing happens because if we take a look we haven't wired up the uh the Y value here so let's press play once again so now I'm pressing the D button and when I press the A Button uh I'm going in the same direction and this is because we we forgot that we also need to negate so we had have to add another modifier and this modifier is negating it so now we have the the uh these kind of three input the W SDA the vast controls I probably should have done this in the in that order order W uh is just by default it just takes the the down button moves you forward the S has the uh does the same thing but negates it the D takes the value does the same thing but Swizzles the input so it moves the x value that we get when we press the D button and it moves it over to the Y uh value of the vector and then a does the same thing but it also inverts the value so now if we press play everything should be working so we go left I press a we go right I press D forward with uh with w and back with s so that's basically it so we created one action one input action and we mapped it to the input and um and yeah that's how you set up these um Avast controls with with uh with enhanced input and input [Music] [Music] mappings
Info
Channel: Gisli's game development channel
Views: 2,349
Rating: undefined out of 5
Keywords:
Id: _fhYCRBjTmY
Channel Id: undefined
Length: 17min 24sec (1044 seconds)
Published: Fri Feb 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.