Module 1 · In-class exercise
Verify you can work with Unity
Overview
Four short steps, about 55 minutes in total. You'll set up a camera, place and transform a few sprites, practice navigating the editor, and write your first behavior script.
Sprites for this exercise
Download these and drag them into your project's Assets folder.
1
Editor camera and the MainCamera
15 minCreate a new project
- In Unity Hub, click New project and choose the 2D (Built-In Render Pipeline) template. It's near the bottom of the template list, so scroll down.
- Create a new 2D scene.
Don't pick Universal 2D
Universal 2D appears near the top of the list, so it's easy to pick by mistake. That template uses the Universal Render Pipeline (URP), and its Main Camera Inspector looks different from the one in the class notes: for example, you won't find Clear Flags or Rendering Path. All the class examples use the Built-In Render Pipeline, so use the same template to make your editor match the notes.
Manipulate the MainCamera
- Set it to look at
(10, 10, -10). Typically, don't change the Z! - Set the camera's Size to
10.
Manipulate the editor camera
- Zoom out your editor camera so you can see the entire boundary of the MainCamera.
Hint: try the following
- Zoom: on a PC, scroll the Middle mouse button.
- Pan: click on empty space in the editor camera, then
- press Q to activate the Hand tool, and
- Left mouse click and drag.
- Focus: select the MainCamera in the Hierarchy window and press F to focus on the selected object.
- Now center your editor camera on the MainCamera so you can see all four of its boundaries.
2
GameObjects
15 minCreate and set GameObjects
Create four new GameObjects: Brain, GreenUp, Plane and Egg.
| Object | Size | Position |
|---|---|---|
| Brain | 3 × 3 | (5, 15, 0) |
| GreenUp | 2 × 2 | (15, 15, 0) |
| Plane | 1.6 × 1.9 | (5, 5, 0) |
| Egg | 1.1 × 1.4 | (15, 5, 0) |
VerifyAll four GameObjects are within the bounds of your camera.
Transform in the editor window
| Do this to the Brain | Tool |
|---|---|
Drag it out to (-10, 15, 0) | W Move |
| Scale it down to 1.5 × 1.5 | R Scale |
| Rotate it by 45 degrees | E Rotate |
Now use a combination of the W E R keys to set the Brain back to 3 × 3, located at (5, 15, 0), with no rotation.
3
Camera manipulation
10 min- Focus your editor camera to center on the Brain.
HintSelect the Brain, then press F to center on the selected object.
- Pan your editor camera to center on the arrow (GreenUp).
HintPress Q to select the Hand tool, then combine Left mouse drag in x/y with the Middle mouse button.
- Set your editor camera so you can see all four objects again.
4
Scripting and object behavior
15 minCreate a script and program simple logic
- Create a script named LeftRightMove to move the Plane.
HintSelect the Plane, then in the Inspector window: Add Component › New Script.
- Edit
LeftRightMove.csto move the GameObject:- Define a variable that's accessible from the Inspector window: Speed, 0.5 units per second. Make sure to declare it as
public. - Move toward the right; if
position.x > 15, setposition.xto-15.
HintSpeed * Time.smoothDeltaTime; - Define a variable that's accessible from the Inspector window: Speed, 0.5 units per second. Make sure to declare it as
Run your game and watch the Plane move
- While the game is running, adjust the Speed variable and watch the Plane move at different speeds.
- Can't see Speed in the editor? Make sure the variable is declared
public.
- Can't see Speed in the editor? Make sure the variable is declared
- Quit the game and see that your changes to Speed are not saved!
Attach an existing script to other objects
- Attach the LeftRightMove script to the Egg and GreenUp.
- Run, and watch the Egg and GreenUp move in sync with the Plane.
- Notice that when you quit, all three objects return to their original positions.
Once againChanges made during gameplay are not saved!
Remove a component from a GameObject
- Remove the LeftRightMove script from the Egg.
- Run, and see that the Egg no longer moves with the Plane and GreenUp.
Solution
For reference, here's what my project looks like at the end of this exercise.
Brain
Plane
Egg