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

Buch teilen
  1. 366 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
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

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

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.

]]>

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Learning C# by Developing Games with Unity 2020 als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu Learning C# by Developing Games with Unity 2020 von Harrison Ferrone im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Informatik & Programmierung in C#. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

Information

Jahr
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...

Inhaltsverzeichnis