In this video, learn how to write code that allows the user to fluidly move in your scene using the immersive controllers.
- Why don't we start by going over to your machine and looking at how to build movement using the controller? So, using the joystick on the controller, for example. - [Stefano] Let's go straight to the code. In here we have created this class called HandCtrls_FluidMov. The reason we called it fluid movement is this class will allow the user to move in space in a fluid way, try to not be too sudden.
The user will be able to use the thumbstick back and forward to move towards the direction in which the user is facing. - [Neil] So, we're always keeping them moving forward in a natural way. - [Stefano] Exactly, that's what we do as humans. We move towards what we're looking at most times. - [Neil] We have front-facing eyes and we use them to move forward. - [Stefano] This class, you notice it's very, very similar to the controllers class that we have shown at the very beginning where we can track all the buttons that are being pressed.
We start with the left and right controller, we grab the index of each one of them, we always catch the position and the rotation of each controller, but we're very much going to use just one for this example. In here, we get the position, the same loop that we went through in the previous class-- - [Neil] And this is in our update-- - [Stefano] Yes, this is always in our update method. What we have added here is...
We use the ControllerIndex against the interactionSourceStates and we can extrapolate a button in particular. In this case, is it thumbstick? We can see what the value of the X axis of the button is. In this case, the axis is this one here, going up and down. That is our Y axis. What we do here is that we check the value against the 0.3 and this minus 0.3.
The value of the Y axis in the controller goes between one and minus one, zero being the neutral pose. - [Neil] So, zero is the middle. This magic number that you've created, 0.3 plus or minus, is really your tolerance. So, if the thumbstick has not moved more than that in either direction, you ignore it, essentially. - [Stefano] Exactly. We start taking into account the value of that Y axis only when it is above or beneath that threshold.
Once we detect the movement of this thumbstick above 0.3, what we do is we read where the user is facing towards using this variable, cameraForwardDirection. We set the forwardDirection Y to zero, and the reason for doing that is that if you're looking up, the forward direction will be exactly the direction you're looking at.
The thing is that if we don't set the value Y to zero, when you press forward you will start flying up in space. - [Neil] Which is unnatural and unexpected. - [Stefano] Which is unexpected, unless you're developing a Peter Pan simulator. Since we wanna keep the same Y-- - [Neil] So, this is world Y? This is a little bit confusing because you've got the thumbstick Y, which is the up on the thumbstick, and you've got world Y which is the up in the world that you were in when you're wearing the headset, and they're not the same thing.
So, we're using the thumbstick Y to determine how much to move forward or back, and we're using the world Y to re-level ourselves to make sure that whatever direction we're looking in, we reset it down to look straight rather than up at an angle or down at an angle. - [Stefano] That's exact, yeah. Once we have set the Y of the camera to zero, we keep it on the same plane, we allow the user to stay and walk on the same plane constantly.
Then, we apply this method here on the transform of the camera. - [Neil] It's really the game object that holds the camera that you're transforming, right? - [Stefano] Yes, actually, to be precise, it is the game object that is the parent of the camera. (mumbling) - [Neil] And by doing that, the controllers come with us as well because they're inside that game object. - [Stefano] That's right, yes.
All the (mumbling), one is gonna be the camera and the meshes of the two controllers are gonna follow the parent. This translate method requires a direction where this transform is going to move towards, and we use this calculation, which is the forward direction of the camera, the time, which increases every second, the interactionSourceStates Y value, and we use this SourceStates Y value because we want the speed to change depending on how much the user is moving the thumbstick.
- [Neil] Right, so if they push all the way forward, you want it to be faster than if they've just nudged it a little bit. - [Stefano] Yes. So, if a person is not comfortable with the speed, they can simply not go all the way with the thumbstick and move slower. - Great. - [Stefano] We obviously do this calculation on both if it goes above 0.3 and below 0.3. Obviously, you can change these thresholds if you want to increase the tolerance.
- [Neil] OK, so, it's pretty simple actually when you think about it. You're really just using the thumbstick to translate the position of the game object in a direction at a velocity. - [Stefano] Yes. - [Neil] Should we see it running? - [Stefano] Yep. Let's go back to Unity. This is the class that we have. We dropped it onto the camera parent, we have set the left and right controller as usual, and we have set a speed.
Now, this speed is part of that calculation. It's here. - [Neil] So, this is our velocity, essentially, that we can go at, and you've made that a public variable there, nice, so that we can set it within Unity so that we can play around with it a little bit without having to change the code. - [Stefano] Exactly. We can go back here. We found that two is a good number to create good movement. Let's try. Again, as we did before, we press play and we hold down the play button while we put on the headset.
- [Stefano] I hear the magic noise. That means he's into MR world. - [Stefano] Yes, I seem to be, OK, yeah, I am. You will notice that when you start, it's gonna take a second or a fraction of a second to pick up the (mumbling) but here I am. I don't have my sphere anymore. I've added a nice 3D model of a hand. - [Neil] That's very flash. Essentially, you've replaced the sphere model with a hand model, and of course, this has bones and things in it, so you could actually grip and you could write some script code to change this.
But that's not what we're here for, although it's very beautiful, Stefano. Let's move around. Let's use the thumbstick to change directions. - [Stefano] If I press slightly forward, you can see that I'm moving forward. If I go back, I move back. You notice that I'm actually moving forward depending on what I'm looking at. All I'm doing is pressing forward with the thumbstick and I'm slowly moving forward. - [Neil] Yes, that's very nice. - [Stefan] So, if I turn my head, while I'm turning my head, all of a sudden the forward and back action change while I'm actually moving.
If I look in the front and press forward, then if I look in a different direction, I move towards that direction. - Nice, that's awesome. - It's very smooth, and-- - And it doesn't feel too jogging to you? It doesn't feel like you're jumping from one place to another? It's nice, smooth, and that's important to get right as you move around this, to feel that. If you're using this kind of mechanism to move, it's got to feel smooth, it's got to feel natural in a way that works and feels comfortable to you as a customer wearing the headset.
- And leaving the user the ability to change the speed if you use this kind of movement, it's much better because you allow the user to be more in control in case they feel that it's too fast, they can slow down, and vice versa.
- Mixed reality
- Setting up the camera and scenes
- Gaze in Unity
- Building movement and teleporting
- Setting up audio in Unity
- Conditional compilation in Unity
- Creating simple models using Paint 3D
- Topology and polygon count
- Normal maps, bump maps, and CrazyBump
- Fixing issues using Maya and Visual Studio
- Exploring the frame debugger
- Building an application in Visual Studio
- Submitting an app to the Windows Store