HW3: The Room, A Unity Step-by-Step Tutorial¶
Due date: 2024-08-28 23:59.
Instructions¶
You need to make sure you have Unity Engine installed. If you don’t have it already installed, download it from here Download Unity Hub. Note that there are many versions available. The latest LTS (Long Term Support) version should be a safe bet for maximum compatibility, but the latest stable release should also work. Unity Hub can be used to manage Unity installations, and is the recommended way to go about installing Unity. Remember to include the Android Build Support and Visual Studio when installing the editor from Unity Hub.
- This document provides a complete step-by-step Unity installation instructions.
As you go on completing your homework assignment, you are more than welcome to add extra features, make it more complex than required, or use the work here as a basis for your other future assignments. The following instructions are written so that you should be able to work independently and you can return your assignment early. If you are unfamiliar with programming, you can check out this C# tutorial before the exercises. If you are not using VR Cardboard, you need to adapt the instructions to your own HMD.
Now, proceed with the following step-by-step instructions to complete your assignment.
Creating a Unity Project¶
Create a
Project
in Unity. To create a new project, click the New Project
button on the top right.We will use the 3D (Build-In Render Pipeline) template:
Get acquainted with Unity’s basic features and the interface: Interface Tutorial.
Part 1. Setting Up the Room and Unity UI Basics¶
1.1 Player Controller in VR¶
To get started with setting up Cardboard VR for your project, you can follow the instructions behind this link: https://developers.google.com/cardboard/develop/unity/quickstart. We have also included some instructions and images here for you to follow, but if there are problems, refer to that link for details.
At this point, you should open the Package Manager (from the Window tab in the toolbar at the top), click on the "+" button in the top left, add from git URL and paste
https://github.com/googlevr/cardboard-xr-plugin.git
It should then add the "Google Cardboard XR Plugin for Unity" as seen in the image above. Unity may also prompt you to restart the editor, so you should do that. However:
- If this approach doesn't work, and you do not see the "Google Cardboard XR Plugin for Unity", try downloading the above Github repository as a .zip, unpack it, then use the option to "Add package from disk" in Unity and select package.json file.
- Also, import the "Hello Cardboard" sample scene included in the package, by clicking on
Samples
category and thenImport
button!
Next, find your "HelloCardboard" scene in the bottom left part of Unity window under Project -> Assets -> Samples -> Google Cardboard XR Plugin for Unity ->... (all the way down in Scenes directory) and open the scene by clicking on it. Then please follow the Configuring Android project settings and Configuring HelloCardboard scene sections here, while keeping the following in mind:
- All of these steps are important!
- For the graphics API, you must first disable "Auto Graphics API". Then, it's recommended to remove Vulkan to keep things simple.
- For "Target Architectures", it's recommended to enable ARM64.
After going through the setup, your project should be ready for its first build using the sample scene! So go ahead and open the sample scene you imported earlier. It should look like this:
In order to make a build for your phone, you must enable USB debugging on your phone. Follow these instructions to enable this setting. When connected to your PC via an USB cable, your phone may also ask you to authorize the connection, which you need to confirm. If all of this is setup correctly, your phone should show up as an available device in the build settings window:
Once your own device is selected and the sample scene is added to the build (as seen above, you can just click on "Add Open Scenes"), click "Build And Run", save the .apk file anywhere you like with any name, and unity will automatically transfer it to your phone and run it. You don't actually need to do anything with the .apk file on your PC unless you want to share it. Note that the first time you build a project is going to take a long time, most likely several minutes. Subsequent builds will be faster. Once it's done, it should start up on your phone and look something like this:
You will probably see some prompts about giving Android permission to use the camera, which you should allow, and scanning a QR code to configure the view for your Cardboard. You should use the configuration you found appropriate from the Setup and Calibration task.
Next, let's start adding some things to this room.
1.2 Planet and Moon¶
Create two spheres
GameObject → 3D → Sphere
. Scale the first sphere to 2 in all directions, and place it in the center of your room (0,4,0). In the Hierarchy
view, drag the second sphere onto the first. The result should look like this:Now, the second sphere is a child of the first sphere. When you change the position, rotation, or size of the parent sphere, its child will also undergo the same movement, rotation, or scaling. The (0,0,0) origin position of the child is now its parent’s position, not the global (0,0,0) origin. That is, the child’s position is an offset from the parent’s position. Finally, if the parent rotates, then the child will rotate about its parent’s axes, not its own axes (this will make more sense later). See the Unity hierarchy manual for more information.
Set the position of the child sphere to be (2,0,0), which is four units from the parent sphere on the X-axis. For why this is, think about the scaling of the parent sphere.
1.3 Material¶
Read the Unity materials, shaders and textures manual, focusing mainly on the materials, for now. A base texture (ground_0010_color_1k), and a normal map (the weird purplish image ground_0010_normal_opengl_1k) can be found from the these provided files. To create a material, go to
Assets → Create → Material
.This will generate a default material, which should show up in the
Inspector
tab like so:Drag the ground_0010_color_1k image to the box labeled
Albedo
. Now, drag this material from the Assets
folder onto the spheres in the Scene
view. If this doesn't work, select the sphere, and in the Inspector
view, drag and drop your material on top of "Default-Material" or click on the circle and select your material.It probably doesn’t look too good. Don’t worry, it’ll get better. Drag the ground_0010_normal_opengl_1k image to the box labeled
Normal Map
in your material. It may warn you that the texture is not marked as a normal map, in which case you can click the "Fix Now" button. Notice how it changes the visual perception of the material. A normal map is a trick used to give the illusion of depth on a flat surface, by telling the engine to reflect light as if there were these little bumps and pits in the material.If you wish, you can try moving the light source in the scene or adding other lights. The result should look something like this:
Feel free to try adjusting the other parameters as well, such as
Smoothness
and Metallic
and see how they affects the material!Finally, create a material that has no albedo or normal map. Next to the
Albedo
option there is a small color box. Since this material has no albedo, the material will be this flat reflection color. Try and see what happens when you change the color of a material with an albedo. Create another object, such as a cube or sphere, and apply this material on it.1.4 Text¶
Create a Text object
GameObject → 3D Objects → Text - TextMeshPro
. Unity will probably prompt you to import TMP Essentials, so go ahead and do that. Then the text should appear, but it might be really big and outside the room, so try scaling it down and moving it inside the room and placing it on a different wall than the already existing text.As you place the text on the wall, don't try to find the exact coordinates and put it there. This can result in what's called Z-fighting, where the renderer can't consistently determine which object to draw on top. You can see an example below that demonstrates this problem:
Part 2. Scripting in C#¶
2.1 Basic Scripting¶
Unity scripts use the C# language. If you are unfamiliar with programming, you can check out this C# tutorial. You’ll only need the basics of objects, classes, and variables for now.
We recommend reading the Unity scripts manual and the script object API.
Unity’s Scripting API Reference is a useful source of information for when you are scripting new objects. For the first script, we will detail the functions that we recommend, but for the others, we expect that you will refer to the API reference if needed.
2.2 Orbiting Asteroid¶
Let's try making the the small asteroid orbit around the bigger one. To start, select the bigger, the parent asteroid, and click on
Add Component
. Type a name for your script in the search bar, such as "Orbit", and click on New script
.This new component should now appear in the inspector view for that object. To edit it, you can right click it and choose
Edit script
or double click the script file in the Assets
view. If you have Visual Studio installed and configured, that should open, but you can technically edit the scripts with any text editor.The easiest way to do this is to have the bigger sphere constantly rotate. Since the smaller sphere is a child of the bigger one, it will rotate around it. You can control the rotation and position of a GameObject with the transform object variable, an instance of the aptly named Transform class. Use the functions of this class to rotate the planet system around the Y-axis. Keep in mind that the Update function runs every frame, but frames often vary in real time length. Thus, using static rotation amounts with the Transform class will make the apparent rotation amount depend on framerate. This is generally not desirable. To use real time frame times instead, use the Time.deltaTime variable.
Note: If you test with play mode in Unity editor, there will be an error about Cardboard XR Loader, but you can ignore it. From the console view, disable
Error Pause
to prevent the execution from pausing because of it.2.3 Gaze Interaction¶
In the case of Cardboard, our input options are limited. The main input method is gaze interaction. This is already implemented for that color changing object in the sample scene, but let's try doing something similar of our own. Add a script to that textureless colored object you added earlier, or make an object like that now. In that script, define
public void OnPointerEnter()
and public void OnPointerExit()
. These functions get called because they are defined in the Cardboard Reticle Pointer
script that's included in the template, which can be found under the Player
object.Also, if you haven't done it yet, make sure to set up the
Interactive
layer for your project according to these instructions.Let's try changing the color of this object when it's getting looked at by directly changing the color property of the material. For this, you can use the
GetComponent
function to access the Renderer
component, which then has the sharedMaterial
property, which has the color
property. Look at this example and browse the Unity documentation to try and put it all together! Make sure that your object is also in the Interactive
layer.Part 3. A More Interesting Room¶
Note: This is for your own learning and not a mandatory step for this assignment. If you are in a hurry, there's no need to stress about this.
Instead of this template room, use a modeling tool to create your own room. Please write down what you created. Your creation still needs to have the other features of the room we described above.
Blender is a free and powerful 3D modeling software, but you may also use something else if you prefer.
The default Unity modeling tools are extremely limited, so we highly recommend you familiarize yourself with another tool. It will assist greatly in your final project. You can use the room as a basis of your course project.
At this point, before moving on the next homework assignments or the project, it's a good idea to familiarize yourself with version control. Here is a quick tutorial for using GitHub Desktop as a way to backup and manage changes to your Unity project: GitHub Desktop for Unity
Authors¶
Elmeri Uotila.
Give feedback on this content
Leave your comments below: