Jump in VR using Unity's New Input System // Unity OpenXR Beginner Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back you'll notice we're in a new  space my wife and i just moved so uh this is   what we've been doing for the last week so a lot  of you have been asking me how to jump over the   last couple weeks now your words have been saying  how do i jump but i think what the meaning behind   all of these questions is is how do i trigger this  action when i press a button on my controller or   perform some kind of action so today i'm going to  walk you through how to jump but you can use this   process for any kind of button press or any kind  of action you want to do in your game with these   controllers and after we get through the jumping  part of this tutorial i'm going to tie the jumping   action into player movement so by the end of this  tutorial you'll have a full working player that   can jump and move around and do all the things  and one more thing before we start we have a new   discord which is super exciting there are going  to be a ton of events we're going to play a lot   of vr games if you need development help just it's  kind of a place for vr enthusiasts like you and me   to just hang out and talk to other people like  us so join that discord in the description and   let's jump into the tutorial we're starting  out in a brand new universal render pipeline   project and i'm using unity 2020.3 this should  work for anything 2019.3 and after but i   definitely recommend updating to at least 2020.3  because it's a long term support version so you'll   get the best results using that version and later  first thing we're going to do like usual is set up   our xr project if you already know how to do this  or have seen one of my other videos jump to that   time stamp right there and that'll be the next  part of the tutorial i'm going to walk through   this xr setup pretty quick so if you want a more  in-depth walkthrough check out this linked video   and let's jump into it first thing  we do is go to edit project settings   we're going to go over to package manager and  enable preview packages then we're going to   open window package manager flip over to unity  registry and we're looking for open xr plugin   install that one it's going to ask you to restart  so it can update your input system settings and   we're going to say yes to that now we're going to  open back up the package manager and scroll down   to the bottom and find xr interaction toolkit  we're going to install this and still inside   of this xr interaction toolkit package we're going  to expand samples and import default input actions   now we can exit out of that window back in  project settings we're going to go down to   xr plugin management and check open xr and then  click on this exclamation point that'll pop up   and click fix all that'll leave us with one more  thing to do and that is going to it's our features   and check whatever controller profile you're using  i'm using the oculus rift s for my testing so i'm   going to check the oculus touch controller profile  i'm going to exit out of this window and let's   find the samples folder and go to xr interaction  toolkit whatever version you're at and then   default input actions this was the sample that we  imported and we're gonna go through and add all   of these presets if you click up here at the top  it'll add these to our profile and then one more   setting we go back up to edit project settings  and then inside of preset manager we have a new   action based controller preset and we're going to  specify right and left now we're going to zoom in   here and right click in the hierarchy go to xr and  add a room scale xr rig action based to our scene   reset the transform and i'm going to drag it over  this way and the last step for this is to add   a component and add an input action manager and  then we're going to drag and drop our xri default   into the action assets and that'll let you control  the controllers and whatnot there we go now we got   our xr rig completely set up the project's ready  to go now we can start doing jumping with your xr   rig selected we're going to add a component and  this one's going to be a capsule collider and i'm   going to reset the y to 1 and make the height 2.  the height is going to get adjusted dynamically   eventually so really the only thing you really  need to worry about is the radius and we're also   going to add a rigid body this lets us implement  physics and when we do jumping we're going to add   an upwards force and the rigid body is what the  force is going to be acted upon the only thing   we need to change inside of here is to freeze all  the rotations because we don't want the capsule to   just fall over that is not fun for anyone using  vr and then the bread and butter of all of this   is we need to actually be able to press a button  and something happen so with all of this set up   we're going to go back and find the default input  actions so mine is in assets samples and then xr   interaction toolkit the version default input  actions and then we're going to double click   on this xri default input actions that's going to  bring up this little control here and you'll see   we have three different action maps we have the  hmd the left hand and the right hand and inside   of the left hand and right hand the reason we  imported this sample is because we have all   of these already set up we don't have to set them  up ourselves but you'll notice that there's not a   good option for jumping there's like none of these  actions are like press a button it's the select   which is the grip button usually activate is the  trigger ui press is not really what we want so   we need a specific action that we're going to use  for jumping so let's make a new one we're going to   have it map to the left hand you can map it to the  left hand or the right hand or both whichever one   hit this plus sign up here at the top and we're  going to type in jump and then the binding is   going to be a xr controller a xr controller  left hand and then we're going to go into   optional controls and primary button this will be  the uh the a button i believe yeah the a button or   the x button on the oculus controllers or if your  controller only has one button it's gonna be that   specific button and then we're going to check this  auto save button up here and that should be good   so now if we went into our xr rig and then camera  offset and looked at our left hand you see over   here on the side in the xr controller we have all  of these references to different actions you'll   see we now have a jump reference right here so  we're going to program a input action reference to   that specific action and it's super easy it's like  two lines of code so let's go back up to xr rig   and make a new component and i'm going to call it  playercontroller this is just going to be a script   and let's double click on that and jump into the  code let's create that input action reference so   we're going to serialize field private input  action reference and my ide automatically   imported this but make sure you have using unity  engine.input system in order for this to work   so if it's underlined that's probably why and then  i'm going to call this one jump action reference   and then we also need a jump force so how high  are you going to be able to jump that needs to   be a variable that we can configure so we're going  to make it another serialized field private this   one's going to be a float i'm going to call it  jump force and we're just going to preset this to   something like 500. so these are the only two  variables that actually need to be set inside   of the inspector and then down here we're just  going to make some private variables we're going   to need the rigidbody so we're going to do private  rigid body and i'm just gonna call this one body   i put an underscore inside of my private variables  that you can't access just to differentiate them   from the uh serialized field ones all right  let's go into the start method and get the body   so body equals get component rigid body and this  will just get the rigid body off of the current   component and we also need to make sure that  there is a rigid body so up here at the top above   the class we're gonna do square brackets require  component type of rigidbody and this just states   that if we add this class to any other component  it's gonna automatically add that in later so you   don't forget it and if you tried to delete the  rigid body for some reason it won't let you so   this is just a little bit of you know housekeeping  i guess this is the super nice part where i just   love the new unity input system i can just call  this a jump action reference say i want the action   and when it's performed and then i can assign that  to a class so we're going to make an on jump class   so now anytime whatever specified action i just  put in here which is going to be the jump action   is performed we're gonna execute this method  on jump so let's go down here below update   and make a new OnJump method and do private void  on jump and anytime you're assigning a action like   this the inside of these uh parentheses you're  going to need an input action callback context   and we're just going to say object all right and  how do you jump well you take the body and then   you add a force and then that force is gonna be a  vector three ah that's the direction multiplied by   the jump force there you go but first we also need  to make sure that it's we're grounded so we can't   jump while we're in the air um so we're gonna have  a if and then we're gonna say is not is grounded   and then we're gonna return so we're gonna make  this new variable is grounded and that's gonna do   a raycast check to make sure that we are touching  the ground and if we're not then it's just going   to return and jump out of the method and nothing  below this is going to get performed so let's make   this is grounded variable we're going to go back  up here to the top and we're going to say private   bool is grounded and then we're going to do  this little arrow sign and this just basically   means that whenever is grounded is called  as a variable whenever you get that value   we're just going to run whatever this function  we put after the arrow is and whatever value   this spits out that's what is grounded is going  to be equal to it's just a little shorthand   for um if i was going to make a variable and do a  get but not a set i can just instead do this arrow   and it makes it a lot easier so we're going to do  a physics raycast i'm going to do a new vector 2   and we're going to use the transform position.x  and transform position.y except on the y position   we're going to add 2 to that because we want  to start the raycast from the player's head   pointing down that way if we're falling and the  refresh rate's a little like not so good just in   case our feet go through the ground our head still  knows we are grounded and then so the direction we   said we want to be down and then the distance of  this raycast is also going to be 2 because we're   going up 2. so now back down here in the on jump  method is grounded is referring to this method up   here and now we can go back into unity let that  refresh and then inside of this player controller   script i'm going to hit this little dot over  here and we're going to find the xri left-hand   jump set that and then the jump force is set at  500 we can adjust that for how high we want to   jump obviously adjusting this will make you jump  higher or shorter so let's hit play and see what   happens so i'm going to take my left controller  see my character here and if i hit the a button player jumps up and down  which is exactly what we want and if we decrease this by say a hundred we'll  jump a little less we go all the way down to   100 just a little bounce which is perfect um  but you'll notice that we can't move around   none of my other controls work we're just kind of  stuck there and if i move my character around in   my if i move my head around the capsule doesn't  follow me either so we're gonna fix that too i   don't want to leave you guys with a half working  project so so inside of the xr rig we're going to   add a few more things so we can get some movement  going we're going to add a locomotion system this   is going to reference the xr rig so let's drag  that in there and then we're going to pick a   locomotion system so i'm going to go down to xr  locomotion and i'm going to do the continuous move   provider action based this is going to reference  our locomotion system so i'm going to drag that   in there and then we want the locomotion to be  controlled with the left hand so we're going to   uncheck the right hand move action and then one  more we're going to go xr locomotion and snap   turn provider this is also going to reference  the locomotion system and we want the right hand   to control this so i'm going to uncheck the left  hand snap turn action and then from here you can   adjust the turn amount and whatnot and also the  move speed from your continuous move provider   and then let's jump back into the code and update  the capsule collider so it's always the player's   height and where the player is in relation to the  room that they're in so back in the code above   the body we're going to have a private reference  to the xr rig and then we're also going to have   a private reference to the capsule collider and  i'm going to name those xrig and capsule collider   with the underscores in front of them and then  down here in the start method just like we did   for the body we're going to want to get the  components for each of these just like this   and then down here in the update method that we  didn't use we're going to reposition the capsule   collider and then also adjust the height so to  do that we're going to create a variable called   center and this is just going to equal the xr  rig dot camera and space position this will get   the current position of the head mounted camera  display and then we're going to get the collider   say the center we can't just set it to this new  center variable that we made because it'll adjust   where our height is as well and we don't want to  adjust our height because if we jump we don't want   our height just manually set to you know however  tall the player is so this will end up being a new   vector 3 and we're going to use the center x the y  position like we said is going to be the collider   center and the y position of that and then the  z position is going to be the center z so this   line will adjust the position and then we're also  going to adjust the height and all we need to do   for that is to collider dot height is going to be  equal to the xr rig dot camera in rig space height   and this uses the camera's natural um floor height  offset to uh just reposition the collider and if   you wanted to clamp this you could do this inside  of a mat f dot clamp and then set a min height and   a max height just so your player can't like you  know crouch all the way down and get into spaces   they're not supposed to but for this purpose  we can just do whatever height they want to so   save it and we're going to jump back into unity  and there's nothing else you should need to do   in the inspector so i'm going to hit go and then  swap back over to scene view and then if i click   on the xr rig capsule collider you can now see  that when i move around and go up and down it kind   of adjusts the height a little bit so i'm going  to walk around the room for a second you guys can   see that and you'll see if it follows my position  and it also follows my height when i move around and then i also can move the joystick and it'll  move my character around and i can also snap turn   by flipping the other joystick and it'll snap  me around there we go you have a fully working   movable snap turn jumping player you can uh you  know take this and run with it once again i want   to thank all of my Patreons thanks so much we just  hit over 100 a month and that means that i'm going   to buy an oculus quest i actually ordered it and  it is on its way here and i am going to put a   unboxing video together for you guys and i'm  going to start doing more game development for the   oculus quest 2 so the next video will probably be  an oculus quest 2 unboxing which is super exciting   and then all the tutorials after that we'll use  the oculus quest 2 as my main development platform   so if you have an oculus quest 2 get excited  because that's all the tutorials coming and   then the next tier once we get to 250 dollars  a month i'm going to create a unity asset put   on the asset store for free and it's going to be  an xr starter project that you can just download   and instead of having to go through and set  up player movement and a player controller   and all those things you can just use my asset  and get started in your xr game so once we hit   $250 dollars a month i'm going to make that  that'll be super helpful for you guys i hope   if you are interested in getting this source code  i provide all the code on screen for you guys but   if you're interested in supporting me and want  to just download the project and get access to   the source code you can support me on patreon for  three dollars a month and you get access to all of   my source code for every single video and as usual  for free i reply to every single comment so if you   ever have any debugging issues or something  you can always put a comment down below and   also we have a discord now so you can join the  discord and chat with me and chat with the other   vr enthusiasts that are also on this channel  so come hang out and i'll see you next week
Info
Channel: Justin P Barnett - VR Game Dev
Views: 6,701
Rating: undefined out of 5
Keywords: Justin P Barnett, unity xr, unity new input system, xr interaction toolkit, unity vr tutorial, beginner, unity input system, unity xr interaction toolkit, xr toolkit, unity xr toolkit, oculus rift s, xr unity, unity vr tutorial oculus quest, unity vr development, unity3d vr, c#, unity vr tutorials, vr tutorial unity, unity vr tutorial for beginners, game dev, unity3d, unity 2020.3, gamedev, how to jump, unity how to jump, unity how to add jumpinng, how to make vr player jump
Id: Mfim9MlgYWY
Channel Id: undefined
Length: 17min 29sec (1049 seconds)
Published: Wed Apr 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.