Gaming in Unity

06. Unity 2D Game 2 - World

(../img/unity-6-2d-game-2-world/2d-game-2-world-header.png)

By using Tilemaps, making a level is like painting!

Step 1 - Creating a Tilemap

To create a Tilemap, right-click in the Hierarchy window and select 2D Object -> Tilemap -> Rectangular

(../img/unity-6-2d-game-2-world/2d-game-world-step-1.jpg)

Create a new folder in the Assets window and call it Tiles. Open this folder.

We must now create a Tile Palette.

To create a Tile Palette, right-click in the Assets window and select Create -> 2D -> Tile Palette -> Rectangular

(../img/unity-6-2d-game-2-world/2d-game-world-step-1-2.jpg)

Once you've created your Tile Palette, you need to open the Tile Palette window.

To do this, select Window -> 2D -> Tile Palette.

I usually place this window next to the Hierarchy for convenience.

(../img/unity-6-2d-game-2-world/2d-game-world-step-1-3.jpg)

Step 2 - Adding Tiles

Drag and drop FloorBricksToGrassCorner.png into Assets -> Tiles.

(../img/unity-6-2d-game-2-world/2d-game-world-step-2.jpg)

This image contains 9 different sprites. To use these, click on FloorBricksToGrassCorner.png. Change Sprite Mode to Multiple and change Pixels Per Unit to 256 (tiles are 256 pixels wide). Click the Apply button.

In the Inspector, click the Sprite Editor button.

In the Sprite Editor, click the small arrow to the right of Sprite Editor. Set Type to Grid by Cell Count. Set Column & Row to 3. Click Slice.

After clicking Slice, press Apply.

(../img/unity-6-2d-game-3-world/2d-game-world-step-2-2.jpg)

(../img/unity-6-2d-game-3-world/2d-game-world-step-2-3.jpg)

By doing this, 9 separate sprites are created!

(../img/unity-6-2d-game-3-world/2d-game-world-step-2-4.jpg)

Drag this into your Tile Palette.

Repeat step 2 for any further sprites you want to use in your world!

Step 3 - Painting Your World

Drag your Tile Palette to be next to the hierarchy.

Click on a Tile in the Tile Palette to select it.

Click in the Scene view to paint the world with your selected tile!

(../img/unity-6-2d-game-3-world/2d-game-world-step-3.jpg)

You may notice that SpriteCharacter no longer appears.

To fix this, go to the Hierarchy and select the Tilemap object.

Change the Order in Layer field to -10. This will mean the Tilemap will appear behind SpriteCharacter.

(../img/unity-6-2d-game-3-world/2d-game-world-step-3-2.jpg)

Click Play above Scene and move SpriteCharacter through the world using the arrow keys!

Step 4 - Adding GameObjects

Create a new folder in Assets and call it Environment.

Drag and drop MetalCube.png into Assets -> Environment.

Drag MetalCube into the hierarchy.

Place the MetalCube within your scene.

Click play and move SpriteCharacter around the MetalCube!

(../img/unity-6-2d-game-3-world/2d-game-world-step-4.jpg)

You will notice that moving SpriteCharacter onto the MetalCube will hide the MetalCube.

To fix this, click Edit -> Project Settings.

(../img/unity-6-2d-game-3-world/2d-game-world-step-4-2.jpg)

Click Graphics.

Change Transparency Sort Mode to Custom Axis.

Change the Transparency Sort Axis Y coordinate to 1 and the Z coordinate to 0.

(../img/unity-6-2d-game-3-world/2d-game-world-step-4-3.jpg)

The Pivot of a Sprite is the point at which it rotates around.

We need to change the Pivot of SpriteCharacter and MetalCube.

First, click SpriteCharacter in the Hierarchy.

Change Sprite Sort Point to Pivot.

(../img/unity-6-2d-game-3-world/2d-game-world-step-4-4.jpg)

Click SpriteCharacter in Assets.

Go to the Inspector and change Pivot to Bottom. (You can also set the Pivot to a custom position using the Sprite editor).

Click the Apply button.

(../img/unity-6-2d-game-3-world/2d-game-world-step-4-5.jpg)

Repeat these steps for MetalCube (change sort point to pivot and change pivot to bottom).

SpriteCharacter will now appear behind the MetalCube!

(../img/unity-6-2d-game-3-world/2d-game-world-step-4-6.jpg)

Step 5 - Creating Prefabs

It would be annoying to have to repeat these steps for every MetalCube you add. To avoid repeating work, we can create a Prefab.

Create a new folder in Assets and call it Prefabs.

Drag and drop MetalCube from the Hierarchy to Assets -> Prefabs.

(../img/unity-6-2d-game-3-world/2d-game-world-step-5.jpg)

Now you can drag this MetalCube from Prefabs into the Scene to create a new MetalCube with the same settings! Prefabs help save a lot of time and avoid duplicating your work.

Repeat these steps for any other GameObjects you want to add to your world!

(../img/unity-6-2d-game-3-world/2d-game-world-step-5-2.jpg)

Created by: David Corish