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
Free Learning
Arrow right icon
Unity Game Development Scripting
Unity Game Development Scripting

Unity Game Development Scripting: Write efficient C# scripts to create modular key game elements that are usable for any kind of Unity project

eBook
₹799.99 ₹2919.99
Paperback
₹3649.99
Subscription
Free Trial
Renews at ₹800p/m

What do you get with a Packt Subscription?

Free for first 7 days. ₹800 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

Unity Game Development Scripting

Chapter 2. GUI Time

In video games, the GUI is one of the most important parts of the game. It's where you see how much health you have, what level you are at, how much gold you're carrying, and so on. It's how you gather information on your character and the world you are in.

In this chapter, we'll be covering the following topics:

  • Both 2D and 3D GUI elements
  • How to create GUI buttons
  • How to create a 2D health bar
  • Tracking the player's level by using a GUI label
  • Using two GUI boxes to make an experience bar
  • Creating a 3D health bar
  • Showing 3D damage reports
  • Showing enemy name tags

A traditional 2D UI

To start our GUI programming, we'll create some buttons, a health bar, a level counter, and an experience counter. All of these are common in a variety of video games and are key elements to game design.

Setting up our scene

Since in this part of the chapter we will be dealing with 3D space, we'll need to set up a scene to test with. We'll only need a basic test level, so create a new scene and name it Chapter 2. Add a cube or plane for the floor, a directional light, a GameObject for our enemy, and two quads. This is what my scene looks like:

Setting up our scene

Set the two quads in the same position just above your enemy object and rotate one of them by 180 degrees so that the two quads look like one object. Drag one of the quads onto the other, making a parent-child object. Now that our scene is ready, let's get to the scripting.

Housing our 2D UI

Before we start creating our 2D UI, let's make the script to house it all. Create a new C# script and name it GUI_2D...

Housing our 3D UI

We will follow similar steps when housing our 3D UI as we did when housing our 2D UI. Create a new script and call it GUI_3D.

Creating a 3D health bar

Our first step will be to add our variables needed for the health bar:

public float currentHealth = 100;
public float maximumHealth = 100;
float currentBarLength;
public Transform HealthBar;
Vector3 OrigScale;

The first three variables are what we'll use to calculate our health bar. The Transform variable is how we'll interact with our 3D object that's being used as our health bar. The Vector3 variable is a reference point for when we scale the bar.

Our next step will be to add a Start() function. We'll use the Start() function to set the OrigScale variable:

void Start()
{
  OrigScale = HealthBar.transform.localScale;
}

We set OrigScale before we do anything else in the health bar. This is what we'll use as a reference point for the health bar. Next, we'll create our Update() function:

void Update(...

Summary

In this chapter, you learned a few ways to create a GUI. First, you started off by learning the traditional way of creating the GUI by making buttons, bars, and text. Then, we switched gears and learned how to make a 3D GUI by making 3D health bars, 3D damage reports, and 3D name tags.

In the next chapter, you will learn how to make a few different classes for in-game items. First, we'll create a class so that items will affect the player, the next class will allow items to affect other objects on touch, and the final class will be created for projectiles. All of these can be used for the player, enemies, or environment objects.

Left arrow icon Right arrow icon

Description

The intuitive and powerful Unity game engine is one of the most widely used and best loved packages for game development. Unity scripting is an essential but challenging skill to master in order to create custom game elements. Learning modular scripting allows you to rewrite as little code as possible as you deploy your scripts to multiple projects and work easier, quicker, and more efficiently than before. In each chapter of this book, you'll learn how to script new game elements. Beginning with making custom controls for the keyboard and mouse, as well as the Xbox 360 Controller, you'll then get to grips with more complex systems such as inventory, data saving, and artificial intelligence. As you create these elements, you'll also learn how to make your scripts simpler and easy to use. This will allow drag-and-drop deployment, which is to be used by designers and nonprogrammers. Finally, you'll combine all of your newfound skills to create your own complete game project.

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Dec 22, 2014
Length: 202 pages
Edition : 1st
Language : English
ISBN-13 : 9781783553631
Vendor :
Unity Technologies
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. ₹800 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 : Dec 22, 2014
Length: 202 pages
Edition : 1st
Language : English
ISBN-13 : 9781783553631
Vendor :
Unity Technologies
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
₹800 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
₹4500 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 ₹400 each
Feature tick icon Exclusive print discounts
₹5000 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 ₹400 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 7,299.98
Unity Game Development Scripting
₹3649.99
Unity Game Development Blueprints
₹3649.99
Total 7,299.98 Stars icon
Banner background image

Table of Contents

11 Chapters
1. Interactive Input Chevron down icon Chevron up icon
2. GUI Time Chevron down icon Chevron up icon
3. Expandable Item Classes Chevron down icon Chevron up icon
4. Inventory Chevron down icon Chevron up icon
5. Enemy and Friendly AIs Chevron down icon Chevron up icon
6. Keeping Score Chevron down icon Chevron up icon
7. Creating Save and Load Systems Chevron down icon Chevron up icon
8. Aural Integration Chevron down icon Chevron up icon
9. Game Settings Chevron down icon Chevron up icon
10. Putting It All Together 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 Full star icon Half star icon 4.1
(7 Ratings)
5 star 42.9%
4 star 28.6%
3 star 28.6%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




MR B BYFORD Feb 16, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Hugely useful overview of various game design has its and how they can work in unity.Got this after the 2d game development in unity book, and was please at how it built on my knowledge and didn't spell things that I already knew. Not for absolute beginners.
Amazon Verified review Amazon
Mário Freitas Mar 01, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The book is written in very concise and simple to understand way, guiding the reader the whole way to actually create a simple working game, and covering most topics on game development scripting.I would highly recommend this book to people who are new to Unity scripting and to those who would like to learn more about how to structurally and conceptually organize a game from the point of view of game development scripting.The book also offers standard approaches to the implementation of features that are present and required in any game.
Amazon Verified review Amazon
Peter Matthew Apr 30, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
As a Lecturer in Games Development I am always on the lookout for books that can help my students improve their understanding of both c# and Unity3d. This book provides a lot of very good tutorials and explanations that cover a variety of subjects within the games development area. The sections are clearly written and provides good examples of the code being discussed. After discussing the book with some of my second year students they described it as “very helpful” and “easier to understand than a lot of other books”.Some criticisms of the book are that the sections are a little abstract in places jumping from topic to topic and it relies on a previous understanding of coding knowledge to get the most out of it, therefore lending itself more to an intermediate or advanced user.Regardless the author does an excellent job of explaining useful scripts that most games developers will want to include: such as re -bindable controller settings and user interface development, therefore providing the reader with a practical set of scripting examples.I would recommend this book for anyone wanting to improve their understanding of Unity scripting and my department is now using this book as part of our essential reading list for all second year games programmers.
Amazon Verified review Amazon
Robert L. Dixon Feb 07, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
I read a lot of books about Unity and C# since I'm always trying to up my game, so to speak. This book has some good sections but overall doesn't cohere as well as it could. But it could be useful for people with some Unity experience who are looking for specific techniques.It jumps right into scripting individual subsystems without talking about what kind of game is going to be built and how everything will fit together. Later on you learn the example game is an RPG-style game with characters, weapons and objects. It would have been nice to know this ahead of time, because some of the chapters are only relevant to this type of game.Overall this is more like a "cookbook" of various recipes, as opposed to a step-by-step guide to building a real game. If you already have some experience coding C# in Unity, and you are interested in some of the chapter topics, you should find the book useful.Here's a brief rundown on some specific chapters:* The first chapter on Interactive Input present a nice technique for switching among input control schemes. However there's an emphasis on supporting the XBox 360 controller which might not be relevant to a lot of developers.* The GUI chapter relies a little too heavily on the old, mostly obsolete Unity GUI approach, so it is not too useful in the post-Unity-4.6 world.* The Expandable Item Classes chapter presents a nice starting point for structuring in-game items that affect player characters and objects.* The chapter on AI covers two different AI topics: simple enemy decision-making (switching an enemy object among Idle, Guard, Combat, Flee states), and pathfinding. The pathfinding section gives a good introduction to Unity's NavMesh system, which is not covered in many other books. The example for this chapter uses the AI scripts to animate some freely-available skeleton models (from the Asset Store) around an environment.* The chapter about Keeping Score on the other hand presents some real brute-force coding with hard-coded achievement thresholds and such. There are better and simpler ways to code this, so I would give this chapter a pass.* The same could be said for the next chapter, which covers Saving and Loading Data. It uses XML, which is generally heavier and harder to work with than JSON, and again shows some brute-force coding that might not hold up well over time.* Chapter 8 deals with audio and offers some simple, reusable classes for managing and playing sounds.* The final chapter, "Putting It All Together," builds a simple game by assembling all the subsystems from previous chapters. It's very nice to see all the parts working together at the end. This chapter could have been fleshed out a bit more. It would have been nice to have an overview, like a diagram, of how all the subsystems fit together, rather than a step-by-step "now add this, now add this" sequence. In fact this kind of overview would have been really useful prior to Chapter 1, so the reader can go into the rest of the book knowing what to expect.Overall there are a lot of things to learn from this book. In addition there is a nice emphasis on play-testing at the end of each chapter, with suggestions of ways to enhance the examples in your own games. But it's not a complete guide to building your own games in Unity.
Amazon Verified review Amazon
Matthew Feb 09, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Bottom line up front, this book is a great tool to keep in your kit bag. With a basic knowledge of programming and Unity, I started an indie game development company with several game ideas in mind. What I quickly discovered was, like most things in life, there are multiple ways to accomplish a single task within Unity. This created a challenge I hadn’t planned for. Originally I thought it would be difficult to find the right literature to help me achieve my goal, but instead I found that I was inundated with information. I recently came across “Unity Game Development Scripting” by Kyle D'Aoust and became quite pleased with what I had found. The book is laid out in a very efficient manner covering all the major aspects of creating a game. The first chapter deals with the most important aspect, defining user input. It lays out two options one for keyboards and the other for an Xbox controller. At first glance the reader may be tempted to think this doesn’t apply to me because I’m not developing for those platforms. This may be true, but if the reader looks deeper they will start to see how the techniques used in the book can transfer into other projects. The process the writer took to define different aspects and requirements for control are useful when designing the controls for all games. Chapters 5 and 7 are also great examples of a situation where the techniques used are beneficial to any reader attempting to develop a game in Unity. I have personally struggled with scripting enemy artificial intelligence in a 2D platformer game and have waffled between different techniques. After reading chapter 5, I was able to add a more efficient solution to my games even though the writer is referencing 3D game creation. In addition, chapter 7 answers the question of saving game progress. When I first started developing games this task perplexed me, but “Unity Game Development Scripting” by Kyle D'Aoust does an excellent job of defining C# scripting that accomplishes the task. Though I was overall happy with this book, there was one point of contention I had with the way it was written. The author provides numerous coding examples written in C# throughout each chapter. He does a good job giving a description after each block of coding, but I found it to be rather taxing on the mind to connect the explanations with the code previously read. A better way to accomplish this would have been to add comments directly into the code itself. This would have personally helped me better follow the readings. Again, this book is an excellent tool for any developer to have. If you don’t have an intermediate understanding of programming and Unity, you will probably find this book to be frustrating as it is not a tutorial based piece of work. But if you are looking for a resource to help refine and/or add to your own techniques for game development then this is the right place to start.
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.