Welcome back, everyone, my name is Justin. And on this channel, we do VR tutorials. So today,
I'm going to be talking about how to do a very simple reset of your VR player. So a
lot of games sometimes need to have the player in a very specific spot, they need to know where the
player is. So an example like this would be Vito VR. So in Vito VR, you are sitting in a cockpit,
and the player has to be in a very specific spot. And so they give you this kind of reset feature,
which is actually really nice and really well done. So you are sitting in a chair
in like a briefing room, and then you hit the reset button, and it will reset you to an exact
point on the chair, based on you know where you are in relation to your room. So today, I'm going
to show you how to set up that reset button. So let's jump into the computer and set it up.
In this project, I'm going to be using the latest version of Unity. So that's going to be
20 21.2 point 19 F one, F one to the end just means it's a final version. And then I'm going
to be using the 3d U RP project to default that they give you. And we'll name this player reset.
Now that we've got unity up, I'm going to run through and set up the VR project using the XR
Interaction Toolkit real quick, I've already done several run throughs of this. And so in order
to keep this video short, I'm just going to skip right over it. If you want a little more detailed
breakdown of how to actually set that up this check out the video in the top corner, or
it's going to be linked down in the description as well. There we go, I've got the whole project
set up. And we are using open XR as well as the XR Interaction Toolkit as well as the sample starter
asset pack that came with the XR Interaction Toolkit. So that's everything I'm using.
But for this particular project, and script and stuff we're gonna go through, it will
also work for Steam VR, if you want to use that plugin instead or even the new Oculus, they're
new interaction system, you can use it with that as well. So first things first, let me just
kind of show you what I want to accomplish. Let me get my headset in link mode. And then
if I hit play right now, you'll see that my camera moves around with my headset,
obviously, and then I have my controllers as well. You can see in the scene view here that
my camera kind of moves around in if I even zoom in and get closer on this camera here.
And then let me bring the icons down a little bit.
There we go. So on the main camera, you can see it kind of moves around a little bit,
but our starting point actually was right here. So this was the center point. And our camera kind of
automatically starts above where the center point is. And you can kind of
think of the camera as existing inside of a box. And so the XR origin is not actually the
player per se. Unity recognizes the XR origin more as the room that you're standing in,
not the actual player. So as I'm moving all of my camera around, by moving the headset,
you'll notice that the position and rotation of the camera are moving. But the XR origin
as I move my camera around is not moving, it's staying in the same spot.
So if I wanted to reset this camera, by moving the headset, I actually can't do that manually off of
the main camera because I can't manually adjust any of these values. So see, as I
try to move it, I can't move and drag or even type new values in here because as soon as I hit Enter,
it just resets back to wherever the actual headset is in relation to the XR origin. So makes it
a little more complicated than just setting the position and rotation of the headset.
So what we're going to need to do so let's say for instance, I have a
little sphere that I want to snap my headset to whenever I reset I want my headset to snap
exactly to that sphere, wherever that sphere is facing. That's kind of what I what I want to do.
So the idea is okay, I'm going to have a some kind of transform game object that I
place and whenever the player hits the reset button, the camera moves exactly to that spot. So
in order to do that, we actually need to move the XR origin so you'll see when I move the
origin position the camera actually moves back and forth. And then the same applies for the rotation.
So if I rotate around the y axis was really the only axis you want to rotate around.
You don't want to rotate you know the extra Z axis because then things you really wonky.
So just why rotation and then the position of the actual XR origin is what we want to
do so So let's write a script for this and figure out how to actually do it,
I'm going to assume that you're going to want some kind of player controller. So we're going
to throw this inside of your player controller. So I'm going to create a new script inside of my
folder structure here. So right click, create a new folder. And we'll call this scripts
and accidentally put it inside of the XR folder. So I'll drag it back out into assets.
And then inside of scripts, let's create a C sharp script. And we'll call this player controller.
And this will help do any of the other things you want the Player Controller to be able to do. But
we really only need one method in order to accomplish this reset.
And then I'll put the player controller on the player.
And then I'll double click to open it. And my IDE is JetBrains a writer. So if
you're a student can get free access to this IDE, it is very nice, much better,
in my opinion than Visual Studio Visual Studio code. It integrates with Unity very, very nicely.
I say that just so that you know that some of the coloring may be different, and some of the
IntelliSense. So like you won't see any of these markers that are popping up above and my code,
mine's a little more advanced. And just think of it like I have extra plugins in my ID. But
you should be able to type in all the things that I type, and everything should work for you.
And shameless plug if you don't want to set this whole project up yourself. Or if you don't want
to type in all the code, you can get access to all of the source code from this project and
all the source code from any other previous project that I've ever done on my channel,
it's all going to be on my Patreon. So there's a link down in the description.
And I would really appreciate it if you contribute, because that helps me out that
helps the channel out that helps me and motivates me to make more videos. So it benefits you.
And I try to provide as much value as I can, in the Patreon members in the discord and
direct messages and just providing code and early access to videos, you get a lot of perks.
So let's jump into this code. First thing I always like to do is clean up a little bit of
the unnecessary things. So I'll delete those two USING statements. And I'm actually just going to
delete both the start and the update methods for now, you can have them in your player controller
if you like, and you're going to use it for other things. But for this particular case,
we don't need it, the only thing we actually need is we're going to do a reset position
methods. So this is going to be a public void reset position. And then we're going to need three
different public variables for this to actually function correctly. So we're gonna need the
actual point that we want to reset the player to that makes sense, we're going to need the actual
player location, so where their head is physically in the world. And then we're also going to need
access to the XR origin, which is technically the parent most parent object of the player.
But Unity kind of recognizes that as the room. So we basically need access to all right,
we're basically just going to move the room in worldspace, as what we're doing, we're gonna
pick it all up all the way up and put it down somewhere else. And that should reset our camera
to the correct place, those variables are going to be a also released field. So we're going to
keep them private, we're going to keep them private, but we want access to them in the
inspector. So first is going to be a transform. And this is just going to be the place that we
want to reset the player to so reset transform is what we'll call it. The next one is the player. So
another serialized field. And this can just be a game object, and we'll call it player,
this will be the actual room. So the most parent object of the player XR origin in our case,
and then finally, serialized field, and then another game object. And then this is going to be
the camera. So the camera of the player which is going to we're going to call it the player head.
And now inside of reset position, we can do a little maths and actually move the room around
and then the player inside of the room to the right spot. And the first thing we want to do is
rotation, make sure you do rotation first and then position because the player may be say in the back
corner of the room. And then if you rotate the whole room, then the player is going to move all
the way over to this side of the room. And so you have to reset the position again. So always rotate
first and then you can reset the position, less less things. So the first thing we need to figure
out is what is the angle of the place we want to reset to the reset transform, and then what is
the angle of the player's head and then we're basically just going to apply the difference,
and that's where the player is going to be facing. We're going to do a VAR rotation thing Go. And
this is just going to be why, because we only care about the y axis, we don't care about
moving the we don't want to tilt the whole room, we just want to rotate it on the Y axis.
So we're gonna take the difference between the player heads, so we always use the player head
as the reference, because that's where the actual cameras and then the transform on that camera,
and then the rotation on that transform. And then EULAR angles, which basically converts our
quaternion rotation to a vector three of angles, and then we only care about the y axis of that
angle, and then we're going to subtract that by the reset transform version of that. So
restart, reset transform, dot rotation, dot EULAR angles, dot y.
And this will give us the difference between the angles. So we are getting the rotation
angle. So it's basically basically the difference between the player's head and
the reset transform. And then all we need to do is actually apply this angle. And we're going to
do that on the player. So player dot transform, rotate. And this applies a specific rotation.
And we give it a vector three, in this case, we want a zero on the x axis, we want
a rotation angle Y on the y axis and then a zero on the z axis, you'll actually notice, since I've
already tried this out before, I've noticed that you actually have to invert this rotation angle,
either inverted here, or you could swap these back and forth, and basically just cut this and put it
here and then just say, reset, transform dot rotation, and then this can be the positive value.
So either way, if your player is actually, you know, facing further away from the, you know,
rotation that you're intending, then just try flipping the sign in front of here as well.
You'll also notice that my code editor adds in the this IntelliSense here,
so it automatically knows that this is the x angle, this is the Z Z angle, this is a float,
this isn't actually code, this is just something you know that it's telling me it recognizes,
okay, this is going to be a float. And so just I say this just so you don't actually type in
colon float, because that will give you errors. So if you see anything that's like grayed out, make
sure not to type that part. All right, so we have rotation, so this should rotate you to face the
correct direction, but you're going to be pointed the right direction, but facing the butt in the
wrong position. So we want to remedy the position. Now. So far, this is going to be the distance.
And we'll call it distance diff the difference between the distances and then
we're going to do the reset transform dot position subtracted by the player head
dot position dot transform dot position. And I'll make that
on the new line just so it's easier to see. And then all we have to do is add that distance to
the player dot transform dot position. So we're going to do plus equals was basically just means
add whatever's after this to the existing thing. And we'll say distance difference.
So you'll notice a pattern pattern here, we're trying to find the difference between the reset
transform and the player head. But instead of applying the difference to the player head, we're
applying it to the actual player, which in this case is the room that we are existing in at the
moment. And that's all you need. This is this few lines of code is all you really need. And you just
need to call a reset position. Whenever you press a button, or you know, some action occurs you all
you have to do is call reset position and it will automatically reset your player.
And if you're interested in getting into button presses and figuring out how that works, then
I have a video that you can also check up here in the top corner. And it will show you how to
you actually apply different methods and stuff on a button press but in this particular case, we're
going to do a really easy thing that is called the context menu. And we're just going to call
this reset position. Make sure you save and all this allows me to do is right click on the actual
script and then click Go button and swapped over to it. So it's makes it easy. Alright, so over
on our XR origin, you'll see we have our Player Controller. And now if I right click, you'll see
reset position is there. And so when I click on this, it is actually going to call our method.
It goes ahead and calls the method. So that's what context menu does here.
And again, don't type item name, colon, or vector, three colon, or any of these, if you're
following along, and last thing we need to do is set up the actual inspector thing. So the
reset transformer is going to be where we want to set our object to. So we're just going to create
a sphere, we'll call it reset point.
And I'll actually reset that to the center and I'm gonna scale it down as well. And actually,
just for fun, we're gonna move it over here, and then we're going to rotate it around a little bit,
there we go. So it's, you know, kind of offset and then kind of pointing in a
slightly different direction. And then I'm actually just going to duplicate it and then move
another sphere out, just so we make sure we are because we're going to our camera's gonna be
right on this point. And so I just want to make sure we're facing this other reset point, when
we move just just to make sure we're oriented in the right direction as well, when we're testing.
So back to the script are going to drag and drop the first reset points, so the actual one we want
to move to, and then the player is going to be the XR origin. And then the player head is the
main camera. And you can set these up in the start and awake methods to automatically set themselves.
And so you don't have to do this. There are many ways and approaches to do this. This is just a
very simplified, dumbed down tutorial version of this, this is how you get it working at the bare
minimum, and then you can go back in and add in some, you know, whatever is best for your game,
and performance. So now, if I hit play here, I don't even have to put the headset on because
I'd put a little piece of tape on the back of the sensor there. And so I can just
move it around and the headset thinks that I'm wearing it, which is nice, so it doesn't turn off.
So there we go. And you'll see in our Scene view that I'm actually way up here at the moment.
So if I click off and keep in mind, if I click off of the Scene view or the game view, it's going to
pause the game. And so my headset won't Orient. So let's see. If I right click here and see now
it stopped orienting. So it's just you need to make sure your headset is where you want it to be.
When you click when you right click, and then that'll do it. And then we
hit reset position and there we go where you are facing you'll see
that my camera is actually pointing in the direction of the second ball, which is correct.
Here, I'll move the Scene view over a little bit just so we can see that a little better.
So if I put the camera down and over here, right click reset position,
you'll see I'm pointing in that direction. And even the controller's lines are pointing in that
direction now because they haven't been set. So if I move over here, right click Reset.
And it moves my camera and angles at the correct angle every single time. So you can imagine how
useful this would be in certain games, especially games that require some kind of
sitting like a NASCAR game or you know racing of some sorts like that, or flight simulators,
anything where you have to be stationary, but the player has to be in a very specific spot.
This is very useful to know how to reset the player to have a specific spot and it's a little
more difficult than it might seem right off the bat. You can't just you know, set the player's
position to whatever point position you want it to be, you actually have to do a little bit of math
in there. So just wanted to show you how to do this real quick. If you want to just download this
project or download the dot Unity project and add it to your existing project. Then join the Patreon
I have all those files for you available. And if you are trying this out yourself and you're around
issues, then join the discord. We have a dev help channel for you to ask your questions in. And
otherwise, I really appreciate you watching the video and I'll see you next one