Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Getting Started with Unity 5.x 2D Game Development
Getting Started with Unity 5.x 2D Game Development

Getting Started with Unity 5.x 2D Game Development: Enter the world of 2D Game development with Unity 5.x

eBook
€8.99 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Getting Started with Unity 5.x 2D Game Development

Chapter 2. Baking Cupcake Towers

In this second chapter, we will begin to build our game. We will see how it's possible to place objects in 2D space and create templates for the most used objects. Furthermore, we will see how Unity deals with scripts, and we will write a couple of our own for our game.

In particular, we will cover the following topics:

  • Placing objects in 2D space
  • Setting up the map for our game
  • Using tags and layers
  • Creating Prefabs (templates for game objects)
  • Creating new scripts
  • Basic concepts of scripting in Unity
  • Writing our first two scripts for our game

Like all the other chapters of this book, you will find the Homework section at the end. It has a range of different exercises for you to do to improve your skills and implement a range of different functionalities into your game.

So, let's get started by learning how to place 2D objects into the scene.

2D objects

In the previous chapter, we saw that 2D objects in Unity are Sprites. However, we didn't mention how to import them into the scene.

The easiest way to bring your Sprites into the scene is to drag and drop them from the Project panel into the Scene view. Unity will automatically create a new game object with the same name of the Sprite along with a Sprite Renderer attached. We have already covered this component in the previous chapter. Since we will not use Sorting Layers (as we decided in the previous chapter), we don't need to change any settings when dragging a new Sprite into the scene.

Another way to add Sprites to the scene is by right-clicking on the Hierarchy panel and then 2D Object | Sprite. However, in the Sprite Renderer, you need to specify which Sprite to use.

Let's bring the Pink_Sprinkle Sprite into our scene (you can find it in the Graphics/projectiles folder within the projectiles_sheet_01 file). It appears like this in the Scene view:

2D objects

Since Sprites...

Parenting game objects

Each game object can have a parent. This means that the game objects will move, rotate, and scale along with its parent.

I could use many words to explain this concept, but there are things which videos can explain better than words. As such, there is a very short video explaining, The Hierarchy and Parent-Child relationships at: https://unity3d.com/learn/tutorials/topics/interface-essentials/hierarchy-and-parent-child-relationships.

I recommend that you to watch it before you continue reading this book. I'll wait for you here.

Difference between world coordinates and local coordinates

Every game object in Unity has a position, but a position needs a reference frame (more about reference frames later in Chapter 5The Secret Ingredient Is a Dash of Physics). In particular, Unity offers two ways to see (and set) coordinates:

  • World coordinates: These are absolute coordinates of where the game object is located (by absolute, I mean with respect to the world frame, which is considered to be absolute in the game)
  • Local coordinates: These are the coordinates of where the game object is with respect to its parent

You can easily switch between the two coordinates with a toggle in the upper-right part of the Unity interface, as shown in the following screenshot:

Difference between world coordinates and local coordinates

As shown in the preceding screenshot, they are both toggles, but the one to switch between world and local coordinates is the one on the left. At the moment, it is selected to be on Global, which means on world coordinates.

Ordering the different layers with Z-Buffering

In the previous chapter, we decided to use Z-Buffering instead of Sorting Layers. However, we need to decide which elements of our game will be in the foreground compared to the others.

In addition, it is important to keep in mind how the camera is set. Select Main Camera, which should be the only camera in the scene. This is what the Inspector should look like:

Ordering the different layers with Z-Buffering

As you can see, by default, its Z value on the the position is set to -10. This means you cannot have any greater negative values (that means smaller than -10) in the Z value of a Sprite, otherwise it will not be rendered. For our purpose, -10 is perfect, and we will stick with it.

Next, we need to assign a Z value to all the elements that we have. We can start to place the map in the background, by assigning the lowest depth (maximum Z value) that we have in mind; in this case it's zero.

Then, we would like to have the enemies. Therefore, we can set their Z value to -1. After...

2D objects


In the previous chapter, we saw that 2D objects in Unity are Sprites. However, we didn't mention how to import them into the scene.

The easiest way to bring your Sprites into the scene is to drag and drop them from the Project panel into the Scene view. Unity will automatically create a new game object with the same name of the Sprite along with a Sprite Renderer attached. We have already covered this component in the previous chapter. Since we will not use Sorting Layers (as we decided in the previous chapter), we don't need to change any settings when dragging a new Sprite into the scene.

Another way to add Sprites to the scene is by right-clicking on the Hierarchy panel and then 2D Object | Sprite. However, in the Sprite Renderer, you need to specify which Sprite to use.

Let's bring the Pink_Sprinkle Sprite into our scene (you can find it in the Graphics/projectiles folder within the projectiles_sheet_01 file). It appears like this in the Scene view:

Since Sprites are also GameObjects...

Parenting game objects


Each game object can have a parent. This means that the game objects will move, rotate, and scale along with its parent.

I could use many words to explain this concept, but there are things which videos can explain better than words. As such, there is a very short video explaining, The Hierarchy and Parent-Child relationships at: https://unity3d.com/learn/tutorials/topics/interface-essentials/hierarchy-and-parent-child-relationships.

I recommend that you to watch it before you continue reading this book. I'll wait for you here.

Difference between world coordinates and local coordinates


Every game object in Unity has a position, but a position needs a reference frame (more about reference frames later in Chapter 5The Secret Ingredient Is a Dash of Physics). In particular, Unity offers two ways to see (and set) coordinates:

  • World coordinates: These are absolute coordinates of where the game object is located (by absolute, I mean with respect to the world frame, which is considered to be absolute in the game)

  • Local coordinates: These are the coordinates of where the game object is with respect to its parent

You can easily switch between the two coordinates with a toggle in the upper-right part of the Unity interface, as shown in the following screenshot:

As shown in the preceding screenshot, they are both toggles, but the one to switch between world and local coordinates is the one on the left. At the moment, it is selected to be on Global, which means on world coordinates.

Ordering the different layers with Z-Buffering


In the previous chapter, we decided to use Z-Buffering instead of Sorting Layers. However, we need to decide which elements of our game will be in the foreground compared to the others.

In addition, it is important to keep in mind how the camera is set. Select Main Camera, which should be the only camera in the scene. This is what the Inspector should look like:

As you can see, by default, its Z value on the the position is set to -10. This means you cannot have any greater negative values (that means smaller than -10) in the Z value of a Sprite, otherwise it will not be rendered. For our purpose, -10 is perfect, and we will stick with it.

Next, we need to assign a Z value to all the elements that we have. We can start to place the map in the background, by assigning the lowest depth (maximum Z value) that we have in mind; in this case it's zero.

Then, we would like to have the enemies. Therefore, we can set their Z value to -1. After them, the...

Unfolding the map


We are finally ready to place the 2D map in our scene.

In the previous chapter, we have set the resolution to 16:9. Therefore, the maps that we will find in our packages are ready to use.

Let's start by dragging the sugar_mountain_map Sprite from the Graphics/maps folder into our scene. We need to place it in (0,0,0). Please note that the z axis is set to zero.

It is a perfect map for what we need. For example, on the left side, there is the beginning of the path that the sweet-tooth pandas will follow. At the end of the path, there is the Sugar castle that the player needs to protect. Furthermore, there is enough space at the top to implement our user interface in Chapter 3, Communicating with the Player - the User Interface.

The next step is to modify the camera settings. What we want to do is to fit the entire map in the Camera view. To achieve this, just modify the Size properties to 22.5 as shown here:

As a result, our map will be perfectly centered in the Camera view....

Layers and tags


If you already have in mind what are you going to do, it's good to set everything up at the beginning. In particular, Unity has some labels that can be given to game objects. These are layers and tags. Unity uses these two properties to discriminate amongst certain kinds of game objects.

By default, some of them are already defined, but we need a few more for our project. From the toolbar menu, we can access the layers and tags settings by navigating to Edit | Project Settings | Tags and Layers. As a result, the Inspector should now look like the following screenshot:

Note

In this menu, we also have the possibility to change the sorting layers for rendering 2D objects. However, as mentioned earlier, we will use Z-Buffering to achieve the same effect.

Let's expand the Tags menu, as follows:

To add a new tag, just press on the + button in the bottom-right corner. We need to add two tags, respectively Enemy and Projectile, as shown in the following screenshot:

In fact, we will need...

Prefabs


When the scene begins to fill up with objects, some of these objects might become complex. By complex, I mean with a lot of components and children. If we need to use many of them in the game and maybe change all of them at once, Unity offers the possibility to create a Prefab.

As the name suggests, it is an object already assembled with all the necessary components and ready to be placed in the scene. The advantage is the possibility to reuse it often and to change all its instances quickly.

Note

If an object in the scene is a Prefab, its name in the Hierarchy panel is blue. If the name is red, instead, this means that there are some references missing.

To keep our project organized, let's create a folder named Prefabs, if we haven't done so yet. Inside the folder, right-click and then select Create/Prefab. You can name it as you want, but for this book, let's stick with Pink_Sprinkle_Projectile_Prefab.

We already have the sprinkle in the scene, so from the Hierarchy panel, drag it into...

The game view


You should already know about the game view, but there might be some new types of functionality that you didn't know about before. So, a short recap might be useful, before we move on in our journey.

First of all, we have our three main buttons, which you should be very familiar with, as shown here:

The first is the play button, which makes your game run. The second button pauses the game, and allows you to tweak some settings. The last one makes your game run for one frame only.

In the top-left corner, we have the displays and the resolution tabs that we covered in the previous chapter. In the opposite corner (top-right), there are many different yet useful toggles, as shown in the following screenshot:

These are their functions:

  • Maximize on Play: If this is on, every time you push the play button, the game view will be maximized to the largest window it can be. It's useful for testing the game in almost full screen; otherwise, it can be a little bit hard to tweak values without...

Math background


Either you like math or not; however, it is a fact that it is required for game development. We don't have time to go through all the math behind this game, since it is required to have a complete toolset for game development. However, this section introduces you to some basic concepts that we will need for the rest of the book. In addition, it also makes some references to the official documentation so that you can learn a bit more about it.

If you feel confident with some of the following topics, you are free to skip them:

Scripting in Unity


In this section, we will learn one of the toughest topics in game development! However, I strongly encourage you not to be scared, but try to practice a lot. As a result, you will be able to master every single detail of your game. And that is awesome!

Creating new scripts

First of all, we need to understand how to create new scripts in Unity. The easiest way is to select a game object and in the Inspector, navigate to Add Component | New Script. In this way, you still have the possibility to rename it, but the script will be located in the Asset folder. Furthermore, it's not possible to create a class that cannot be attached to a game object.

A much better way is to create a folder called Scripts in the Project panel, if you haven't done so yet. Then, right-click and navigate to Create | C# Script. As a result, it will be in the right folder, and we don't have problems if we create a script that cannot be attached to game objects.

For the rest of this book, it will be assumed...

Making sprinkles


In the Prefabs section, we created the Prefab for our sprinkle projectile. In this section, we will see how to make sprinkles move into the 2D space. In particular, we will learn how to create and use scripts in Unity for 2D game elements.

The projectile class

Since there might be different projectiles in our game that can be thrown against the sweet-tooth pandas, and not only sprinkles, we need to define a general class. All the different kinds of projectile will follow some general rules:

  • They move in a straight line

  • They carry information about how much damage they will inflict on the enemies

Whereas the first is identical for all the projectiles, the second depends on the specific kind of projectile that will be fired. So, we need to create a template. Once we attach this script to a game object, we can set some of its variables and tweak its behavior. In this specific case, we want to tweak how much damage is given and how fast.

Scripting the projectile mother class

So, to...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Build a complete and exciting 2D Tower Defense game from scratch.
  • Understand and learn to perform each phase of the game development pipeline
  • Homework and exercises to improve your skills and take them to the next level

Description

Want to get started in the world of 2D game development with Unity? This book will take your hand and guide you through this amazing journey to let you know exactly what you need to build the games you want to build, without sacrificing quality. You will build a solid understanding of Unity 5.x, by focusing with the embedded tools to develop 2D games. In learning about these, along with accurate explanations and practical examples, you will design, develop, learn how to market and publish a delectable Tower Defense game about cupcakes versus pandas. Each chapter in this book is structured to give you a full understanding on a specific aspect of the workflow pipeline. Each of these aspects are essential for developing games in Unity. In a step-by-step approach, you will learn about each of the following phases: Game Design, Asset Importing, Scripting, User Interfaces, Animations, Physics, Artificial Intelligence, Gameplay Programming, Polishing and Improving, Marketing, Publishing and much more. This book provides you with exercises and homework at the end of each chapter so that you can level up your skills as a Unity game developer. In addition, each of these parts are centered on a common point of discussion with other learners just like you. Therefore, by sharing your ideas with other people you will not only develop your skills but you will also build a network.

Who is this book for?

If you are looking forward to get started with 2D game development, either if you are a newcomer to this world, or you came from 3D games or other game engines, this book is for you. Although there are many references to other resources throughout the book, it is assumed that you have a general understanding of C# and its syntax and structure.

What you will learn

  • Import and set up assets for 2D game development
  • Design and implement dynamic and responsive User Interfaces
  • Create and handle complex animation systems
  • Unlock all the potentiality of the physics engine
  • Implement Artificial Intelligence algorithms to give intelligence to your NPCs
  • Script gameplay and overall bring your ideas to life

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 10, 2017
Length: 478 pages
Edition : 1st
Language : English
ISBN-13 : 9781784393298
Vendor :
Unity Technologies
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Feb 10, 2017
Length: 478 pages
Edition : 1st
Language : English
ISBN-13 : 9781784393298
Vendor :
Unity Technologies
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 111.97
Mastering Unity 5.x
€41.99
Getting Started with Unity 5.x 2D Game Development
€36.99
Unity 5.x 2D Game Development Blueprints
€32.99
Total 111.97 Stars icon
Banner background image

Table of Contents

8 Chapters
1. A Flat World in Unity Chevron down icon Chevron up icon
2. Baking Cupcake Towers Chevron down icon Chevron up icon
3. Communicating with the Player – the User Interface Chevron down icon Chevron up icon
4. No Longer Alone – Sweet-Toothed Pandas Strike Chevron down icon Chevron up icon
5. The Secret Ingredient Is a Dash of Physics Chevron down icon Chevron up icon
6. Through a Sea of Sprinkles – Navigation in Artificial Intelligence Chevron down icon Chevron up icon
7. Trading Cupcakes and the Ultimate Battle for the Cake – Gameplay Programming Chevron down icon Chevron up icon
8. What Is beyond the Cake? Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(3 Ratings)
5 star 66.7%
4 star 0%
3 star 0%
2 star 33.3%
1 star 0%
Bart Knowles Feb 27, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
In the vast sea of technical manuals, each of us is looking for something specific. I found exactly what I wanted in this book: a clear, step-by-step guide to writing a real game with Unity. I was not looking for abstract theory or academic blah blah, therefore if you are looking for those, you shouldn't probably buy this one.But if you are, as I am, into serious game programming and want to learn without useless fatigue how to develop your own game, together with priceless insider tips along the way, then you've come to the right place. I haven't read it all yet (a bit more than halfway through), but I am programming as I proceed, and the tower defense game that is coming out is pretty cool. It won't be my final product, of course, but I am planning on experimenting with variations until I get to my perfect tower defense game!
Amazon Verified review Amazon
Stelvio Feb 25, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I'm getting started! And this book's helping me so much. A few days ago I even proposed it to my teacher, I'm quite sure he's going to adopt it for his course.
Amazon Verified review Amazon
TechReader Feb 21, 2017
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
I've read enough technical books over the years to know that reading a really great one is effortless, and reading a not so great one can be the most frustrating thing. This one's not so great. The main problem this book suffers from is that the author tells you what he wants you to do, but not EXACTLY what he wants you to do. Quick example, in an early chapter the author explains all about the Sprite Editor by stepping through an example with an actual sprite. Then he tells you to do the rest of the sprites in the example game the same way before moving on. When you try do it the same way, you wind up with sprites getting cut right through the middle of the graphics and you're left wondering if this is intentional or not? Then it takes way too much time and effort to decide on what to do next, and once you do decide, you don't know if you've done it right or not. This happens over and over again throughout the book, and it takes quite an effort to figure out what's going on. If you like a challenge and a struggle while learning, then this book might be for you. If you learn better from clear instructions and want to breeze through each topic like I do, then look for another Unity book.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.