All About VR Interactables

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're getting back to basics and we're  going to talk all about how to interact with   items in the world so grab interactables direct  interactables ray interactions the real basics of   how to you know pick up things so if we jump into  unity i have a brand new project we're using unity   2021.1.20 f1 this is the newest version of unity  and uh this tutorial should work for anything   2020.3 and after now i'm going to run through  the setup really fast if you know how to do this   already then go ahead and jump to this timestamp  here and that's where we actually start this   tutorial but real fast if you want a refresher on  how to set up a vr project for unity we are going   to go to project settings xr plugin management  install the xr plugin management once that's   installed we're going to be using open xr so check  that box this will install the open xr plugin   and all that goes with it it's going to ask you  to restart your unity project because it wants to   swap the mode over to the new input system instead  of the old input system and when we come back up   you'll see there is a exclamation point next to  openxr and it's because we have a couple different   issues we need to fix so go ahead and click fix to  lock input to game view and then we need to have   at least one interaction profile so if you click  on edit it'll take you to the open xr tab so all   we did was jump to this one here and then under  interaction profiles we need to add in whatever   devices you're planning on having your project  be used by so i personally use an oculus quest   2 so i'm going to add an oculus touch controller  profile and then i also have a valve index so   i can add that in as well and i should be able  to use both devices interchangeably and then if   you're running oculus quest 2 and if you have  a mac for instance and can't use the link cable   then you're going to want to set up the android  side of things and go ahead and run build to your   quest um but that's a i have a whole other video  about doing that specifically we're doing this uh   we're doing this really quick all right we need  to download a couple different packages as well   so i'm going to jump over into package manager  go to unity registry scroll all the way down   i'm going to need to enable preview packages so  i'm going to click this gear icon advanced project   settings and then enable click that checkbox i  understand these things are in preview and then   we go back all the way back down to the bottom  we're going to install the xr interaction toolkit   and then we're also going to want to download  the default input actions sample that's gonna   go ahead and set up the controls so we don't  have to and now in our project we're going to   want to open up the samples and then go ahead  and add the default left controller and default   right controller to the action based controller  default so this is basically just setting up a   preset so now when we add in our xr rig then the  interactions are going to go ahead and come and   then in our project settings last change we need  to make is in preset manager we're gonna need   to specify right and left now we can swap over to  the scene view and right click on hierarchy to add   an xr rig and we want the action based xr rig and  you'll see that will come with a camera offset   left hand controller and right hand controller  and if you did it right you'll see that all   the right hand actions are already mapped to the  right hand controller and the left-hand action's   left-hand controller now we can get  into actually interacting with things   obviously first i'm going to want to ground so i'm  going to add in a plane here call this ground and   then i'm going to make sure gizmos are on so i can  see where my grid is going to be something to note   is that the player could spawn anywhere inside  of this green box so this is a 6 meter by six   meter play space that the player could spawn  anywhere so like if i have mapped this whole room   to be my boundary i could if i start here um  end up on one side or the other of this space so   just something to keep in mind if you don't want  your players spawning inside of walls and stuff   so in order to pick up some things we're going to  add in a cube this is going to be called the table   and then i'm going to reset the transform back to  the center and then just kind of drag it over in   the direction that my players could be facing and  then just to kind of help us visualize all this a   little bit i'm going to create a folder in my  assets called materials i'm going to spell it   correctly now it looks weird we're just going to  go with it um and then inside of materials we're   going to create a material and this is going to  be called table matte and then we're going to do   like a brown color fun fact if you didn't know  brown is orange but darker i actually didn't   find that out until uh kind of recently so fun  facts the more you know all right so we have a   little table here and we want you know some items  to pick up um we could do some cubes you know or   we could go into our package manager assets and  uh you know go to the unity asset store and find   some interesting things to actually you know  download and use so if we jump over to the   unity asset store we can click on assets we want  3d assets and then we can browse by different   items so let's look over we can do props and  then if you want you know more specific kind   of things like let's say we want maybe furniture  and we could do tools and then we also can sort by   free assets check this box down here and so now  these are all the free things we can download   so if we want to replace our cube table with  you know this table out of this furniture pack   or if we want to use some survival game tools like  this pack would be pretty cool so i can look at   this and then add it to my assets there we go so i  can just add this little survival kit to my assets   and then we can jump back into unity refresh the  asset list and then i can search for it up here   on the top and let's see which survival pack was  it there it is survival game tools go ahead and   download this and then back in our scene view  you'll see that under samp was it samples oh i   never imported it i downloaded it forgot to import  i do this all the time uh we'll take all of it   all right now back in scene view you'll see that  we have a new folder in the project menu called   survival tools and then we can go to prefabs and  let's say we want a flashlight these don't have   any colliders on them so we're gonna have to add  colliders to them um let's add in a water bottle   and then i'm going to shift click both of them  so we've selected both and then i'm going to   add component and let's add a mesh collider and a  rigid body anything you pick up is going to need a   rigid body so that's important and since we are  going to actively pick these up we're going to   change the collision detection mode to continuous  speculative and we could interpolate it if we want   interpolate just smooths out the position a little  bit better so it's not as janky and then you could   also update the mass and whatnot so like we say  the flashlight is a little heavier than the water   bottle or whatever let's see does the flashlight  actually have you know a light source it does not   all right so we have a flashlight and a water  bottle and we want to be able to pick those up   so let's jump over to the xr rig look at the left  hand and right hand controllers and see how these   work right now they are pre-set up with the ray  interactors which is usually what i recommend   for um a type of interaction so you have ray  interactor and you have direct interaction so   ray interactor basically just shoots out a line  from the hand and anything that that line hits   it's gonna check and say okay can i interact with  this item and if it's true then it will bring   the item back to the hand so that's basically  how that works and you can set up the length   of the line and say you only want to be able to  interact with things 1 meter away 10 meters away   100 meters away whatever um that's nice but you  also have direct interactor which is instead of   shooting out a line from your hand it's  a sphere or some other collision object   so it could be a box collider or something so  you have a little more space so i don't have to   point directly at the thing i want to pick up i  can you know just be in the same space as it and   as long as the collider is touching that object  then it's going to get picked up so those are the   differences between those two the problem is you  can only have one you can't choose both of them   at the same time even though that would be nice  and i know there are some workarounds by using   parented objects inside of the controllers and  it gets a little complicated i may do a whole   specific video on that but for now pick one i  usually recommend going with the ray interactor   because you can do ui do teleportation with that  and you don't have to you know swap back and forth   from direct to director and blind interaction so  we're gonna stick with the uh xr line interactor   and when it's pre-set up when you start out it's  gonna have a red line and then when you actually   hover over something that is interactable then the  line is gonna turn to white so how does it know   you know this is something i can pick up well  to do that we have to add in a component onto   whatever object we want to pick up so i'm clicking  on this water bottle if we add a component we want   to add a grab intractable and this has all of the  data or anything that you would want to interact   with so you have a interaction layer mask so  these are the things that can interact with this   particular object you can select and unselect  those colliders so this is going to stay and   start empty but if you say you let's say you  have a complex object and it has a whole bunch   of different colliders um you would put this  grab interactable as the parent and then you   just select the correct colliders that need to be  interacted with so like if you don't want all of   the colliders to be like pickupable or if you want  to optimize a little bit of performance you can   say okay well there's just this one box collider  around the entire object and that's close enough   we don't need to use every single collider  or the specific mesh collider in this case   if you leave it empty it's going to default  to whatever collider is attached to the object   or the children objects you can't you can set up a  custom reticle for this particular object so if i   am hovering over this or if i'm using a direct  interactor and i get close enough then i could set   a particular reticle and so like it's a bullseye  or a target or something and this could be any   kind of game object so usually it's a 2d sprite  that goes on this and it just kind of maps to   the surface and it shows you that you're pointing  this and you can pick this up a lot of games right   now are using like a white circle to you know  indicate that this is something you're looking at   or you can change it from um you know like a  circle to a square depending on some kind of   feature or whatever anyway movement type is pretty  important based on what kind of gamer you're doing   if everything is um physics based then you're  going to want to swap this over to velocity   tracking which basically means if i'm holding this  and i you know move my hand through a pole which   i can do because i'm in vr and i'm not limited  by you know physically running into the pole   the bottle won't actually go through the pull as  well it's gonna you know drop or fall off and will   be limited by physics so that's what velocity  tracking does kinematic is velocity tracking   but it doesn't react to outside sources so it's  only reacts to your hand and your hand motion and   then instantaneous basically just teleports the  bottle to the next point every frame these check   boxes so you want to retain transform apparent so  this basically says when i let go of the object do   we want to reset it back to what it originally was  when i picked it up or do we want to just release   it in the current orientation that my hand is at  track position and rotation basically just means   am i following the hand when i get grabbed um so  you could use a i'm trying to come up with a good   example of when you would not check these if you  want to grab something but you don't want that   object to be movable so if you want your hands  stuck to it but you hear you can't move it so like   maybe uh so maybe a wall or something you don't  want the wall to come with you that kind of thing   and then smooth position and smooth rotation kind  of eases that and applies some you know math to it   so you're not really frame jumping or anything so  if you're holding something and walking around and   moving and it's looking a little jumpy you can  apply these uh smoothing and do a little bit of   finagling with the actual values of the smooth  amount and tighten them out and then you also   have this throwing section so this is like okay  in vr people don't really throw as hard as you   would so like if i'm throwing a baseball 80 miles  an hour when i'm pitching in real life i'm gonna   be applying a lot of force to my throw but if i'm  using vr i'm not going to be applying all of that   force because you know i can't see my surroundings  so i don't want to like you know go full force and   like smash my computer screen so a lot of people  are normally reserved when they're throwing things   in vr they'll kind of like you know stop short  have a little alligator arms we'll call that   alligator arms like when you don't get all the way  out you want to just like snap really quick so in   order to combat this you want to add a little  velocity boost so it's default here to set to   1.5 so when you throw it it's going to take  the velocity of your hand multiply it by 1.5   and apply that velocity to the object and it's  going to continue going in that direction and   you can set the the fall off and different kind  of smoothing and durations and whatnot and then   you can also apply gravity when you release the  object if you want to pick up an object from a   specific point every single time so like so let's  say for instance i'm picking up this flashlight   here i want to pick this flashlight up from a  particular point every single time and so you know   let's go ahead and do that i'm going to add a grab  interactable to the flashlight and then we want to   add in a just an empty 2d object create empty  object and this is going to be grab target and   then we want to grab the flashlight and we're  going to change this to the local rotation and   we want the up axis to be pointing forward let's  see there we go and we may need to experiment   with this a little bit so now when i grab this  flashlight it's going to grab it every single time   from this grab target and all i need to do is  assign that to this attach transform and then you   have an attach ease in time so this is how quickly  the object will come into your hand so you you   increase this value and it will make it a little  bit slower so if you want to mimic some kind of   like force effect or something you could slow  this down to make it a little more obvious that   the object is like coming into your grasp instead  of just you know sitting there and like snapping   really fast and then attach point compatibility  this is really has to do with versioning of   the old version of this and the new version so  we're just going to stick with the default here   so now you can pick up your objects and uh there's  some more fine tuning you can do at the hand level   so if we go into the ray interactor this is the  all the values you can do for ray interaction   something that i always uncheck is anchor control  so if i uncheck anchor control what this does is   when i pick up the object i can move it around  with my thumbstick which is not really something   that you use very often in games is after you  pick up an object being able to like move it   around i mean it would work if you're doing some  kind of uh like holding something with a gravity   gun and wanted to push the object further away and  rotate it and whatever and it basically has that   kind of effect so i don't know why it's checked by  default so i always uncheck this so if you're like   running and your object and your hand is moving  further and further away you probably need to uh   come back here and uncheck this value so i'm gonna  go to the right hand and uncheck that as well   actually in the right hand i'm just going to  delete the ray interactor which you'll notice   that you can't delete you have to delete all  these components in a certain order so i can't   remove the line renderer until i delete the xr  interactor line visual so if i remove all of these   keep the xr controller because this basically  just deals with controls control inputs and   translating those so we always have those on our  controllers but let's add in the direct interactor   to the uh right hand so you'll notice there's a  lot less of uh things to do with this one and then   let's add a sphere collider and make it a  uh pretty small let's do point one spherical   collider yeah that should be good i'll turn  gizmos on in the game mode so you all can see   what we're actually seeing so the right hand has  a direct interactor and you'll see it has the same   interaction manager interaction layer mask attach  transform like you saw with the ray interactor   but you also have a select action trigger this  actually might be yeah so you have select action   trigger on both hide controller on select so if  you want to set it up so you have a controller   but you want to hide it after you grab the  object you would just check this box so i've   talked a lot let's test it and make sure all  of this works before we go a little further i   wanna there's one more thing i wanna show you is  how to actually turn the flashlight on and off   using the trigger button and some interactable  events just to show you how this works and then   that'll be the video so let me go get uh my  quest set up because i think it's um tucked away   somewhere oh there we go all right uh one problem  i just noticed is that we need to add the input   action manager to the xr rig forgot to do that  during the setup and then inside of samples   we want to drag the input actions onto that now it  should be set up i'm going to run it i'm in link   mode with my oculus quest and we'll see how this  works so i'm going to turn gizmos on jump over   into scene view so see my right hand has this  uh oh i did not add a uh rigid body to the box   um but you'll see that my right hand has a  little collider here and then my left hand has a   laser pointer type thing and this is the ray  interactor and this is the direct interactor   so director interactor is when i'm close to  an object and when i'm like within the sphere   that you know triggers the direct  interactor and now i can you know   interact with objects that are within the sphere  and then ray interactor is when i'm pointing   at objects i can interact with them so let  me set up the box a little bit better so that   nothing falls through it actually these should not  have fallen through anyway slightly confused let's   add a rigid body to this box and you can just be  continuous collision so i put this flashlight and   water bottle let's just swap these over to  continuous um and then i'm gonna actually make   the mesh colliders ah that's it mesh colliders  need to be convex for these objects all right   right hand has a sphere collider oh the sphere  collider needs to be a trigger that's important   so if we just have a normal collider we're going  to knock over things but if it's a trigger then   we can go through it and then it'll you know  trigger the interaction there we go so there's my   um little box collider so if i come back here  there we go so you can see i'm holding the   flashlight i can drop it on the no it rolls um  and so you'll see when i um pick it up it auto   snaps when i pick it up it snaps to pointing  forward which is great because if i'm like   wandering around needing to use a flashlight i  want it to point where my controller is pointing   so that's nice um and then if i use my left  hand controller when i point at it let's say   let's try the try the bottle see when i point the  bottle i can pick it up and i'm grabbing using the   grip button here that's the default and then  when i drop it this is hard to do in like a   third person kind of perspective and then you can  throw it and you have a little bit of a multiplier   you see that even with the multiplier it didn't  even go far see there we go um so that's the   basics of that and uh let's do the one more thing  where we want to turn the flashlight on when you   uh say pull the trigger so first off we're gonna  want to add in a light we're gonna do a spotlight   because it's directional right and then i'm gonna  swap over to the rotation mode have it pointing   forward and then move it so the cone is actually  coming out of the light like so like that all   right and you can obviously play around with these  values so first we want to start with the light   off but when we turn the flashlight on we want  when we pull the trigger while we're holding the   trigger we want the flashlight to be on and if  you want it to toggle you'll need to do a little   bit of coding but right now we're doing purely  no code kind of stuff so inside of this xr grab   interactable if you scroll all the way down to the  bottom there's an interactable events section and   this triggers an event for anything you can do to  this interactable object so if you are hovering   over it which meaning if you're pointing the  ray at it or if the direct interactor sphere   or whatever collider you're using is you know  touching it then this will happen entered exit   select is when you're pressing the grip button  by default and activate is when you're holding it   and you press the trigger button by default  that's how these are all set up so activate   when we activate it so when we press the trigger  we want to so i'm going to hit this plus button   it's going to add in an event here i'm going  to drag the light and then we're going to go to   game object and set active and we want to check  that box and then when it's deactivated we want   to grab the light go to game object and set active  to false so we're going to leave that unchecked   so activate holding down the trigger it's going to  turn the light on and deactivate when we let go of   the trigger it should turn the light off so let's  test this all right so let's grab it grabbing the   light can i see if it's oh it is turning on it's  just very dark there we go so i'm holding down the   holding the light you can't see it when you're  looking at it but when you point it it works   so you would also need to add some kind of  uh you'd also need to add some kind of point   light inside of there if you wanted it to  like you know look more like a flashlight   so what i'm holding on the trigger it lights  up and then when i release it like it lets go   and then from here you can kind of expand  on that and add in some more complex   code that happens when you interact with an object  so if i wanted you know to actually toggle like a   normally in a game situation you would want  the flashlight to toggle instead of just   like trigger and hold only when you're  holding the thing so instead you would want to   have this actually run some code so you  would add on an extra script and just   have a toggle method that toggled the light  on and off every time the method was called   and then you would just call the method inside  of activate so every time we pull the trigger   off on off on so instead of dropping in the  light game object we would just drop in the   flashlight game object and then we would add in  this add the script on top of the flashlight as   an extra component here and then call that method  from this drop down here it would like pop up as   another option instead of game object transform  and light would be a third option here so that   kind of stuff that it's um stuff like this i just  kind of want to get your mind going thinking about   different possibilities of things so let's say  you want to be able to drink the water or um you   know other things like that so thanks for watching  let me know what else you would like to know about   you know unity vr if you're interested in just  watching me play games i may even do some live   streams where i invite some of you to come hang  out and play you know pavlov or something with   me instead of just you know teaching and being the  boring teacher all the time i want to be able to   interact with y'all in the community and i think  it would be really fun to play some games and you   know meet up in population 1 or pavlov and play  some community games i think that'd be super fun   so let me know if you're interested and you  know stuff like that watching just live streams   of gameplay or even just live coding that kind of  stuff or what else you would like to see subscribe   and hit the notification bell so you know the  next time i post and i'll see you next time
Info
Channel: Justin P Barnett - VR Game Dev
Views: 4,439
Rating: undefined out of 5
Keywords: Justin P Barnett, unity xr, unity new input system, xr interaction toolkit, unity vr tutorial, beginner, unity vr, unity tutorial, vr tutorial unity, unity3d, 2021, unity xr interaction toolkit, openxr, new input system unity, unity xr toolkit, openxr tutorial, indie game devlog, xr unity tutorial, vr interface, xr toolkit unity, xr interaction toolkit unity, unity input system, unity input system 2021, unity xr toolkit tutorial, openxr unity, vr interaction, pick up in vr, vr
Id: furfe8E7SOA
Channel Id: undefined
Length: 25min 34sec (1534 seconds)
Published: Wed Sep 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.