UNITY TUTORIAL - VR HANDS - NEW INPUT SYSTEM

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so about a month ago i made a video using unity where i created a scene and we used our virtual reality hands um we looked for some inputs from the controllers to drive the animations of our hands and now at the time i said that if i found a better way of doing it then i'd let you guys know well good news is i have we can have in fact make our code half the length it currently is let's take a look [Music] just as a little bit of background here from the last video i definitely recommend checking that out because it gives you an overview of what we've done here where we're starting from but just as a quick recap we got xr setup we've gone to our package manager and we've brought in the xr interaction toolkit just here and we also brought in the default input actions and those samples and then in the project settings we've also enabled the xlr plugin management and enabled oculus so that got started there and in our main scene our rig here that we're using is actually the action based uh room scale xr rig and as you can see we went through the process of actually assigning all of the correct actions to things like our controllers and using those samples that we imported from the package manager and then in the previous video we then wrote that long script to listen for um button presses on the controllers and which would then animate the hands and the vr hands were available as a package that you could import into your project all ready to go with the animations we needed to do is hook up a couple of variables but for those of you that watched that previous video that that script was very very long and we'll take a quick look at that script now but here's the script that we wrote in our previous lesson and as you can see it's quite lengthy just to get the grip animating and then the pinch of the was actually almost 50 lines of code which seemed ridiculous at the time was what we were doing we were reading in we were taking in some inputs in our code so first just show you the refab so our hand controller was we had to type we had to assign the action asset and then i was we were looking for the controller name and then the action name for the activating select so we were actually having to manually type these which isn't great especially if someone comes along in our input actions and changes those if you're working in a team then it completely break our hand interactions i wanted to make it so we just dropped in the action that we're actually using and which would then make it more efficient because also in our code we're using the update function to read a couple of values even when we're not pressing the buttons which you know when it's just two two buttons we're just reading in a couple of floats isn't too much of an overhead but i'd like it to be way more efficient so i found a way let's have a look so let's make a new scene i'm going to go file a new scene and we're not going to save anything there now i'm just going to add a couple of um things into my scene i'm going to add my level up plane might be a good bit of branding i'm going to remove the main camera let's delete that and my directional light that's fine for a minute and then we're going to create an xr rig and we're going to go and create a room scale xr rig which is action based and it's going to drop it into our scene i'm just going to reset it back to zero so it's in the good place and make sure my tracking origin mode is the floor then i'm going to open up my um xr rig and just double check that all the um all the corresponding actions are assigned and you can see they're all assigned there by default like we set up in our previous tutorial i don't have my model prefabs in there for my hands at the moment so i'm going to use my vr hands package and i'm going to select my prefab on my left hand i'm going to put my left hand model in there i'm gonna do the same for the right hand model like so and you can see here we've got our old script right hand controller we don't want that working at the moment so we're going to go ahead and turn that off so now we're going to go ahead and write our new script so let's go and click on our scripts folder and create a new folder here called awesome new script let's keep these separate a little bit i'm gonna right click and go create c sharp script improved and controller so in the prefabs folder on my left hand model i'm going to add a component which is going to be our improved hand controller and i'm gonna do the same for the right hand model take the old script off i don't want that garbage anymore okay like so then we're gonna go ahead and double click the script and we're gonna open up in visual studio first thing we're going to want to do is we're going to make the screen a bit bigger for you let's go to 150 so we don't need the two using statements the name spaces at the top we're just going to import the unity engine namespace when we hit squeaky though and and then we are going to use the input system so we're going to need to bring in that so using unity engine dot input system we're not going to use our start or update functions so we're going to go ahead and remove those we are going to need two variables and they are going to be able to type input action reference now we don't want other classes to be able to access these two variables so they can be private but we are gonna need to be able to see them in the inspector so we'll make them serialize field and we'll put input action reference it's gonna reference as you guessed it an input action and this is gonna be for the controller action rip we want another one for the trigger so serialize field input action reference oh small controller action figure let's get the camel case there let's put it on an a and semicolon so those are the only two variables we're going to need as opposed to the five or six we had before we are going to need to get a link to our hand animator so we will need a private animator and animator and i put my semicolon there that's the same as before so we're going to initialize everything in the awake function so we're going to use the weight we're going to avoid awake so in our awake function we are now going to allocate the performed event of our controller actions to a function and this make more sense when we type it out so we're going to go ahead and type controller action grip and then commit dot and then we want the action and then we want to say performed this is an event that's performed when we press the grip button and we're going to assign it to a method called grip this is a method to put a big g rip press finish off of a semicolon now it's going to go red here because it doesn't know that we haven't made this method yet so shortcut we can press control and then full stop and generate method at top of the list and it's going to put that grip press method in for us and we can go ahead and remove the exception we're also going to need to allocate a method for the trigger press so we're going to say control action trigger though this here is reference is this variable at the top this input action reference so when this action is performed when we're pressing the trigger it's going to fire off the trigger press method and again this doesn't exist so we can press ctrl stop and create that function down below and we can take out that we're also going to need to get hold of our animator so we can say hand animator equals get component animator and believe it or not we're almost done and then inside our trigger press we are now going to set the value for our hand animator because now this is only going to fire off when we're actually pressing the trigger so we can say hand animator we want to set the float or trigger and then the value that we're going to pass it is the value of the button press which is being passed to us through this object so we can say object dot read value bloat and finish it off with a couple of brackets and then a semicolon and that is literally all we need and we're going to copy this and put it in our grip let's copy that paste that inside there and change this to grip i remember from the other tutorial these strings here correspond back to our animator which is going to control the blend tree that doesn't stop there we can actually make this even less so we can turn these two methods into expression bodies and to do that we're going to put equals and then the greater than sign and copy this entire line paste it next to that little greater than sign and then we can remove all of that that might be a little not a lot you might not like long lines like that you might find it easier to read and when it's all broken down like that and that's okay this is just my personal preference i'm going to do the same here let's copy that paste that there and we don't need to use extinct that's popped in for some reason this is all we need to actually get our hands working and compared to this one it's way better we're no longer looking for values every frame it's only going to be allocated when we're actually pressing the buttons and there is a little bit more we need to do in unity to set this up so let's jump back to unity and take a look so back on our hand prefabs let's go ahead and select one so now we have uh the variables that we put in there for our input action reference are now visible to us in the inspector and we're looking for the impact action reference for the grip and the trigger i'm going to do this for the right and the left hand so on the right hand for the controller action grip we click on the little circle and we're going to be looking for the select which is the xri right hand select go ahead and click on that one and pop it in and close that and we want to do the same for the trigger which is going to be the activate so click on the circle go to xri right hand and we'll look for activate there it is just there double click and it's assigned it now for our right hand model we'll jump into our left hand model and do exactly the same controller action grip we want the left hand select and the left hand activate so now these these actions correspond back to our input actions which we imported you can see on the left hand we've got the select and the activate which is currently um set to a button and we need to go to our xr rig or our xr interaction manager actually even one will do probably interaction manager makes more sense and then we can go ahead if we're going to add a component to this and it's going to be the input action manager we're going to need to assign an action asset to this on the input action manager so let's go ahead and we'll sign our xri default input actions without this um nothing will work this will go through all our input actions and enable and disable them accordingly so make sure we've got that input action manager on there here we go so we have our hands they're orientated the wrong way but that's not a problem that's very easy to fix but now you can see we can press our buttons and they're going to pinch and do all the business which is great um but there is one problem with our hands and that is at the moment they're only taking in a value that goes between one now if you watch i'll just do that again if you watch my left hand here i'm gonna press my grip it goes on off on off there's no like slow movement of it down which is what we're really looking for let's take a look at how we can tweak our impact action assets to us our scripts the values that it needs so our script's already set up to receive a float value but currently we're only passing it zero one kind of like a true or false kind of thing because it's a button so we need to tweak that so i'm gonna use just on my xr controller i've just got my right hand controller selected i'm just gonna click on one of these references it's just gonna bring up um where my default input actions are located and i'm going to open up my xri default input actions which is contained in assets samples xr interaction toolkit and the version number and then default impact action so let's open up this i'm going to tweak a couple of things to pass the correct values across to animate our hands properly so let's have a look at what we've got at the moment we click on our xy left hand action map and then select our select which is going to be our grip so when we're making a fist at the moment it's trying to passing it it's just a button we're going to change it to value and then under control type we're going to change it to axis now i'm not great with a new input manager um so there might be a better way of passing the value across but this is what's worked for me and then we're gonna extend it down and you might have something different here to me you can actually go ahead and just kind of delete these i only generally you can only have one but you can put in as many as you want um on my grip pressed don't worry if you've got something different we're going to change its path so instead of grip pressed we're going to go to xrr xlr controller and the xr controller left hand optional controls and then we want to find our is that one there now we've got an action type of value control type of axis and um we're passing we change this just to the grip with generic xr controller ticked so save that let's have a quick test so here's our hand the red is just our xlr raid tractor so before it was just snapping straight to our grip closed position when we pressed the grip now i'm just going to gently squeeze the grip and you can see there that my hand is slowly closing which is what we wanted rather than just instant pose it's now reading in a value properly and see i need to set my um properties for the uh activate well that's much better that's exactly what we wanted to do does that work we now need to look at the activate so drop down from select to activate i'm going to change this to a value and an axis just like before instead of the trigger pressed we're going to change our path i'm going to go to other let's go back no don't go to other go to xr controller xr controller for the left hand optional controls and then we want trigger and then that should be the left hand complete let's save it and i will move ahead and do the same for the right hand so go to our activate give it a value make it an axis instead of the trigger press we'll change it from trigger press to trigger it's going to find it on the list there it is near the top trigger left hand x or xr controller it's in the right hand ah i need to call us out back again excited right hand optional controls trigger then just confirm you've got um the right path for the right hand so we've got right hand here and we've got the trigger right hand xr controller i think i've already set down the select again if you have got on button change it to value put it on axis then drop it down and instead of grip pressed change it to grip that would be um clicking on there going to the path and then signing the grip and then save that that is now all of our buttons changed to you to pass a float value across to our script which then our script will allocate the corresponding value to our animator so before we go ahead and test what we've just done i'm going to fix our hand rotation so they're around the right way so i'm going to create an empty object in my left hand controller and we're going to call this left hand rotation offset and make sure it's all at zero zero zero i'm going to duplicate that back into my right hand controller and name this one right hand controller offset and then for the right hand we're going to go minus 90 on the zed and for our left hand i think it'll be 90 and what that's going to do is beforehand where am i i was holding my hands like this in the real world my vr hands were like this and so we've just we've just made two transforms which are going to snap our hands back around the right way and we do that by going to our left controller where we have our xr controller script and dragging in that transform so drag the left hand rotation offset into the model transform and do the same for the right hand drag the right hand rotation offset in like that now let's press play and see what we have so our hands around the right way which is a great start and now let's press our buttons oh yeah that's much much better so now we're passing in that float value that's going to allow us to do a slow blend for a grip press instead of an instant snap which seems and feels much more realistic so we've got a pinch there fragile pinch gradual fist and that's working on both hands as well so this is way more efficient because beforehand our script was like 50 plus lines and had to enter a load of strings that had to go and then find the corresponding action asset and now all we're going to all we have to do is just drag and drop in um the input action reference for our grip and trigger uh and it's not now it's no longer looking for values for our grip and our trigger press every single frame on both hands it's only doing it when we're pressing these buttons which is way more efficient so i really hope you found that video useful again apologies having to update it but i did want to do an up-to-date method but i did say that if i found a better way of doing it that i'd show you guys and i think in the long run it save way more times haven't got that really long script that we have to do and generate as we did beforehand it's actually just a few lines of code and get our hands up and running really quickly with some nice animations um let me know your thoughts in the comment section below and if it helped you out at all it'd be really good to know and again your subscription is always appreciated i'm going to actually put this project up on my patreon page so if you're interested you can grab it from there thanks very much
Info
Channel: Daniel Stringer
Views: 6,134
Rating: undefined out of 5
Keywords: xr, vr, unity, action, input, tutorial, game dev
Id: qThlKMMHSpU
Channel Id: undefined
Length: 19min 49sec (1189 seconds)
Published: Sat Feb 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.