Back

Activity 1.2: A World Built From Objects

Lesson · Junkyard

Objectives

  • Describe a game world as objects, each built from parts that do one job.
  • Bring pixel art into Unity as sprites at the right size, and keep it sharp.
  • Place objects with a Transform's position, using 2D coordinates.
  • Fill a long floor by tiling one small picture instead of stretching it.
  • Decide which picture is drawn in front of which.
divider

Activity 1.2

A World Built From Objects

An Empty Scene Is Not a Game

Activity 1.1 ended with a Unity project and a scene with nothing in it. There is nothing to look at, so there is nothing to play.

Every Game Is a Pile of Objects

Mario, every brick, every coin, every pipe and the sky behind them: each one is a separate object the game keeps track of.

Every Game Is a Pile of Objects

The Junkyard scene: a scrapyard at sunset with a crane, a gravel floor, and three wooden crates stacked in a pyramid

Five objects: the yard, a floor, three crates.

An Object Does Nothing Until You Give It Parts

In Unity an object is a GameObject. Each part you add to it is a component, and each component has one job.

An Object Does Nothing Until You Give It Parts

The Inspector for Background, showing two components: Transform and Sprite Renderer

Transform: where it is and how big. Sprite Renderer: what it looks like. Nothing here makes it fall.

Pixel Art Needs Sharp Edges

Unity smooths every image it imports. On a photo you never notice. On pixel art, where every pixel was placed on purpose, it smears the edges.

Pixel Art Needs Sharp Edges

The same three crates, close up, twice: on the left blurred by Unity's default smoothing, on the right with sharp, square pixels

Left: Unity's default. Right: Point (no filter). One setting.

A Position Is Two Numbers

A crate is placed at (0, 0). Where on the screen does it show up?

A Position Is Two Numbers

The crate at position 0, 0, sitting in the exact center of the Game view

Dead center. (0, 0) is the middle of the world, not a corner.

Up Is Positive

Unity's 2D coordinates: X grows to the right, Y grows upward, origin in the center; the camera sees Y from -5 to 5; a crate sits at 4, -3.5

A bigger Y moves things up. Type -3.5 and the crate drops below the middle.

Tiling Repeats the Picture

The floor art is one strip of dirt, 64 pixels wide. Stretch it to fill the screen and every pixel turns into a smeared block. Tile it, and Unity lays copies side by side, 25 units across.

Tiling Repeats the Picture

The dirt strip repeated into a long gravel floor across the bottom of the Game view

The Yard Goes Behind Everything

When two pictures overlap, one has to be drawn on top. Forget to choose, and the junkyard picture can cover your crates.

Order in Layer: lower numbers are drawn first, further back. The yard gets -10.

Press Play. Nothing Moves.

The crates hang in the air exactly where you typed them. They are pictures with positions, and nothing yet gives them weight.

That part comes in Activity 1.3.

'F' → Fullscreen

divider

Build

Today Junkyard gets its yard, a floor and three crates. Everything is done in the editor, with no code.

Starting point. Open your Junkyard project from Activity 1.1. If it is missing or broken, download the junkyard/1-1 copy, as How This Course Uses Unity explains.


Task 1: Bring the art into Unity

A sprite is a 2D picture a game can place and move, like Mario or a coin. Unity measures its world in units, not pixels, so every image needs a setting that says how many pixels make one unit. The crate is 26 pixels wide, so 26 pixels per unit makes one crate exactly one unit: easy numbers for everything that follows.

Junkyard's art is pixel art: small pictures where every pixel was placed on purpose. Unity smooths images as it imports them, which blurs pixel art, so two more settings keep it sharp. Filter Mode set to Point draws each pixel as a crisp square, and Compression set to None keeps every color exactly as it was drawn.

The same three crates, close up, twice: on the left blurred by Unity's default settings, on the right with sharp, square pixels
  1. Download the Junkyard Starter Pack and unzip it.
  2. In the Project window, right-click the Assets folder and choose Create → Folder. Name it Sprites.
  3. Open the Sprites folder. Drag the six .png files from the starter pack into it.
  4. Click the first image, then Shift-click the last one, so all six are selected.
  5. In the Inspector, set Sprite Mode to Single and Pixels Per Unit to 26.
  6. Set Filter Mode to Point (no filter).
  7. Lower down, on the Default tab, set Compression to None.
  8. Click Apply, below the Default tab.
The Project window with Assets, Sprites open, showing six images: background, ball, barrel-explosive, crate, crate-heavy and ground

Check it: click crate on its own. The Inspector should read Single, 26, Point (no filter) and None.

Import settings for crate: Sprite Mode Single and Pixels Per Unit 26, Filter Mode Point (no filter), and Compression None, each outlined in red

Task 2: Lay out the yard

Dragging a sprite into the Hierarchy (the list of everything in your scene) makes a new GameObject with two components already on it. The Transform holds where the object is, how big, and how it is turned. The Sprite Renderer draws the picture. Click any GameObject and the Inspector lists its components.

  1. Drag background from the Sprites folder into the Hierarchy.
  2. It is named background. Right-click it, choose Rename, and call it Background.
  3. In the Inspector, under Transform, set Position to X 0, Y 0.6.
  4. Under Sprite Renderer, open Additional Settings and set Order in Layer to -10.
The Inspector for Background with its name, Position 0, 0.6, 0, and Order in Layer -10 outlined in red

Order in Layer decides which picture is drawn on top when two overlap. Lower numbers are drawn first, so they end up behind. The yard goes far back at -10 so nothing you add later ends up behind it.

Click the Game tab above the scene. It shows what the player's camera sees. You should see the junkyard at sunset filling the whole view:

The Game view filled with a scrapyard at sunset: mountains, a crane, a scrap heap, a fence and an old car

Task 3: Tile the floor

The ground image is a strip of gravel and dirt only 64 pixels wide, about 2.5 units. A Sprite Renderer's Draw Mode decides how it fills the space you give it. Tiled repeats the picture side by side instead of stretching it, so a 25-unit floor is ten copies of one small strip, each pixel still square.

  1. Drag ground into the Hierarchy and rename it Ground.
  2. In the Inspector, under Sprite Renderer, set Draw Mode to Tiled. A yellow warning appears under it.
Draw Mode set to Tiled, with a yellow warning under it: Sprite Tiling might not appear correctly because the Sprite used is not generated with Full Rect

Read a warning before you click past it; this one says exactly what to fix. Tiling needs the image's Mesh Type set to Full Rect, a plain rectangle Unity can repeat.

  1. In the Project window, click the ground image.
  2. In the Inspector, set Mesh Type to Full Rect and click Apply.
Import settings for ground with Mesh Type set to Full Rect, outlined in red
  1. Click Ground in the Hierarchy again. The warning is gone.
  2. Under Size, set Width 25 and Height 2.
  3. Set its Position to X 0, Y -5.
The Inspector for Ground with its name, Position 0, -5, 0, and Draw Mode Tiled with Size 25 by 2 outlined in red

In the Game view, a gravel floor runs along the bottom edge. The image is 52 pixels tall, so at 26 pixels per unit the floor is exactly 2 units thick:

The Game view with the junkyard and a long gravel floor across the bottom

Task 4: Find the middle of the world

Every position is two numbers, X and Y. (0, 0), called the origin, is the center of the world. X grows to the right. Y grows upward, the same as on a math graph or in Scratch, but not like most image editors, where Y grows down.

  1. Drag crate into the Hierarchy and rename it Crate.
  2. Check that its Position reads X 0, Y 0. If not, type the zeros in.
  3. Look at the Game view, then click the Scene tab to go back.

The crate sits in the exact center of the Game view:

The crate at 0, 0, in the center of the Game view, floating in front of the crane

In the Scene view, the selected crate shows two arrows. The red arrow points along +X and the green arrow along +Y. (An icon sits on top of the crate: the scene's light is at (0, 0) too.)

The Scene view with the crate selected at the origin: a red arrow points right along X and a green arrow points up along Y

The camera shows 5 units above the origin and 5 below. A position is the object's center. The floor is 2 units thick with its center at -5, so its top edge is at -4, and the crate is 1 unit tall, so its center has to be at -3.5 for it to sit on the floor:

Unity's 2D coordinates: X grows to the right, Y grows upward, origin in the center; the camera sees Y from -5 to 5; a crate sits at 4, -3.5 on the floor
  1. Set the Crate's Position to X 4, Y -3.5.

The crate now sits on the floor, right of center:

The crate resting on the gravel floor to the right of center

Task 5: Stack three crates

Ctrl+D duplicates the selected GameObject, components and all. Unity names the copy after the original with a number, like Crate (1).

  1. Select Crate in the Hierarchy and press Ctrl+D. Set the copy's Position to X 5, Y -3.5.
  2. Press Ctrl+D again. Set this copy's Position to X 4.5, Y -2.5.
  3. Save the scene with Ctrl+S.

Your Hierarchy lists six objects plus the light:

The Hierarchy for the Junkyard scene: Main Camera, Global Light 2D, Background, Ground, Crate, Crate (1), Crate (2)

Press Play (the triangle at the top of the editor). Three crates sit in a small pyramid on the floor, and nothing moves. That is correct: nothing has weight yet. Press Play again to stop.

The finished scene: three crates stacked in a pyramid on the right side of the gravel floor, with the junkyard behind

Task 6: Push the numbers to the edge

Record both answers on your worksheet, then put everything back the way it was.

  1. Find the edge. Select Crate (2). Raise its Y by 0.5 at a time, watching the Game view, until no part of the crate is visible. Record the Y where it disappears completely. Then set it back to -2.5.
  2. Break it on purpose. Set the Ground's Order in Layer to -20. Describe what happens in the Game view and explain why. Then set it back to 0.
divider

Wrap Up

Practice (optional, not graded)

 

Loading quiz…

Reflection

  1. A crate in your scene is a GameObject with two components. Pick one object from a game you play and list the parts it would need, and what job each part does.
  2. You placed every object by typing numbers instead of dragging it into place. Give one reason a designer would want an exact number for where something sits.
  3. The crates do not fall when you press Play. Looking at a crate in the Inspector, what do you think is missing?

Submit

Submit one screenshot showing your Hierarchy and Game view with the three crates, and your worksheet.

divider

Key Terms

Design terms

object
One thing a game keeps track of, such as a player, a coin or a wall.
coordinates
The pair of numbers, X and Y, that says where something is.
origin
The point (0, 0), where X and Y are both zero.
draw order
Which picture is drawn first when two overlap; the one drawn last appears in front.
pixel art
Small pictures where every pixel is placed on purpose, so they need to stay sharp.
tiling
Filling a space by repeating one picture side by side instead of stretching it.

Unity terms

GameObject
Unity's name for an object in a scene; on its own it is an empty container with a name.
component
A part added to a GameObject that gives it one job, like drawing a picture.
Transform
The component every GameObject has, holding its position, rotation and scale.
Sprite Renderer
The component that draws a sprite on screen.
sprite
A 2D picture the game can place and move.
unit
Unity's measure of distance; in Junkyard, one crate is one unit.
Pixels Per Unit
An image setting saying how many of its pixels make one unit.
Filter Mode
An image setting; Point (no filter) keeps pixel art sharp instead of smoothing it.
Draw Mode
A Sprite Renderer setting; Tiled repeats the sprite to fill the Size you set.
Order in Layer
A Sprite Renderer setting; lower numbers are drawn further back.
Hierarchy
The window listing every GameObject in the open scene.
Inspector
The window showing the components of whatever is selected.
Scene view and Game view
The Scene view is where you arrange the world; the Game view shows what the player's camera sees.