Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
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 a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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.

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 : 9781784397173
Vendor :
Unity Technologies
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Feb 10, 2017
Length: 478 pages
Edition : 1st
Language : English
ISBN-13 : 9781784397173
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
Unity 5.x 2D Game Development Blueprints
€32.99
Getting Started with Unity 5.x 2D Game Development
€36.99
Mastering Unity 5.x
€41.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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.