I have actually received a surprising amount of requests for how to eat in VR. So since we're so
close to the holidays, and everyone's going to be eating a big feast, I hope I will show you how to
eat your own feast in VR. So let's jump into it, I'm going to start out with a brand new project,
we're going to be using 21 point 2.7. In this version, I'm going to show you how to do all
of the eating parts first, and then afterwards, I'm going to show you how to set up the XR part
of things. So if you already know how to do the XR part, then all you have to do is watch
the beginning. And if you want to see the whole process, then you have the whole video. So there
we go, I just like showing y'all how to do the whole process start to finish, I don't like just
doing this little segments. So to help all of you out who need that context, I'm gonna do the whole
video. So let's jump into it, we're gonna call this eating tutorial, it's just going to be a
standard 3d project. While that's booting up in the background, I'm going to show you the
asset pack that we're going to be using for this particular tutorial. So I'm going to be using RPG
food and drinks pack. So you can just search for that on the Unity Asset Store. And the reason
we're gonna be using this is because as you can see, in this video, they have different sections
of, you know, eating foods, so there's different, like states have the food from fool to halfway.
So we'll be able to actually use this in our code to simulate eating. Now that we've got our project
up and running, let's go ahead and import that assets. So let's go to Window package manager, and
then my assets. And then you can just type in RPG food should come up, there we go, you just need to
download it and then import the latest version, it will come with a whole bunch of assets,
it comes with a couple scripts too, we're actually just going to uncheck those, not download those.
And we also don't need the demo scene as well. So we just want the materials and the prefabs.
So import that. And then for this particular project, we're going to be using the turkey
legs. So I'm going to find it inside of prefabs. I think it's a hand, it's gonna be hand. So I'm
gonna drag that into the hierarchy here, and then double click on it to zoom in. And there we go.
And if you look over here on the inspector, you'll see that we're going to reset it back to the
center. So if we look over here on the inspector, you'll see there's a script that was here and has
been removed. And that's because we just didn't import the script. So I'm just going to right
click on it and click Remove component and that'll take it away. And then if we expand the ham in the
hierarchy, you'll see there is ham four, a four, three, or four, two, a four and one of four. And
these all relate to the state of how eaten this ham is. So for example, I can turn on ham three,
or four and turn off for four, it'll say it goes down a little bit, same thing for three,
or four and two, four, so turn off three fours, and there's one more state 1/4. And then we can
actually even turn 1/4 off, and we're left with just the bone. So we're going to add some code
to this model and have it kind of stepped down every time we take a bite of this particular
food item. And this applies to all of them. So if we have a pie in here, you'll see that the pie
also has different states. So I could turn on PI three or four and turn off four, four,
and you'll see that it goes down in chunks as well. So let's make a script, I'm going to go back
up to my assets folder, right click Create Folder scripts. And then inside of our scripts folder,
right click Create C sharp script. And we're just going to call this consumable
because we want this to be able to apply to food drinks, anything that you can consume
in portions. So we don't, we don't just want to call it food or something generic like that. So
double click on it to open it up, I'm going to clean up this starting folder a little bit.
And we're not going to need the update method. So delete that, there we go. Now we have just the
basic all we're using is Unity engine. And then we have this mono behavior consumable. And for
all the portions that we're going to use, we're going to have a serialized field of game objects
and the game objects are going to be an array. So add in the square brackets, and we can call this
portions, we just need to remember to set this on the inspector. And then we're also going to have
a serialized field integer. And this is going to be an index. So what portion are we on and
we're going to set this equal to zero, default to zero, but I like to just have it in there. So we
know. And then we're also going to want to have an audio source. So we can play
an eating sound whenever we eat this particular object. So I'm going to add in a audio source,
and we're going to call it underscore audio source. Reason I add the underscore is because
no one else can access this property. This is just within this particular class, unlike the
serialized fields that can be accessed from the inspector. So that's the reason I differentiate
that even though they're both private and then in our Start method, we're going to get the audio
source audio source equals get component audio source. And then we're gonna go ahead and set
the audio source to Play On Awake equal to false just in case we forget to set it in the inspector
we just don't want right when Everything spawns in you to hear a chop sound like you're eating
something already. And then we want to have a method that goes ahead and sets whatever index
we have to that corresponding portion for the item of food. So I'm going to just pick a method called
Set visuals. And then down below, we're going to create that so void set visuals, and inside
of this method, we want to loop through every portion and set that to active or not active,
depending on what the index is create a for loop or loop of I have length portions dot length,
and you'll see this green, the green comments that show up below are actually the hotkeys that I'm
using. So some of y'all were asking, what button Did you press for doing that? So I added a little
hotkeys kind of script to the bottom. So you can actually see what buttons I'm pressing if I,
if I type something in and forget to actually tell you what I typed. So in this particular case,
we're looping through portions dot length, and we're gonna say for whatever portion we're
currently on, so portion pie, we're going to set that portion active if the AI is equal to index.
So if the iteration for the for loop we're on is actually equal to whatever index we're at, we're
going to set that one to active, everything else will get set to not active. And then we also need
a method to actually set visuals, you know, during game time. So right now, we set everything up at
the beginning, but we don't have a way to actually set things when we're eating it. So we're going
to add a new method, and this is going to be a public void, or we're going to call this consume.
And then the first thing we want to do inside of consume is check to see if we are actually
done consuming this. So we're going to say if and then we're going to make a method for this. So is
finished property rather. So if we're not finished, then we want to up the index. So index
plus plus, and then we're going to set visuals, and then we can also do audio source dot play. And
then the last thing we need to do is set this is finished. So I'm just gonna go up here at the top,
and we're going to say public rule is finished, it's going to be equal to, or we're going to do
equal, this is an expression body operator. So basically just says set is finished equal to
whatever is on the other side of this arrow. And that's going to be equal to the index equal to
portions dot length. So if we get to the end of the portions that are listed,
so this accounts for we could have five portions, 10 portions, whatever, how, however tiny,
you want to segment the bytes, we have a operator that checks to see if we're finished. And this
just prevents errors. And then in order to test this without actually having to go into VR and
pick it up and try to eat it, all we have to do is set a context menu. And we'll just say consumer,
and this will allow us to actually call this method from Unity. So when one more look at the
code, just to review, we have a list of portions a index and is finished check to see if the index
is actually at the portions length, which means we've hit the very end, we have an audio source,
and then we set everything up at the beginning with the audio source, we get that component
and then Play On Awake, it's false. And we go ahead and set up the visuals. So just in case we
set the portion to equal one or two, or you know, if it's this meal is not all the way full, we can
go ahead and set that at the start without having to automatically have everything full at the very
beginning. And then set visuals just loops through and sets each each portion to whether or not that
index is active. And then finally consume checks to see if we're finished eating. And if we're not,
we'll increase the index, set the visuals and then play the audio source. So back in unity,
now we just go to the ham drop in the consumable script. And then I'm going to lock the inspector,
click on the top portion and the bottom portion holding down shift that'll grab all of them,
and then just drag and drop into portions. And the order here is important. So make sure the
fullest portion is here at the top, then you can rearrange them just by clicking and dragging
over here on the left side. And we'll start our index offset zero play, and then switch back to
Scene View. And then over here, the beauty of this context menu is we can right click on consumable,
and go down and you'll see we have a new button consumed. And when we click it, you'll see the
meal goes down. And you'll see this index is incrementing. And now we're at the end for you
consume again, the index just stays at four, which is exactly what we want. But what if we want to go
back up to the beginning you'll say like when we are even, you know, here in the inspector, let's
go back to the code and add in one more method. After start we're going to say on validate.
And all we're gonna do on validate is set visuals. And this allows us to increment the index and
have everything update inside of the inspector without having to hit Play mode. So here we can
set the index to one and two, and you'll see how it automatically decreases and updates everything.
So that's what on validate means It means anytime you changed anything inside of this script in the
inspector so we can adjust everything how we want just a fun little trick for you. And then
we want a consumer so we have a consumable well To make a script that actually consumes consumable,
so right click create a new C sharp script and this is going to be consumer. Open that up and
writer and I'm going to clean this class up again. And this one's relatively simple.
All we need is a collider. And it could be any collider so I'm not specifying box collider or
sphere collider or anything like that. And then in the start method, we want to get the collider
so collider is going to equal get component collider and then we're going to set this collider
is trigger equal to true so just in case we forget it in the inspector, we can automatically set it
here in the start method and then create a new method on trigger enter. And we're going to check
to see if the thing entering the trigger area is a consumable, so consumable consumable is equal to
other so whatever is getting passed in dot get component consumable. So this will not be know if
other the item getting passed in is a consumable. So we're gonna say if consumable is not equal to
no, and we're also going to check to see if the consumable is not finished. So if because it was
enabled, and all and consumable is not finished, so we added this not operator in front of is
finished, then we can do consumable.ca, tada, and this one. So this one's pretty simple. You just
set up the collider at the beginning. And then on trigger, enter, we're just going to check and see
if it's a consumable, and if it is, we consume it. And now we just have to make sure that this script
is on the same game object that the collider is and realistically anything could be a consumable,
we could check that without setting up the XR reg by creating a new empty object,
we'll just call this a consumer unlock the inspector so we can actually see it and then
drag the consumer script on there. And then we're going to add a box collider, we're gonna reset the
transform back to the center, move it over, and let's make the box collider quite small.
So, on the ham, we're going to add a box collider and it automatically puts it around the bone. But
we actually want the box collider to be around the ham MIDI part that you would actually eat.
So I'm going to adjust this by clicking this tool. And then let's move this collider so it
lines up better with the actual hand. And this is kind of tricky sometimes because your perspective
is off. So you have to kind of move around in this doesn't have to be super accurate. But there
we go. So that's a better collider. And this one doesn't have to be a trigger. And one more thing
we forgot to add is an audio source. Let's drop an audio source into the ham game object. And
then for an audio clip, I'm actually going to go to freesound.org. And there's this eating sound
effect to cut chomp kind of sound. So I'm just going to download that to my computer.
And then we're going to drop it into the right project. So eating tutorial assets, and then we're
going to make a new folder called Audio and save that there. And now inside of our audio folder,
we have this sound clip. So on the ham audio source, let's drag and drop that audio clip there
and then playing awake, we can uncheck and the last thing we actually need to add on to the ham
is a rigid body. And we're going to set us gravity equal to false because we don't want it to just
fall off into oblivion at the moment. So let's hit play, we'll see this run again, go back into Scene
View, I'm going to click on the consumer and we'll watch how this happens. So eat, eat.
And there we go. Let's set this up. So it actually works in VR. Now first thing we're going to do
is go to our project settings. So Edit Project Settings. And I'm going to dock that right next
to the actually we'll just make it big here, we're gonna go down to XR plugin management
and install that. And then for our PC tab, we're going to check open XR, and that's going to
install the open XR package. And it's going to ask you if you want to restart unity, so it can
update to the newest input system. So just hit yes for that unity will reboot. And now it's giving us
a little warning. So we want to drop down into the open XR section and add in a couple of controller
profiles. So we're going to do valve index, Oculus Touch and HTC Vive. And then if you have the issue
where you only see in one eye for some instance, try checking multipass instead of single pass,
instantiate it up here in the render mode, and that should fix your issue there. And then let's
go back into XR plugin management over to the Android side and check Oculus and that'll install
the Oculus SDK. Alright, that's all we need in the project settings for now. Now let's go to
Window package manager and then we're going to add in the XR Interaction Toolkit. But the interaction
toolkit has actually been it's not showing up anymore even if you add giffy go to Advanced
project settings and enable the pre release packages then it still does not show up so even
if we refresh here doesn't show up. So what we're going to need to do is hit this plus icon and then
add project for To get URL, we're going to type in comm clm.unity.xr dot interaction toolkit. And
this is the unity get URL for that particular package. And that'll install the latest version.
for us. It's gonna say the XR Interaction Toolkit has recently been updated to a 2.0 version. So
if you have a 1.0 version, this will cause some breaking changes. So just say I made a backup go
ahead, and it'll continue to import that package. And then we want to expand the samples drop down
and import the default input actions. And that will give us some pre setup controller
profiles. So we don't have to set up what each button does. Alright. And then if we
look in our project window, we'll go to samples XR Interaction Toolkit, and then the 2.0 release, and
then default input actions. And then we have five presets here. And I'm just going to go through and
click on the button up here in the inspector, and add each of these presets. One more thing,
go back to Edit Project Settings. Inside of the preset manager, you'll see we have all of our
presets here that we just added. And we want to differentiate the right from the left. So
just in this filter here, we're just going to type right and left. And now we can add in our XR rig.
So we're going to right click in the hierarchy go down to XR and it's actually been renamed from XR
rig to XR origin. So we want this XR origin action based one and then just go through and check and
make sure that the left hand actions have left hand references and the right hand actions have
right hand references. And on the interaction manager, we're going to add a component
and this is going to be an input action manager. And then we're going to click on the plus button,
click on this little dot here and add in the SRI default input actions. And that'll hook
everything together. And then let's add in a floor so we don't fall. So be called Ground. Make sure
this is set to the center, make sure our origin is reset to the center. And then we're going to
pick our ham up and kind of rotate it so it's facing up. And you know, right there, it's just
fine. Now we need the ability to to extra pick up the ham and eat it. So on the ham object, we're
going to add a grab interruptible. And I'm just going to check smooth position and smooth
rotation. And we also want to grab point, because right now if we were grab that, if we picked it
up, you'll see the blue arrows pointing down so the ham would actually be facing kind of
weird. So we're going to right click on the ham, create a empty object and call this attach point.
And then I'm just going to rotate it around so that the blue axis is pointing in what a forward
position would be. And then we'll drop it down a little bit. So you actually hold it by the bone,
there we go. And then we drop in that attach point into our attach transform. And then we want to
drop the consumer game object that we made as a child of the main camera. So we're
going to make sure that the consumer has been reset to the center and then the main camera,
and then we're going to kind of position this where the mouth would be. So right here,
just below and maybe in front of the camera. So when the person puts food up to their mouth,
it's like right below their eyes. And so their eyes would be right here with the cameras. And
since that's parented, it'll go wherever the camera goes and face wherever the camera faces.
So it's exactly what we want. And now let's test it and see if it works. So now if we hit play,
we can pick up our hands, see it grabs by the attach point, and then we can eat it
perfect, and there's no errors or anything when we try to eat it anymore. And there we go. We
have a working food item. And there we have it eating in VR. I hope you enjoyed this video. If
you run into any issues along the way, feel free to type them out in a comment. Or if it's a little
more complex of an issue, you can join our Discord and we have super helpful people there and we can
help you out figure out what your issue is and help you solve it. If you are interested
in the source code for this particular project, there is a link down in the description if you
support me through Patreon you can have access to that. I really appreciate all of my Patreon
supporters they really helped me continue to do videos like this. Leave your suggestions
for what the next video should be down in the comments and I'll see you in the next video.