Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Mastering Unity 2D game development
Mastering Unity 2D game development

Mastering Unity 2D game development: Mastering Unity 2D Game Development will give your game development skills a boost and help you begin creating and building an RPG with Unity 2D game framework

eBook
$24.99 $36.99
Paperback
$60.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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
Table of content icon View table of contents Preview book icon Preview Book

Mastering Unity 2D game development

Chapter 2. Building a Character

It's time to start putting in the building blocks that will make up your game, starting with the setting up the project and then on to main character. It's an important first step as most of your game's core logic and framework generally centers on the main protagonist and highlights exactly how the player will interact with the game.

Here, we are aiming to create a 2.5D style in-game world where the player can explore each town and have random encounter scenes to fight off those pesky bad guys. So, first we need to get our character in and moving.

The following is the list of topics that will be covered in this chapter:

  • Designing a good class structure
  • Planning and designing behaviors
  • Importing sprites
  • Setting up user control effectively

Note

For this game, we will use the excellent, free Unity games starter kit resources, which can be found at http://wootstudio.ca/win8platstarter (towards the bottom of the page).

There are several different...

Getting your project started – the right way

Before you start your project for real, you should consider how you intend to set it up and architect your project in the long term. I've looked at or worked with far too many projects that have created problems for themselves by just diving in rather than designing the outline for the project at the start.

Your game and your assets are not the only things to consider when starting a fresh project. Sure, you can start importing assets, creating scripts, and getting things running; most Proof of Concept (POC) projects start this way. Once your project is of a sufficient size and you start expanding on your initial concept, you'll realize that you have issues with regards to picking up items and putting them together. Then, you will start devising new ways to organize your project and eventually find that it's an unmanageable mess; nevertheless, you will stride on, taking longer and longer to produce new content or add new...

Understanding components

Components in Unity are the building blocks of any game; almost everything you will use or apply will end up as a component on a GameObject inspector in a scene.

Until you build your project, Unity doesn't know which components will be in the final game when your code actually runs (there is some magic applied in the editor). So, these components are not actually attached to your GameObject inspector but rather linked to them.

Accessing components using a shortcut

Now, in the previous Unity example, we added some behind-the-scenes trickery to enable you to reference a component without first discovering it. We did this by adding shortcuts to the MonoBehavior class that the game object inherits from. You can access the components with the help of the following code:

this.renderer.collider.attachedRigidbody.angularDrag = 0.2f;

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www...

Creating the project

First things first! To ensure that you have all the bases covered, you need to start a new project. For 2D, however, you need to ensure that you start the project using the 2D game template in the Unity New Project wizard, as shown in the following screenshot:

Creating the project

If you accidentally leave the 3D option set (which is the default) as is, don't worry! You can change this at any time for your project through Editor Settings. To open Editor Settings, simply navigate to Edit | Project Settings | Editor, as shown in the following screenshot:

Creating the project

Lastly, let's create the folder structure for the project, as shown in the following screenshot, using the guidance provided earlier:

Creating the project

At this point, it's important to note that we will most likely not use all of these folders during the course of the book. Nevertheless, it is a good working practice to get these set up for every project just so that you have a standard template.

Note

This book will cover the process of creating...

Classes

Architecting the core of your game from the beginning is an often-skipped process. Because we're too eager to just get on and build our game, let's jump straight in and start placing assets in a scene, adding them as we go. This kind of practice is fine for prototypes (mostly, however, even with prototypes, a level of architecture is usually required). When building your actual product, however, without setting up a proper architecture from the beginning, you are heading toward a world of utter mess.

When we say architecture, it doesn't mean that you need to design everything (but it helps). You just need to ensure that you plan what you are going to build before you build it instead of thinking about stuff and checking Google for information on how to do it. Even if you are using some kind of an agile method, you should have a good framework and goal for each sprint as you plan for each sprint. This will guide you on what should be done and when, not just designing...

Planning behaviors

Behaviors are just a fancy way of saying things or interactions that will happen in the game. Breaking down these actions or reactions in this way helps to componentize how we think our game will work. Stopping and thinking about this from the very beginning means we won't get too many surprises later on. (There are always surprises after a good night's sleep.)

For example, behaviors can take the following forms:

  • Attacking another entity
  • Taking damage
  • Collecting the loot, which could be money or items
  • Teleporting to another land

It is also important to note that visual effects as the result of a behavior or action are different to those that affect the characteristics of the game's object. You may have a visual representation as the result of a behavior or action (for example, a particle effect when taking damage or the character swinging their sword to attack), but in Unity especially, you have to keep these separate. We will cover more on this later.

Note

In...

Getting your project started – the right way


Before you start your project for real, you should consider how you intend to set it up and architect your project in the long term. I've looked at or worked with far too many projects that have created problems for themselves by just diving in rather than designing the outline for the project at the start.

Your game and your assets are not the only things to consider when starting a fresh project. Sure, you can start importing assets, creating scripts, and getting things running; most Proof of Concept (POC) projects start this way. Once your project is of a sufficient size and you start expanding on your initial concept, you'll realize that you have issues with regards to picking up items and putting them together. Then, you will start devising new ways to organize your project and eventually find that it's an unmanageable mess; nevertheless, you will stride on, taking longer and longer to produce new content or add new features.

The best advice...

Understanding components


Components in Unity are the building blocks of any game; almost everything you will use or apply will end up as a component on a GameObject inspector in a scene.

Until you build your project, Unity doesn't know which components will be in the final game when your code actually runs (there is some magic applied in the editor). So, these components are not actually attached to your GameObject inspector but rather linked to them.

Accessing components using a shortcut

In most Unity examples, you are shown how to access components through scripts by using shortcuts to the MonoBehaviour class that the game object inherits from. Accessing the components with the help of the following code:

this.renderer.collider.attachedRigidbody.angularDrag = 0.2f;

Tip

If you need any help with this title or have any suggestions on it, join in on the support forum for the book at http://bit.ly/MasteringUnity2DForums. The forum will also keep you updated on any announcements on the title, so...

Creating the project


First things first! To ensure that you have all the bases covered, you need to start a new project. For 2D, however, you need to ensure that you start the project using the 2D game template in the Unity New Project wizard, as shown in the following screenshot:

If you accidentally leave the 3D option set (which is the default) as is, don't worry! You can change this at any time for your project through Editor Settings. To open Editor Settings, simply navigate to Edit | Project Settings | Editor, as shown in the following screenshot:

Lastly, let's create the folder structure for the project, as shown in the following screenshot, using the guidance provided earlier:

At this point, it's important to note that we will most likely not use all of these folders during the course of the book. Nevertheless, it is a good working practice to get these set up for every project just so that you have a standard template.

Note

This book will cover the process of creating your own project...

Classes


Architecting the core of your game from the beginning is an often-skipped process. Because we're too eager to just get on and build our game, let's jump straight in and start placing assets in a scene, adding them as we go. This kind of practice is fine for prototypes (mostly, however, even with prototypes, a level of architecture is usually required). When building your actual product, however, without setting up a proper architecture from the beginning, you are heading toward a world of utter mess.

When we say architecture, it doesn't mean that you need to design everything (but it helps). You just need to ensure that you plan what you are going to build before you build it instead of thinking about stuff and checking Google for information on how to do it. Even if you are using some kind of an agile method, you should have a good framework and goal for each sprint as you plan for each sprint. This will guide you on what should be done and when, not just designing the project on...

Left arrow icon Right arrow icon

Description

If you have C# knowledge but now want to become truly confident in creating fully functional 2D RPG games with Unity, then this book will show you everything you need to know.

What you will learn

  • Explore the new features introduced in Unity3D 4.3 by building your own RPG game Delve into the new sprite importing and rendering system Master the best parts of the new Sprite animation system and improved Mecanim features Make the most of your rendering with tags, layers, sprite sorting, and ordering Dissect the proper use of Delegates, Events, and Messaging, including tips and tricks, and build a full RPG conversation system Do battle with Mecanim-based AI and state machines Build for success with tips on marketing, monetization, and deployment Drive home the platform of choice and expand into deployment/editor extensions
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 26, 2014
Length: 474 pages
Edition : 1st
Language : English
ISBN-13 : 9781849697347
Vendor :
Unity Technologies
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Aug 26, 2014
Length: 474 pages
Edition : 1st
Language : English
ISBN-13 : 9781849697347
Vendor :
Unity Technologies
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.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
$199.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
$279.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 $ 109.98
Unity Game Development Blueprints
$48.99
Mastering Unity 2D game development
$60.99
Total $ 109.98 Stars icon

Table of Contents

14 Chapters
1. Overview Chevron down icon Chevron up icon
2. Building a Character Chevron down icon Chevron up icon
3. Getting Animated Chevron down icon Chevron up icon
4. The Game World Chevron down icon Chevron up icon
5. NPCs and Interactions Chevron down icon Chevron up icon
6. The Big Wild World Chevron down icon Chevron up icon
7. Encountering Enemies and Running Away Chevron down icon Chevron up icon
8. Shopping for Weapons Chevron down icon Chevron up icon
9. Getting Ready to Fight Chevron down icon Chevron up icon
10. Fight and Gain XP Chevron down icon Chevron up icon
11. Onward Wary Traveler Chevron down icon Chevron up icon
12. Deployment and Beyond Chevron down icon Chevron up icon
A. Additional Resources Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8
(21 Ratings)
5 star 38.1%
4 star 23.8%
3 star 14.3%
2 star 23.8%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Suraj May 18, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I just got it , thank you
Subscriber review Packt
Brett A. Brown Sep 14, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excellent beginner book. Very clear instructions with lots of screenshots. By noon I could make my monkey dance!
Amazon Verified review Amazon
Heleen Durston Nov 11, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Another Unity game engine book from Packtpub was released in October, “Mastering Unity 2D Game Development” by Simon Jackson. This is an intermediate to advanced guide to using the 2D features of Unity. The book centers around creating a 2D RPG complete with world map, a conversation system, a store, and the beginnings of a battle system. When the book was initially released there were a variety of typos and show-stopping problems with the book and code. Packtpub has recently reissued the book and code and now everything works well. Chapter 1 is an overview of the updates that occurred in Unity 4.3. I went through the book using Unity 4.6 beta and had no issues. “Mastering Unity 2D Game Development” is definitely meant for those with some experience using Unity and in particular the C# programming language. Many links are provided for reference throughout the book; but knowing how to use coroutines and delegates will definitely help you complete the examples in the book. Chapters 2 and 3 explain how to set up the foundation of the RPG project and begin with having you create a 2D game character and animate it. Importing sprites and using the Sprite editor are discussed and then Unity’s Mecanim animation system is explained while the main character gets animated. In chapter 4 the background for your initial scene is created and then populated with some trees, rocks and even store and house sprites. This chapter also talks about transitioning from the background edges to another scene that you will set-up. Chapter 5 is a heavily scripting-oriented chapter and has you create some NPCs and then set-up a conversation system. This chapter also discusses co-routines and how to use them in Unity. Chapter 6 discusses places online to have maps drawn for you and alternately gives advice on how to create your own game world map. The world map for the game is placed in your project and a nice fade-in technique is used to transition between the world map and the local scene for your player. In chapter 7 a battle system is created and the book explains how to have goblins randomly spawn in your scene. In chapter 8 a shop is created so that you can purchase a sword to fight the goblins from chapter 7. Both chapters discuss the creation of an inventory system and how to add the sword stats as a bonus to the battle system. Chapter 9 talks about using a state system for the battle and also programming a user interface system to keep the player informed about the battle. Everything seemed to work fine using the ‘old’ user interface system in Unity 4.6 beta. For those that don’t know the UI was quite significantly updated in Unity 4.6; but all the code in the books seems to work fine. Chapter 10 is all about creating particle systems for the battles in your RPG and chapter 11 discusses expanding the Unity editor to create some nice menus for your game. I found chapter 11 a really interesting read as I was not aware that you could expand Unity that much for your own purposes. Chapter 12 concludes the book with information on building your game for different platforms. There is also a lot of links listed presenting information on a variety of tutorials and tools for working with Unity. This is a great book for Unity and one that I will keep around for reference on how to do a lot of programming in Unity. I am really pleased that I had a chance to read the book and try the code. The link to the book on Packtpub is https://www.packtpub.com/game-development/mastering-unity-2d-game-development. You can also find the download with all the assets for the RPG project there and a PDF that has all the pictures from the book. Simon Jackson has a website at http://darkgenesis.zenithmoon.com/ and a forum for the book at http://darkgenesis.zenithmoon.com/DarkGenesisForums/forum/book-forums/ .
Amazon Verified review Amazon
Shaun Walsh Nov 10, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
So far so good. Havent made it too far into the book yet, but of all the Unity books I have read so far, this one is by far the best. Everything is broke down nice and simple, and there are plenty of illustrations to go along with it. Author is well versed and thorough. My only complaint (not about the book) but is with the kindle version. Please fix your format so that highlighted text works please.
Amazon Verified review Amazon
Muhammad Bilal Sep 01, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
great for people interested in game development
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 the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela