Session 1

Introduction to the UnityGame Engine

The Editor

The most important tools on the toolbar:

The Play Button runs your current game scene. Pause and Frameskip are useful for debugging or watching interactions in slow motion later.

The Hand Tool (Q) is used to adjust your current view in the Editor.

The Move Tool (W) lets you change the position of the selected GameObject.

The Rotate Tool (E) controls the direction and rotation of the GameObject.

The Scale Tool (R) adjusts the size of the GameObject.

The Unity Editor interface is made up of a set of different windows. You can move these windows around if you prefer them in different places. You can save your custom layouts, and try other ones in the Layout menu at the upper right corner of the Editor.

The Scene Window is where you will spend most of your time. This is the window where you can place and move GameObjects in your game. The Game Window shows you what your game looks like when it’s playing.

The Hierarchy Window lists all GameObjects in the current Scene. The Project Window displays all files saved for your game, including Scenes, Scripts, graphics, audio.

The Console Window will show you errors in code, and can be used to display messages from code to help with the debugging process.

Let’s make a shooter game

When we first open Unity, we’ll get a new Project Window. If you are asked to sign into Unity, you can skip that and go to the project screen. Type “Your Name-Shooter” into the Project name, substituting your last name for Your Name.

If you have a USB flash drive or portable hard drive, plug it into the computer. Click “…” next to the Location bar and select your USB flash drive or portable hard drive and create a new folder for this class, called “Yourname Game Design Projects”. If you do not have a flash drive, change the location the to desktop and create the folder. I will save your projects temporarily until you bring a flash drive to class.

Click on “2D” to change the default mode to two-dimensional, and click Create Project.

Setting the scene

The default scene will have two objects: a camera, and a directional light. Click on the Camera and look at the Inspector Window.

(If you forgot to click “2D” when creating your project, you can fix that now by changing the camera’s Projection from “Perspective” to “Orthographic” and clicking “2D” at the top of the Scene Window.)

Right now our background is a blue sky and a field, but we might want our background to look more space-like instead. To do that, click on the drop-down next to Clear Flags and change it to “Solid Color”. Click on the color box, and use the color picker to pick a dark blue, purple, or black.

The ship also takes a lot of space on the screen. We can zoom out with the camera by changing the Size parameter underneath Projection to a larger number, such as 8 or 9.

Making a ship

Let’s create a simple object to be our ship. Click on the GameObject menu, highlight 3D Object and click on Capsule.

Select the Move Tool (Shortcut: W key), and use the handles to move the ship to the lower middle part of the screen.

We need to be able to control the ship, and to do that, we’re going to attach a script. With the Ship selected, click on the Add Component button in the Inspector. Scroll down and click on “New Script”. In the name, type “Ship”, and make sure C# is the chosen language.

This will create a new empty script set up for Unity, and attach it to the ship object.  You’ll see the script in the Heirachy Window. Double-click on the script, and the script editor MonoDevelop will open.

By default, a script for Unity will have two special commands or Functions inside of it: Start() and an Update(). We’re going to focus on what goes inside of Update() { } right now.

For this project, we will use a simple Translate command when we push the left and right keys to move the ship. Type the highlighted code into the script inside the Update function seen below. I’ll explain more about how these work as we go:

void Update () {

       if (Input.GetAxisRaw("Horizontal") != 0) {

           float hMove = Input.GetAxisRaw("Horizontal");

           transform.Translate(hMove * 10 * Time.deltaTime, 0, 0);

       }

}

Save the Ship.cs script in MonoDevelop, and return to Unity and press Play. You should be able to move your ship left and right now! When you’re ready to move on, press Play again to stop the running game.

Getting our ship to fire

Just like we made the ship, we’re going to create a sphere . Go to the GameObject menu, highlight 3D Object and click on Sphere.  So we can find it easily in the Heirarchy, click on the name at the top of the Inspector, and type “Bullet”, and press Enter to change the name.

This sphere is a bit large, so we’ll want to reduce the size. Select the Scale tool by pressing the button in the toolbar or the R key, and click on the white cube in the center and drag down to reduce the size until it’s about half. You can also change the size by going to the Transform component in the Inspector Window, and changing the Scale of each axis to “0.5”.

On the Sphere Collider component, click the “Is Trigger” checkmark to make sure it is on. This will be useful later.

We need to make a script to tell the bullet to move. Click on Add Component, select New Script and name it “Bullet”. This is a vertical shooter, so the bullet only needs to go in one direction: up.  Double-click the Bullet.cs script in the Project window to open it in MonoDevelop. Inside script we’re going to add the following code:

void Update () {

           transform.Translate(0, 15 * Time.deltaTime, 0);

}

Save the Bullet.cs file and return to Unity.

We want to make a lot of copies of this bullet, so we will make the bullet a Prefab. Click on the Bullet object in the Hierarchy Window, and drag it into the Project Window to create a Prefab version. You can now delete the bullet gameobject in the scene.  You can make adjustments to the Prefab in Inspector right from the Project Window in the future.

Now that we have our bullet set up, we can go back to our Ship.cs script in on our ship to add a way for the ship to shoot bullets.

We need to add the following two highlighted snippets to the Ship script:

public GameObject bulletPrefab;

void Update () {

       if (Input.GetAxisRaw("Horizontal") != 0) {

           float hMove = Input.GetAxisRaw("Horizontal");

           transform.Translate(hMove * 10 * Time.deltaTime, 0, 0);

       }

       if (Input.GetButtonDown("Jump")) {

           Instantiate(bulletPrefab, transform.position, Quaternion.identity);

       }

}

Save the new additions to the file and return to Unity. Adding the “public GameObject bulletPrefab” line will now show a “Bullet Prefab:” box in Ship script component. Click and drag the bullet Prefab that we made to the “Bullet Prefab: [GameObject]” slot. Press Play, and when you press the Space key, you will fire bullets straight up from your ship!