Published using Google Docs
Unity Game Engine: How To Guide
Updated automatically every 5 minutes

Unity Game Engine: How To Guide

Summer 2021

This guide is available digitally: https://go.ncsu.edu/unityguidevr

Table of Contents

What is It?        2

How to Get Unity Started        2

Building a Project        2

Unity’s Interface        4

Creating Gameplay        5

               Scenes                                                                                                                                          5

                Game Objects                                                                                                                               9

                Prefabs                                                                                                                                         11

                Layers                                                                                                                                          12

                Constraints                                                                                                                                  13

                Implications for Animation                                                                                                           17

                Lights                                                                                                                                          17

                Rendering Paths                                                                                                                         18

                Cameras                                                                                                                                     18

                Publishing Builds                                                                                                                        19

Other Information?        20                                                                                                               

What is it?

Unity is a 2D/3D modeling software that allows you to code and build these video game/animation elements. Unity utillizes C# for its coding purposes.

How to Get Unity Started: Installation

To start using Unity, you must download and install Unity Hub, where all of your Unity projects and other assets are stored. Go to the Unity website to download Unity Hub https://unity3d.com/get-unity/download for either a Windows, MacOS, or Linux desktop.

From there, you must install Unity Editor. To do this, go to the Installs tab. For MacOS, it will look like this:

        /Applications/Unity/Hub/Editor

For Windows:

        

C:\Program Files\Unity\Hub\Editor

Once installed, click “Add”, then “Next”, and install the necessary modules for your Unity experience.

For more in-depth installation instructions, go to https://docs.unity3d.com/Manual/GettingStartedInstallingUnity.html

How to Get Unity Started: Starting a Project

Within Unity Editor, click on “New”, and choose the template with which you want to work with. Name the project, and choose a location to store your project.

Your Projects Tab is stored in the Hub and manages all of your Unity projects. In Unity Editor, to open a project, go to File > Open Project.

Choose a Project Template that fits your project needs. These templates include 2D, 3D, 3D with Extras, High Definition RP, and Universal Render Pipeline. To learn more about these templates, go to https://docs.unity3d.com/Manual/ProjectTemplates.html 

If you want to delve deeper into what you can build and code within Unity, click on the Learn tab within Unity Hub. Go to https://docs.unity3d.com/Manual/learn-tab.html to read more about this.

Introducing the Unity Interface

Unity’s interface provides many tools and areas that help regulate and create the gameplay. These include commands and tools such as:

Toolbar: Creates the workspace. The left controls Sceneview and GameObjects. The center allows for pausing, playing, and step controls. The right allows for access to Unity Collaborate, Unity Cloud Services, and your Unity Account.

There is also a layer visibility as well as the Editor layout menu. The Editor layout menu allows you to change the layout for the Editor windows as well as create custom layouts.

Hierarchy window: This is a hierarchical text representation of GameObjects in your Scene. Items within the Scene have an entry in the hierarchy, so the windows are linked. The hierarchy shows how GameObjects attach to each other.

Game view: shows how the final rendered version of the game will look like. You can see for yourself how the scene plays out through the Scene Cameras by clicking the Play button.

Scene view: allows you to visually navigate and manipulate the Scene. This view shows 2D or 3D perspectives, depending on the project being created.

Inspector window: allows you to view and change the properties of the GameObject. The layout and contents of the Inspector window change with every selection of a different GameObject, because each GameObject comes with a different set of properties.

Project window: shows the Assets library that you can use in your project. Imported Assets into the Project will display here.

Status bar: contains notifications for different Unity processes and also provides access to related tools and settings.

Creating Gameplay in Unity

Creating GamePlay in Unity can be done by anybody. But, in order to create a seamless GamePlay, there are foundational workflow concepts to understand.

Scenes 

This is where the Unity contents show up when you create your GamePlay. It contains the game assets for a part of a whole game or application.

Every scene opens with a camera and a light source. Scene templates are used to create new scenes, and they are assets that are stored in a project. These are meant to be copied instead of used directly.

To create a new scene from a scene template, go to File > New Scene or hit the Ctrl/Cmd +n shortcut keys.

Components of scenes:

        Search field: search by name and find available scene templates

        Templates: a list of available templates

        Template details: shows information about the template selected

        Command bar: has commands and options for creating a new scene from the selected template

To open and edit a template in the Inspector window, click “Edit” link listed under it

Multi-scene editing allows you to have more than one scene open in the editor at once. To open a new scene and add it to the current scene hierarchy, do one of the following:

        Click “Open Scene Additive” in the context menu for a scene asset

        Drag at least one scene from the Project Window into the Hierarchy Window

 

Multiple scenes open in the editor window will cause the scenes’ contents to be displayed separately in the hierarchy window. These will appear below a scene divider bar which displays the name of the scene and its save state.

Scenes can be loaded or unloaded to show/hide GameObjects that are contained in the scenes. This is not the same as adding/removing them from the hierarchy window

To save scene, click “File” > “Save Scene” or press the Ctrl/Cmd + S shortcut keys

The context menu in the scene divider bars lets you perform other actions on the selected scene

Scene Templates

Scene templates are just preconfigured scenes implemented at the start of a project.The most basic scene template just contains a camera and a light. You can also create your own scene template for your GamePlay.

        Creating new scenes:

        To create a new scene from an empty template in the current project folder, go to Asset > Create > New Template

        To create a new scene from an empty template in a specific project folder, either:

  1. Go your Project window and right-click it to open the context menu
  2. Open the folder in the Project window, right click the asset pane to open the context menu
  3. Go to Create > Scene Template

        To create a new scene from an existing scene asset, open the Project window and either:

  1. Right click a scene asset to open a context menu then click Create > Scene Template From Scene
  2. Select scene asset, then from Main Menu, click Assets > Create > Scene Template from Scene

        To create a template from a current scene, go to Menu > File > Save As Scene Template

        

You can also create scenes from your C# scripts

To learn more about Templates and scene creations, go to https://docs.unity3d.com/Manual/scene-templates-creating.html

Editing Scene Templates

To edit a scene template, select that scene in the Project window and then open it in an Inspector window.

The scene template inspector has different properties:

  1. Details: specifies which scene the template uses, and has the template description that appears in the New Scene dialog.
  2. Thumbnail: Provides options for creating a preview image for the template
  3. Scene Template Pipeline: Specifies optional custom script to run when Unity creates a new scene from the template
  4. Dependencies: Lists the template scene’s dependencies, and specifies whether Unity duplicates them when it creates a new scene from the template

Customizing new scene creation

You can create a New Scene Template Pipeline script to connect your custom code to the template.

How to connect the script to the template:

  1. Inspect the template to edit its properties
  2. Set the Scene Template Pipeline property to point it to your Scene Template Pipeline script

You can also use the SceneTemplateAsset.templatePipeline method to connect the script to the template with C# coding.

It must come from the [SceneTemplatePipeline] interface or the [SceneTemplatePipelineAdapter]. The script should implement the events you want to react to, such as BeforeTemplateInstantiation or AfterTemplateInstantiation in your coding script.

Scene Template Settings

To control template Project settings, go to the Project Settings window (Edit > Project Settings) and click Scene Template.

For New Scene settings, go to File > New Scene or use the Ctrl/Cmnd + n shortcut keys.

For Default Types settings, this controls whether Unity duplicates specific types of assets by default when it creates a new scene from a scene template.

To make Unity duplicate a certain type of asset by default, allow the Clone option (2) for that asset type.

To set the default clone/reference behavior for asset types that do not appear in the list of assets, enable/disable the Clone option for All Other Types

To remove an asset type from the list, click Remove.

To add an asset type to the list, either:

        In the Add Type field, enter a specific asset type, or Click the Browse button to open a search window where you can find and choose a specific asset tpe.

        Click Add to add this asset type to the list.

To go back to Unity’s default type list and settings, click Reset Defaults.

GameObjects

GameObjects are the most important part of the Unity Editor. GameObjects are simply characters and collectible items, even your lightsource, cameras, and special effects that go into your game experience. GameObjects need properties in order to perform tasks or actions or before they can become characters, the environment, or a special effect.

GameObjects are the building blocks in Unity that represent characters, props, and environmental features. They act more as containers for Components than anything else, which implement the functionality.

In order for GameObjects to obtain the properties necessary to become light, props, or a camera, etc., you need to add components to those GameObjects. The kind of object you want to add to your game will depend on the combination of components within that GameObject.

You can use the Unity Scripting API for creating your own components to attach to these GameObjects, such as Box Collider to represent an object’s volume and physicality when colliding with other objects. There are also Mesh Filters and Mesh Renderers for drawing the surface of the cube.

There are pre-constructed objects that you can access by clicking on the GameObject > 3D Object menu (these include primitive shapes, cameras, etc).

There is a Transform component attached to GameObjects that allows you to change the position and orientation of the object, and you cannot remove this.

For more in-depth information about Gameobject use as well as its components, visit https://docs.unity3d.com/Manual/GameObjects.html 

Prefabs

Unity’s Prefab system allows for the creation, configuration, and storage of GameObjects with all of their components, property values, and child GameObjects as reusable Assets.

Prefab Assets are templates from which you can create new Prefab instances in the Scene.

Prefabs allow you to automatically keep all the copies in sync should you use a GameObject more than once in a certain way, such as if it’s a prop, part of the scenery, or a non-player character (NPC).

Edits to Prefab Assets are automatically reflected in the instances of the Prefab, which lets you seamlessly make broad changes throughout the whole Project without manually editing every Asset copy.

Nesting Prefabs inside other Prefabs allows for more intricate hierarchies of objects that you can edit at multiple levels.

You can also override settings for individual Prefab instances for creating whatever kind of differentiation for your Prefabs. You can also use variants of Prefabs that allow you to group a set of overrides together into a variation of a Prefab.

Prefabs are great for instantiating GameObjects at runtime that did not exist before in the Scene at the beginning. This can include powerups, special effects, projectiles, or NPCs.

Commonly used Prefabs:

        Environmental assets, NPCs, projectiles, and of course the player’s main character.

For more information on Prefabs, go to https://docs.unity3d.com/Manual/Prefabs.html 

Layers

Layers in Unity determine which GameObjects can interact with different features as well as with one another. These are mostly used by Cameras to render a part of a scene, and by Lights to illuminate parts of a scene. However, layers can also be used by raycasting to selectively ignore colliders or create collisions.

We can assign new layers to a GameObject. To create a new layer, go to the Tags and Layers window (Edit > Project Settings > Tags and Layers).

After creating a new layer, you can assign that layer to at least one GameObject. GameObjects can only be assigned one layer, but you can apply a layer to several different GameObjects. You can do this in the Tags and Layers window.

To learn more about performances and actions within Layers, go to https://docs.unity3d.com/Manual/Layers.html

Constraints

Constraint components link position, rotation, or GameObject scale to another GameObject. Constrained GameObjects move, rotate, or scale the GameObject it is linked to.

These are the following types of Constraint components:

        Aim: rotate the constrained GameObject to face the linked GameObject

        Look At: Rotate the constrained GameObject to the linked GameObject (simplified Aim Constraint)

        Parent: Move and rotate the constrained GameObject with the linked GameObject

        Position: Move the constrained GameObject like the linked GameObject

        Rotation: Rotate the constrained Gameobject like the Linked GameObject

        Scale: Scale the constrained GameObject like the linked GameObject

Linking to GameObjects

You can use the Sources list in a Constraint component to specify the GameObjects to link to. This could look like adding a Position Constraint component to the crosshair in order for it to follow your player’s spaceship in a 2D space shooter game. To link this crosshair to the spaceship, go to the Position Constraint component and add the spaceship to the Sources list. As the player moves the spaceship, the crosshair will follow suit.

A Constraint can link to multiple GameObjects.You would then add the GameObjects to illuminate in the Sources list. For example, you can add an Aim Constraint to the Light GameObject. This will shift the light to face the averaged position of its sources.

Unity evaluates source GameObjects in the order that they show up in the Sources list. This order does not affect the Position and Scale Constraints. It will affect, however, the Parent Rotation and Aim Constraints. Reorder the Sources list by dragging and dropping them in the preferred order so that you get your preferred Constraint effects.

You can also constrain a series of GameObjects. For example, if your goal is to get duckling GameObjects to follow a mother duck GameObject, you would:

  1. add a Position Constraint component to Duckling1.
  2. You would then go to your Sources list, and link this ducking GameObject to MotherDuck.
  3. After, you would add a Position Constraint to Ducking2 that links to Duckling1. As MotherDuck moves throughout the game, Ducklings 1 and 2 will follow.

It is best not to create a cycle of Constraints, however, This will cause unpredictable updates during gameplay.

Setting Constraint Properties

Use the Inspector window to change Constraint properties.

You can use Weight to control the influence of the Constraint. For instance, a weight of 1 will cause the Constraint to update a GameObject at the same rate as its source GameObjects. A weight of 0 removes the effect of the Constraint altogether. Each source GameObject also has an individual weight.

In Constraint Settings, you can use the At Rest properties to specify the X, Y, and Z values to use when Weight is 0 or when the property in Freeze Axes is unchecked.

You can use the Offset properties in Constraint Settings to specify the X, Y, and Z values to use when constraining the GameObject.

You can use the Freeze Axes settings to manipulate which axes the Constraint can actually modify.

Activating and Locking Constraints

Activating a Constraint will allow it to evaluate the position, rotation, or scale of the constrained GameObject.

Locking a Constraint will allow it to move, rotate, or scale the Gameobject. A locked Constraint controls the needed parts of the Transform of the GameObject. You cannot manually move, rotate, or scale a GameObject with a locked Constraint. You cannot edit the Constraint Settings while it's locked.

To manually edit the position, rotation, or scale of a GameObject, unlock the Constraint. If the Constraint is active while unlocked, the Constraint updates Constraint Settings for you as you move, rotate, or scale the constrained GameObject or its source GameObjects.

When adding a Constraint component to a GameObject, the Constraint is inactive and unlocked by default. This lets you tweak the position, rotation, and scale of the constrained and source GameObjects before you activate and lock the Constraint.

The Activate and Zero buttons update Constraint Settings.

        Activate saves the current offset from the source GameObjects, then activates and locks the constrained GameObject.

        Zero resets the position, rotation, or scale to match the source GameObjects, then activates and locks the constrained GameObject.

Animating and combining Constraints

You can use animation clips to modify the source GameObjects that your constrained GameObject links to. As the animation modifies the source GameObjects, the Constraint modifies your constrained GameObject.

You can also animate properties in a Constraint component. This could look like using a Parent Constraint to move a character’s sword from their hand to their back. Firstly, add a Parent Constraint to the sword GameObject.  In the Sources list, link the Constraint to the character’s hand and the character’s spine.

        To animate the sword, add keyframes for the weight of each source. To animate the sword moving from back to hand, add keyframes to change the weight of the hand from 0 to 1 with keyframes for the weight of the spine from 1 to 0.

You can add multiple Constraint components to the same GameObject. When updating the GameObject, Unity evaluates Constraint components from first to last as they appear in the Inspector window. A GameObject can only contain one similar Constraint component. For example, you cannot add multiple Position Constraints.

Importing Constraints

Go to the Animation tab of the Import Settings window and check your Import Constraints. When you import FBX files into the Unity Editor from 3D modeling softwares (ex: Autodesk, Maya, MotionBuilder), you can include Constraints.

To accompany each incoming FBX file, Unity attaches a Constraint component and links it to the correct GameObjects.

Adding and Editing Constraints

To add a Constraint component:

  1. Click your preferred GameObject you want to constrain
  2. In the Inspector window, go to Add Component and each for the type of Constraint you wish to add, then add it.
  3. To add a source GameObject to your new Constraint, drag the GameObject from the Hierarchy (or Scene view) into the Sources list
  4. Move, rotate, or scale the constrained GameObject and its sources GameObjects
  5. To activate the Constraint, choose Activate or Zero, or click Is Active > Lock

To edit a Constraint component:

  1. Choose the constrained GameObject in the Editor
  2. To manipulate the At Rest or Offset fields, go to  the Inspector window > Constraint Settings, uncheck Lock, then edit the values.
  3. To specify the axes that the Constraint updates, expand the Constraint Settings, then check the properties in the Freeze Axes.
  4. To add a source GameObject to the Constraint:
  1. If no empty slots in the Sources list, click the plus sign (+) at the bottom of the list
  2. Drag the GameObject you want to use as Constraint source from your scene into the Sources list
  1. To remove a source GameObject, go to your Sources list and click the minus button (-) at the bottom of the list
  2. To change the order of the source GameObjects in the Sources list, click the double bar icon on the left hand side of each GameObject you want to move, and drag it up or down. Reordering has no effect on the Position, Rotation, and Scale Constraints. It will affect how Parent and Aim Constraints move or rotate a GameObject.
  3. Check Is Active > Lock

For more information on Constraints, go to https://docs.unity3d.com/Manual/Constraints.html 

Implications for Animation

You can use Unity’s Animation window and Euler angles to specify rotations during an animation.

Unity’s Animation Window: Unity uses either angles within the Animation window used for different instances.

External Animation Sources: When importing from external sources, these files normally contain rotational keyframe animation in Euler format. Normally Unity resamples these animations and generates a new Quaternion keyframe for every frame in the animation so as to avoid any situations where the rotation between keyframes exceeds a Quaternion’s valid range.

For more information on orientation within Unity, go to https://docs.unity3d.com/Manual/QuaternionAndEulerRotationsInUnity.html

Lights

Lights are vital to creating scenes within your GamePlay. Light determines the color and mood of your environment. There might very well be more than one light within a scene you work with.

 

To add light you can go to GameObject > Light. You then will choose the light format that you wish for your scene from the sub-menu that appears. You can manipulate the light just like a GameObject once it’s part of your scene. You can also add a Light Component to any selected GameObject by clicking Component > Rendering > Light.

There are several options to choose from within the Light Component in the Inspector window.

Rendering Paths

These paths affect mostly Lights and Shadows, so the rendering path you choose for your specific game requirements can affect the project’s performance.

For more information about Lights and Rendering Paths, go to https://docs.unity3d.com/Manual/Lights.html

Cameras

There will always be at least one camera per scene within your GamePlay, but you can always add more. These are meant to display the 3D world to the viewer. Numerous cameras can offer you a 2-player splitscreen or create advanced custom effects. You can animate cameras or control their movements with physics.

For more information on cameras, go to https://docs.unity3d.com/Manual/Cameras.html

Publishing Builds

The Build Settings window has every setting and option needed to publish your build to numerous platforms. From the Build Settings window you can create a Development Build to test your game, as well as publishing a final build.

If you want to adjust the publishing settings for your game’s build, go to File > Build Settings.

You can use the Scenes in the Build panel to control which Scenes Unity includes in the build. Use the Platform section of the window to choose which platform you want to build to, and adjust the settings like the Compression Method. The options offered will depend on the Platform you choose.

Choose the Build and Build And Run button to start building.

You can pick the name and save the location for your game by clicking the Save dialog that appears. Unity might just ask you to choose a folder depending on the platform you build to.

When you Save, Unity will build your game. You can’t save the game into the Assets folder, but you can make a subfolder inside your root folder to contain all of the builds.

To learn more about Builds or GamePlay within Unity, go to https://docs.unity3d.com/Manual/PublishingBuilds.html

Other Information

The Unity online manual as well as Linkedin learning provides more in-depth information about creating within Unity and learning its interface.

To learn about the Unity Interface, go to https://docs.unity3d.com/Manual/UsingTheEditor.html 

To learn about creating gameplay within Unity, go to https://docs.unity3d.com/Manual/CreatingGameplay.html 

For step by step instructions on learning Unity, go to https://www.linkedin.com/learning/topics/unity 

Or https://learn.unity.com/ and sign in with your Unity login

More Help is Available!

Looking to get some one-on-one help learning how to develop using Unity and other game engines? Request a technology consultation!

Library staff are available to help: https://www.lib.ncsu.edu/tech-consultation