Tutorial1 - PushyPixels/CookingWithVR GitHub Wiki
Heya! This is the text tutorial for the new show! It is currently under construction so please pardon the debris!
We're going to be doing VR development here, so you'll need a headset. The instructions I've written are supposed to work for all commercial 6dof headsets. That said I do not have access to most headsets! So this is only tested using a Quest with Virtual Desktop at the moment. If you find problems, please create an issue and contact me so that I can work with you to get things figured out.
Writing these text tutorials is exhausting for me so don't expect them for every episode! I'll try and open this wiki up for user edits ASAP so that more tutorials can be added in case I can't get to them.
First off, you are going to need to install Unity3d version 2019.3.15f1 if you haven't already. The tutorial should work just fine with many earlier versions, but if you are starting fresh, may as well start compatible! When you install, I recommend adding the Windows Build Support (IL2CPP) module; otherwise you may get errors when you try to build on Windows, assuming you don't change the scripting backend in the settings, which will not be covered in this tutorial.
Note: if you are currently in the middle of an important Unity project of your own, do not replace your installation of Unity3d! I highly recommend installing Unity Hub and using that to install 2019.3.15f1 (UnityHub installation link: unityhub://2019.3.15f1/59ff3e03856d) as an independent installation. This will protect you from the update breaking compatibility within your game project. Upgrading a project is always a risk and a potential time-sink.
While Unity is downloading, assuming you don't already have it, get git! If you know nothing about git, don't be overwhelmed by all the settings that it asks about. Just go with the defaults. We're on a website called "GitHub" may as well have the tools for it! <3
Open Unity or Unity Hub and create a new project.
The project we are making is intended to be as universal and future-proof as permissible, so we'll be selecting the Universal Render Pipeline starting package. This will give us access to many advanced rendering features and allow us to use ShaderLab, which is a really fun way to explore shaders without having to learn to write them yourself.
Once the project is opened, you should see a scene with some carpentry tools in the editor. It's a great reference scene to see how to set up post processing effects, but we're not going to use it for right now. Go ahead and create a new scene by clicking the File menu and selecting New Scene from the pulldown. Now go into File again and choose Save. Give your new scene a name, and save it into the Scenes folder. If the scene doesn't automatically open, double-click the scene in the Project window to open it. The carpentry tools should disappear and you should be presented with a mostly-empty scene.
Right now, you can hit the Play
So our first order of business is to get our head in the game. First, we're going to enable VR. Open the Project Settings window and click on the Player tab on the left column. Expand the XR Settings header and under the scary-sounding Deprecated Settings heading click the checkmark next to Virtual Reality Supported. It should automatically add the appropriate SDKs for your device.
Why are we using a deprecated option? Even now, VR is still very much in flux, though it's stabilized a lot since I last tried writing a tutorial up. The legacy XR support is currently more compatible with a wider range of headsets. Almost everything we learn in these tutorials will work in the new XR Plugin system as well, so if you'd feel more comfortable setting that up, please do so! But it is beyond the scope of this tutorial.
Hit the Play
It's a little awkward floating in space. Let's give ourselves a floor. Click the plus + button at the top of the Hierarchy window, choose 3D Object->Plane. It should appear at 0,0,0 in your scene. If it does not, click on the Plane in the Hierarchy and in the Inspector window set the Position and Rotation fields of the Transform component to 0's. (You can do this automatically using the Reset option in the triple dot ⋮ menu to the right of the Transform heading.)
Now make sure the Main Camera is at 0,0,0 for rotation and position as well.
Why put our camera on the floor? Unity uses the camera's starting position as the floor reference by default, so when you launch the game in your VR headset, you should see our new floor at your feet, as you'd expect. Go ahead and hit the Play
Unity finally has a new input system and it desperately needed one, as the old one is very limited and had a lot of problems. It's not built-in though so we'll have to install it first.
Open up the Package Manager window and wait for a moment while the list fully populates. After it does, click Input System in the left tab list, and click Install in the lower right of the window. It will give you a big warning, go ahead and hit Yes as this warning is mostly for people who already have a functioning project; ours is brand-new so we haven't used any legacy input. The Unity project will restart (hit Save on your scene when it asks!) and you'll be back where we were before, but with the new Input System installed.
Okay, so, first, we're gonna do something that doesn't seem necessary, but may as well clean things up right now, and it will be good practice for the next step. Unity by default tries to make VR stuff work as easily as possible, which means they automatically assume that you want your Main Camera to move with your head. But we really should explicitly tell Unity we want to track our headset position using the Main Camera. To do this, we'll add a Tracked Pose Driver component to our Main Camera.
Click the Main Camera in the Hierarchy window and scroll down in the Inspector window till you see the Add Component button. Click it and type in "Tracked Pose Driver" and click Tracked Pose Driver (New Input System) in the list. This will add the component in the Inspector.
The Tracked Pose Driver must be configured to work properly. Leave the Tracking Type and Update Type to what they are set to and click the plus + button across from Position Action and choose Add Binding. A new binding labeled will appear. Double-click this binding and click Path->XR HMD->◉centerEyePosition. The binding should now read centerEyePosition [XR HMD].
Do the same for rotation: click the plus + button across from Rotation Action and choose Add Binding. A new binding labeled will appear. Double-click this binding and click Path->XR HMD->◉centerEyeRotation. The binding should now read centerEyeRotation [XR HMD].
Go ahead and hit Play
We'll be using spheres to represent our meathooks. Go ahead and make a sphere in your scene by pressing the plus + button on the Hierarchy window and choosing 3D Object->Sphere. If it's not already selected, select it in your scene by clicking Sphere in the Hierarchy window. It doesn't really matter where we put this sphere, but we should make it smaller as our hands are way smaller than 1 meter! In the Inspector under the Transform component change the Scale values to 0.05, 0.05, 0.05 to make the sphere 5 cm in size.
Like we did for our head, scroll down in the Inspector window till you see the Add Component button. Click it and type in "Tracked Pose Driver" and click Tracked Pose Driver (New Input System) in the list. This will add the component in the Inspector.
Now, we follow the same steps that we did above for our head, with slightly different settings so that Unity understands that this is our left hand, not our head. Leave the Tracking Type and Update Type to what they are set to and click the plus + button across from Position Action and choose Add Binding. A new binding labeled will appear. Double-click this binding and click Path->XR Controller->XR Controller (Left Hand)->◉devicePosition. The binding should now read devicePosition [LeftHand XR Controller].
Do the same for rotation: click the plus + button across from Rotation Action and choose Add Binding. A new binding labeled will appear. Double-click this binding and click Path->XR Controller->XR Controller (Left Hand)->◉deviceRotation. The binding should now read deviceRotation [LeftHand XR Controller].
Now, we can make our right hand. Simply duplicate the sphere in the scene (Select it in the Hierarchy, right-click, Duplicate) and then change the devicePosition and deviceRotation bindings to use the XR Controller (Right Hand) bindings instead.
Let's test it! Hit the Play
Finally, rename your spheres to "LeftHand" and "RightHand" so that you know these are your hands. You can do this by clicking each sphere in the Hierarchy and then clicking the name again to rename. Optionally you can drag them onto your Main Camera so they are child objects for Scene organization purposes.
Save your scene, save your project, and we're done with Tutorial1!