Learning C# by Developing Games with Unity 2020
eBook - ePub

Learning C# by Developing Games with Unity 2020

An enjoyable and intuitive approach to getting started with C# programming and Unity, 5th Edition

Harrison Ferrone

Share book
  1. 366 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Learning C# by Developing Games with Unity 2020

An enjoyable and intuitive approach to getting started with C# programming and Unity, 5th Edition

Harrison Ferrone

Book details
Book preview
Table of contents
Citations

About This Book

Key Features

  • Understand C# programming basics, terminology, and coding best practices
  • Put your knowledge of C# concepts into practice by building a fun and playable game
  • Come away with a clear direction for taking your C# programming and Unity game development skills to the next level

Book Description

Over the years, the Learning C# by Developing Games with Unity series has established itself as a popular choice for getting up to speed with C#, a powerful and versatile programming language that can be applied in a wide array of application areas. This book presents a clear path for learning C# programming from the ground up without complex jargon or unclear programming logic, all while building a simple game with Unity.This fifth edition has been updated to introduce modern C# features with the latest version of the Unity game engine, and a new chapter has been added on intermediate collection types. Starting with the basics of software programming and the C# language, you'll learn the core concepts of programming in C#, including variables, classes, and object-oriented programming. Once you've got to grips with C# programming, you'll enter the world of Unity game development and discover how you can create C# scripts for simple game mechanics. Throughout the book, you'll gain hands-on experience with programming best practices to help you take your Unity and C# skills to the next level.By the end of this book, you'll be able to leverage the C# language to build your own real-world Unity game development projects.

What you will learn

  • Discover easy-to-follow steps and examples for learning C# programming fundamentals
  • Get to grips with creating and implementing scripts in Unity
  • Create basic game mechanics such as player controllers and shooting projectiles using C#
  • Understand the concepts of interfaces and abstract classes
  • Leverage the power of the latest C# features to solve complex programming problems
  • Become familiar with stacks, queues, exceptions, error handling, and other core C# concepts
  • Explore the basics of artificial intelligence (AI) for games and implement them to control enemy behavior

Who this book is for

If you're a developer, programmer, hobbyist, or anyone who wants to get started with C# programming in a fun and engaging manner, this book is for you. Prior experience in programming or Unity is not required.

]]>

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Learning C# by Developing Games with Unity 2020 an online PDF/ePUB?
Yes, you can access Learning C# by Developing Games with Unity 2020 by Harrison Ferrone in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in C#. We have over one million books available in our catalogue for you to explore.

Information

Year
2020
ISBN
9781800204447
Scripting Game Mechanics
In the last chapter, we focused on using code to move the player and camera, with a trip into Unity physics on the side. However, controlling a playable character isn't enough to make a compelling game; in fact, it's probably the one area that remains fairly constant across different titles.
A game's unique spark comes from its core mechanics, and the feeling of power and agency those mechanics give to the players. Without fun and engrossing ways to affect the virtual environment you've created, your game doesn't stand a chance of repeat play, to say nothing of fun. As we venture into implementing the game's mechanics, we'll also be upgrading our knowledge of C# and its intermediate-level features.
This chapter will round out the Hero Born prototype by focusing on individually implemented game mechanics, as well as the basics of system design and user interfaces (UIs). You'll be diving into the following topics:
  • Adding jumps
  • Understanding layer masks
  • Instantiating objects and prefabs
  • Understanding game manager classes
  • Getter and setter properties
  • Keeping score
  • Scripting UIs

Adding jumps

One great thing about using a Rigidbody component to control player movement is that we can easily add in different mechanics that rely on applied force, such as jumping. In this section, we'll get our player jumping and write our first utility function.
A utility function is a class method that performs some kind of grunt work so that we don't clutter up gameplay code—for instance, wanting to check whether the player capsule is touching the ground to jump.
Before that, you'll need to get acquainted with a new data type called enumerations, which you'll do in the following section.

Introducing enumerations

By definition, an enumeration type is a set, or collection, of named constants that belong to the same variable. These are useful when you want a collection of different values, but with the added benefit of them all being of the same parent type.
It's easier to show rather than tell with enumerations, so let's take a look at their syntax in the following code snippet:
enum PlayerAction { Attack, Defend, Flee };
Let's break down how this works, as follows:
  • The enum keyword declares the type followed by the variable name.
  • The different values an enum can have are written inside curly brackets, separated by a comma (except for the last item).
  • The enum has to end with a semicolon, just like all other data types we've worked with.
To declare an enumeration variable, we use the following syntax:
PlayerAction currentAction = PlayerAction.Defend;
Again, we can break this down, as follows:
  • The type is set as PlayerAction.
  • The variable is named and set equal to a PlayerAction value.
  • Each enum constant can be accessed using dot notation.
Enumerations may look simple at first glance, but they are extremely powerful in the right situations. One of their most useful features is the ability to store underlying types, which is the next subject you'll be jumping into.

Underlying types

Enums also come with an underlying type, meaning that each constant inside the curly brackets has an associated value. The default underlying type is int and starts at 0, just like arrays, with each sequential constant getting the next highest number.
Not all types are created equal—underlying types for enumerations are limited to byte, sbyte, short, ushort, int, uint, long, and ulong. These are called integral types, which are used to specify the size of numeric values that a variable can store.
This is a bit advanced for this book, but you'll be using int in most cases. More information on these types can be found here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/enum.
For example, our enum PlayerAction values right now are listed as follows, even though they aren't explicitly written out:
enum PlayerAction { Attack = 0, Defend = 1, Flee = 2 };
There's no rule that says underlying values need to start at 0; in fact, all you have to do is specify the first value and then C# increments the rest of the values for us, as illustrated in the following code snippet:
enumPlayerAction { Attack = 5, Defend, Flee };
In the preceding example, Defend equals 6, and Flee equals 7 automatically. However, if we wanted the PlayerAction enum to hold non-sequential values, we could explicitly add them in, like this:
enum PlayerAction { Attack = 10, Defend = 5, Flee = 0};
We can even change the underlying type of PlayerAction to any of the approved types by adding a colon after the enum name, as follows:
enum PlayerAction : byte { Attack, Defend, Flee };
Retrieving an enum's underlying type takes an explicit conversion, but we've already covered those, so the following syntax shouldn't be a surprise:
enum PlayerAction { Attack = 10, Defend = 5, Flee = 0};

PlayerAction currentAction = PlayerAction.Attack;
int actionCost = (int)currentAction;
Enumerations are extremely powerful tools in your programming arsenal. Your next challenge is to use your knowledge of enumerations to gather more specific user...

Table of contents