Virtual Reality Coding 1
Unity is a cross-platform game engine developed by Unity Technologies, first announced and released in June 2005 at Apple Inc.’s Worldwide Developers Conference as an OS X-exclusive game engine
Section 1: Prefabs
Often times in our games, we will create a complex object in our scene that we want to use more than once. An example of this would be a vehicle with lots of moving parts or a building with interactable doors and windows. To do this, Unity provides an asset type called a Prefab which is short for “prefabricated GameObject”.
A prefab on its own is not a GameObject, think of it as a set of instructions for Unity to create a specific object. Just like you can use a single blueprint to build multiple identical houses. The blueprint itself is not a house.
Creating Our First Prefab
Prefabs are analogous to Classes in Object-oriented programming.
As the students make their way through this lesson, confirm their understanding of this concept by asking questions on the relationship between Prefabs and their instances.
If a student is struggling with this concept use the following analogy:
- A prefab is like a blueprint for a house.
- A GameObject created from a prefab is like the house itself.
- Many identical houses can be built from one blueprint, but a blueprint is not the house.
We’re going to make a simple prefab for demonstration purposes. Let’s set up our production environment.
- Create a new Project named “Exploring Standard Assets” in your projects directory.
- Save the current scene and name it PrefabDemo
- Right click on the hierarchy and create a new 3D Sphere
- Create a new material in your project window (we don’t need a folder for this demo since we only have one material) and drag it onto the sphere.
- Drag your sphere from your hierarchy into your project window and name it RedSphere.
Once our prefab is in the project window, it is saved. We can put this prefab wherever we like even if we switch scenes. The RedSphere in our hierarchy is now an instance of the RedSphere prefab. If we drag the prefab into the scene it will create another instance but both will be linked to the prefab. The same goes for duplicating instances.
Another nice thing about prefabs is that once we create them and place them in our scenes, any changes made to that prefab can be applied to change all the instances as well.
Create 5 duplicates of your RedSphere instance and change one of their sizes. Then, using the new Prefab toolbar at the top of the inspector (this only appears on selected prefabs) hit Apply and watch all the spheres adopt the new prefab specification.
You can see the power of this feature. Sometimes when working with a team of people, you’ll be making a scene where you need assets from the lead artist or a complex scripted object from one of the game programmers. By making a simple prefab that can later update all the instances in the scene, you can work on a scene now even if the assets aren’t finished.
Prefabs and Parenting
As our GameObjects get more and more complex, they are going to have children. Prefabs take all children into account when saving and instantiating. Let’s create an Empty Object in our scene and parent all of our RedSpheres to it. We can do this by selecting all of our RedSpheres and dragging into the GameObject. Let’s rename our Empty Object “SixSpheres” and drag it into our project window.
As you’ll see in the next section, almost all assets that will actually make it into the final version of a project will have children and will be saved as a prefab in the project folder.
Section 2: Importing Standard Assets
Unity is famous for being a very accessible engine. Part of this fame comes from all the content Unity makes available for students to use for free. Today we’re going to import the Unity Standard Assets. These are professional grade assets that we can interact with and see how they work. Let’s hop on over to the Asset Store tab and import the package.
Once you’ve found the asset pack, hit import and you should see a new window appear with a list of the assets to import. Hit Import to get all the assets in the package.
Section 3: Exploring the Asset Pack
Once the import is complete, we can see some real working game scenes in action. Before we dive into the details, however, it’s important we familiarize ourselves with the content we’re studying. Let’s play around with the demo scenes to see all the different things we can make with Unity. Find the demo scenes at Assets->Sample Scenes->Scenes in your project window. Now go through each one and press play to test it out. Take 20-30 minutes to try out each of the demo scenes. Talk to your partner about each one as you play them. Use the questions below each gif to facilitate this conversation. Try to use the vocabulary you’ve learned thus far.
Knowing what you know about Unity so far, how do you think these games work?
2D Character Scroller
- How might the types of assets we use change in a 2D game like this?
- What objects in this scene do you think have colliders, what about rigidbodies?
- What do you notice is different about the 3D space when compared to our default scene?
- What would you change to make the flying experience better?
- Pay attention to the audio of the car when you accelerate, decelerate, and turn. Hypothetically, what kind of tool would we need to replicate this effect?
- Under what conditions do the different sounds play?
- What other sound effects do you wish were in this demo?
Third Person Controller
- What kind of information do you think we need to make a character move like this?
- Pay close attention to the controls and what happens when you click on different keys.
- What does the mouse do?
- What do the WASD keys do?
Character Click Navigation
- What kind of games have you played that use this movement mechanic?
- Do you think this movement style could be used in a virtual reality game?
- What condition has to be true for the ball to be able to jump? When can it not jump?
- What differences do you see between the particle effects (smoke, water, etc.) and the solid geometry in the scene?
- What components do you think the objects in the different scenes have?
Section 4: Asset Analysis
Now that we’ve seen all the scenes in action, let’s take a look at the building blocks that make them work. Much of it is going to look very complex and maybe even scary, but keep in mind that all the individual parts are actually very simple and they add up to make something complex.
Aircraft Jet Analysis
The jet is a very interesting asset on its own. Open up the AircraftJet2Axis scene in the Scene view. Select the AircraftJet asset in the Hierarchy and watch what gets highlighted. You should see thin green highlights and thick orange highlights.
The first thing we’ll note is the green highlights. These are the colliders that specify what parts of the jet are solid. If we open up the Colliders item, we see that it has lots of children. Each of these is a collider, when any of these hits another collider in the scene (like the ground), they tell the Rigidbody in the AircraftJet object itself that it got hit and should apply a force. The colliders only sense collisions with other colliders, the Rigidbody is the one that actually calculates what the object should do.
Having multiple colliders in an object with complex geometry is much better than simply using a Mesh Collider for two reasons.
- Sometimes you may want to handle those collisions differently in the code. For example, hitting the wing might do more damage to the aircraft than hitting the wheels.
- Using a Mesh Collider makes the job really hard for the computer if the geometry is complex. The math that the computer has to do for a box or sphere collider is not all that intensive so we prefer to use basic colliders wherever possible.
The thick orange lines outline the mesh from the Mesh Filters in the AircraftFuselage and AircraftWingsJet GameObjects. Look at the children in this object and see that they all have a transform, a mesh filter, and a mesh renderer.
The models in the mesh filters were all made in an external 3D modeling program and then imported into Unity. A game development team typically has a designated group of artists that make assets like this so the game developers can put them in the project. We’ll take a deeper look into one of these programs in the next Module.
For now, just note that the mesh filters are purely visual and are not solid in any way.
The jet has two particle systems in Particles. These are really just the same particle system but mirrored. The particle systems are activated by the game scripts on the AirCraftJet when they’re supposed to be on.
We can also find another type of particle system which is much more subtle. If you play the scene again and fly around, you’ll notice that the jet also has two very subtle vapor trails coming out of the tips of the wings but only becoming visible a little further back. Little details like this add to the realism of the jet. These objects have a special type of particle system component called a trail renderer. We’ll cover those when we look at particles in the next section.
Sky Car Analysis
Now that we’ve seen the jet let’s take a look at our Car asset. Open the Car scene in the same directory as the last scene and click on Car in the hierarchy.
One thing that sets this model apart from the last one is its use of wheel colliders. In WheelsHubs you’ll notice each wheel has a Wheel Collider. A wheel collider is noticeably different from other colliders and has many more properties than say a capsule collider.
Wheel colliders in Unity are typically used in vehicles to support physics behaviors that are close to those observed in real-world vehicles.
The car also features a smoke particle system that blows out behind it. This is triggered by a script on the Car GameObject. We will go more into Particle Effects later in this course and how to design our own.