© Sebastiano M. Cossu 2021
S. M. CossuBeginning Game AI with Unityhttps://doi.org/10.1007/978-1-4842-6355-6_1

1. Introduction

Sebastiano M. Cossu1  
(1)
LONDON, UK
 

Among all the technologies that have been flourishing in the last decade, there is one that is becoming essential for our society and it’s enhancing all the other tech fields as well as every aspect of our life: Artificial Intelligence (AI). From navigation systems to smart cars and from virtual assistants to augmented reality (AR) applications on our smartphones, nearly every software and device we use features AI under the hood. Video games make no exception.

The more we go on, the more AI is present in game applications in the form of non-player character (NPC) , simulations, and more recently AR applications aiming to enhance the user experience. Machine learning algorithms are starting to be a common solution to enhance graphics and animations or even implement new gameplay features.

In this chapter, I will briefly present to you Artificial Intelligence and talk about its relationship with video games. Finally, I will present to you what this book has to offer for you and what to expect from it.

Let’s start with the basic question: What is Artificial Intelligence?

1.1 Artificial Intelligence

Intelligence is the trait that we, as humans, are most interested in. We survived in the feral world thanks to our intelligence that allowed us to impose ourselves on animals and the environment itself by understanding the laws of nature and how to craft and use tools to take advantage of them to sustain our species.

Thanks to our intelligence, we managed not only to survive but to evolve and build a world that would maximize our chances of survival, creating complex organizational systems to satisfy all our needs.

This is why we value intelligence so much even now. We judge people by their intelligence; we value them by their ability to think out of the box and optimize processes or get the best results in some specific process. To us, intelligence is not only the ability to think, but more importantly it’s the ability to perceive and understand the world that surrounds us and act in order to exploit it to reach our goals.

Intelligence is the blessing that Nature gifted us, and with Artificial Intelligence, we aim to pass the torch to our own creations: machines.

It’s still debated if Artificial Intelligence is more about making machines think and act like humans or if it’s about creating machines with the gift of rational thinking that do things purposefully in the best possible way exploiting the environment. Both the directions are completely legit, and they are leading to very interesting results. In this book, we will explore the interpretation of AI intended to create rational agents that can process the perceptions from the environment and elaborate them applying a reasoning process to come up with a solution (an action or a sequence of them) to solve a complex problem.

In the next section, we will see more in detail what is an intelligent agent and in which applications they are involved.

1.1.1 Intelligent Agents

An intelligent agent is the combination of an intelligent system and an agent.

An intelligent system is an apparatus capable of processing information in order to reach a specific goal, while an agent is something that acts and reacts to the information it receives. Therefore, an intelligent agent is a system capable of processing perceived information and taking action in order to reach a specific goal.

An agent is made of sensors to perceive the environment and actuators that allow them to take action (Figure 1-1).
../images/491049_1_En_1_Chapter/491049_1_En_1_Fig1_HTML.jpg
Figure 1-1

An agent has sensors to perceive the environment and actuators to act on it

It’s easy to picture in our head an intelligent agent as a robot with devices like photocells, cameras, antennas, or other kinds of sensors to perceive the environment, a computer to elaborate information and to come out with an action that will be executed using some kind of actuators, like robotic arms, wheels, or other kinds of mechanisms to interact with the world.

From a mathematical point of view, an intelligent agent can be seen as a function where the arguments are perceptions; the logic of the function allows the elaboration of perceptions which takes to a result (the return value) which is the final action to be taken.

This can be represented by the following definition:
→→ P* → A

which means that an agent function → maps every possible percept sequence P* to an appropriate action A. Obviously, this concept is easily representable in a programming language, and it will be the foundation of our work to create intelligent agents with Unity.

Before starting to talk about Unity, the game engine we will use to explore AI, let’s see how AI and intelligent agents relate to the world of video games.

1.1.2 AI in Video Games

From the very beginning of the history of video games, AI was part of it. In fact, one of the very first AI applications were intelligent agents capable of playing games by their own both against other AIs and humans. That kind of intelligent agents are just programs that can play a game by themselves without the help of humans. Their goal is to make the best choices to win the game. They perceive the game environment, elaborate the collected data, and come out with the most convenient action.

Autonomous playing agents had their first great victory in 1997 when Deep Blue, the AI built by IBM, defeated the world champion Garry Kasparov in a game of chess. Kasparov commented on that game saying, “That day I sensed a new kind of intelligence.”

Today, winning a game of chess against an AI is just impossible. Another more recent victory of the AI against humans was registered in 2011 when Watson, the new AI built by IBM, won by a long shot at Jeopardy! against human players. Watson’s achievement was way more impressive than Deep Blue’s because to win Jeopardy!, you need to have a great knowledge of pop culture and be able to link facts in fields like music, gossip, cinema, and so on. While we expect an Artificial Intelligence to be good at rational reasoning activities like math and chess, we don’t expect them to have a better understanding of our culture, which Watson proved to have – this is what made that achievement so important.

With the increasing popularity of video games, in the 1970s, AI started to focus on creating compelling enemies for single-player games. Some of the first games to do so were Speed Race (Taito, 1974), Qwak! (Atari, 1974), and Pursuit (Kee Games, 1975). Following those precursors, in the so-called Golden Age of video games, AI started to become more popular and common in video games, and some of the first interesting applications started to show up, for example, the acrobatic maneuvers of enemies breaking out of formations in games like Galaxian (Namco, 1979) and Galaga (Namco, 1981) or the original approach of Pac-Man (Namco, 1980) in which the enemies had distinct personalities and tried to chase the player by combining efforts and using actual strategies based on pathfinding techniques and application of patterns to chase the player.

Following the popularity of AI applications in the Golden Age, video games started to implement intelligent behaviors featuring increased complexity. In particular, those were the years in which tactical and action RPG started to show up with games like Dragon Quest IV, Baldur’s Gate, and Secret of Mana which implemented some interesting new features, like the possibility to issue orders and set behaviors for the members of the party so that they could fight in battles in autonomy. Other interesting techniques started to rise to simulate team efforts in sports games too. Raising the complexity of the applications, the limits of the current technologies started to show up, and more complex solutions were found. In particular, the 1990s were the years in which video games started to implement formal AI techniques. Finite-State Machine (FSM) is one of those, and it was (and still is) one of the most popular. FSM can elegantly solve a broad set of problems affecting nearly every game genre, but probably the most popular applications are enemies’ behaviours in action games; in this type of games, enemies have to react to situations and execute appropriate actions, and those behaviors can be represented as FSMs.

The most popular examples of video games using FSM are Pac-Man (Namco, 1980) and Ms. Pac-Man (Namco, 1981). They introduced the concept of enemies adapting to different situations and changing their behaviors deciding to chase or flee the player. But that’s not all; in fact, Pac-Man and Ms Pac-Man were also the first games where the enemies were looking like they were collaborating towards the common goal of defeating the player. In fact, the enemies in both games were four different ghosts with four different personalities, and they were following complementary strategies to chase the player, making them looking like they were actually collaborating following a big and well organized chasing plan. All this was achieved thanks to FSM which is basically a graph where you associate states to actions, making the NPC take a specific action according to the state it (or the game world) is in. This way, the player has the impression that the NPC is actually thinking and reacting rationally to a specific situation.

The Finite State Machine is not a thing of the past, in fact, we can still find it in very modern and complex games like the Tomb Raider series of games or the Battlefield or Call of Duty series of games. Generally, most of the action games are implementing FSM to manage their NPCs’ behaviours. This is because FSM is easy to implement and has low impact on performances, and yet it provides a genuine and credible user experience when well designed. Other AI approaches require many more resources and work, and in some game genres and situations, they don’t even create a more immersive or credible result compared to their FSM equivalent.

In today’s video games, AI is not only used to create intelligent agents. We are recently seeing the rise of augmented reality (AR) which uses computer vision techniques to enhance objects in the real world with virtual elements. One of the most important applications of that kind is Pokémon Go (Niantic, 2016), which allows players to see and catch Pokémons in the real world using a smartphone.

Other interesting applications of AI in video games arise with the advancement and popularity of machine learning, which is used to enhance graphics and gameplay elements in many new video games. A very interesting example can be seen in Warframe (Digital Extremes, 2013), where a machine learning algorithm is used to teach the enemies how to wall-run by observing the players doing it and processing that data to mark all the runnable walls and the starting and landing points for those wall-runs.

There were some famous examples of more advanced AI techniques used in video games, like the Creatures series of games by Steve Grand that created one of the most famous and important Artificial Life (AL) games, which is still today remembered as one of the most complex and genius AL games. Creatures used many interesting AI techniques like genetics algorithms and neural networks to create virtual-biological beings that could transmit their genetic traits to their offspring by mating and that were able to learn by experimenting in their environment and learning from the consequences of their actions. This is one of the most interesting and important products of AL, and I strongly suggest you to buy and play the game and to read the book written by Steve Grand about the making of the game: Creation: Life and How to Make It.

No doubt, AI is becoming more and more important to enhance the overall experience of video games in many different ways and to keep up with all the changes and evolutions brought by AI. It’s wise to start learning from the very beginning; this means understanding how to create basic intelligent agents. To reach this goal, we will make use of many different tools belonging to Computer Science and Math. The concepts explained will be implemented in Unity using the C# programming language. For simplicity’s sake, we will not use complex 3D models, but only very basic 3D objects (mostly geometrical 3D shapes like cubes and spheres) as the focus here is not to make a nice-looking video game, but to understand useful game AI techniques to reuse and adapt to any game project.

1.2 Unity

Unity needs no introductions. It’s probably the most popular game engine around, and it helped making game development more accessible to indie developers and small studios.

Our choice to use Unity is mostly dictated by its simplicity and completeness. It’s a game engine that’s very easy to pick up, but at the same time, it features many advanced tools, and its libraries offer many interesting and useful functions that will make our work way easier, letting us concentrate on the interesting parts.

In Unity, you program using C#, an OOP language that offers many advanced features packed in a gorgeous C-like syntax. It is an extremely powerful and clear programming language that allows us to achieve complex goals with smart tools and clarity. Moreover, it’s the best choice if we want to use all the features that Unity has to offer. Last but not least, it’s based on the object-oriented programming (OOP) paradigm, which allows for more structured and modular coding style.

All those characteristics make C# a good choice for this book not only because it’s a good tool to get the work done but also because it’s extremely good to learn coding and master some techniques we are going to use in the book.

The first step to get the benefits of using Unity and C# is to actually install Unity, so let’s see how to do it!

1.2.1 Installing Unity Hub

Unity is a free software; the first step to get it installed on your machine is to download Unity Hub from the official website: https://stsssore.unity.com/download.

Unity Hub is the software that will help you organize and keep track of all your Unity projects and Unity versions. With Unity Hub, you can install or uninstall specific versions of Unity and add or remove Unity projects and link them to specific versions of Unity very easily.

Once on the download page, you will be asked to accept the license agreement, after which it will be possible to click the download button to get the installer.

After downloading the installer, you will need to follow a different process depending on whether you are installing it on Windows, Mac, or Linux:
  • On Windows, you have to accept the license agreement and choose a path on your drive on which you want Unity Hub to be installed, then press “Install” and the software will get your machine the latest version of the Unity Hub software.

  • On Mac, you just need to drag the app into your Application folder, as usual.

  • On Linux, you will need to open the Unity Hub installer (which in this case is an AppImage file) with the AppImage file Launcher.

1.2.2 Activating Your Unity ID and License

Starting Unity Hub for the first time, you will be prompted to the License Management window: here, you will be asked to activate a license to start using Unity (Figure 1-2).
../images/491049_1_En_1_Chapter/491049_1_En_1_Fig2_HTML.jpg
Figure 1-2

License Management window. You need to have a Unity ID and activate a license to start using Unity

Before you do that, you need to create a free Unity account, which you can do by clicking the Unity ID icon on the top right of the screen and selecting Sign In (Figure 1-3) and “create one” (Figure 1-4).
../images/491049_1_En_1_Chapter/491049_1_En_1_Fig3_HTML.jpg
Figure 1-3

Click the Unity ID icon and then Sign In to log in or register your Unity ID

../images/491049_1_En_1_Chapter/491049_1_En_1_Fig4_HTML.jpg
Figure 1-4

This is the log-in form; click “create one” to register a Unity ID

A new window containing a registration form will open up (Figure 1-5). Just fill the form or use one of the two buttons if you rather register using your Google or Facebook account.
../images/491049_1_En_1_Chapter/491049_1_En_1_Fig5_HTML.jpg
Figure 1-5

Fill this form to create a Unity ID, or click the buttons at the bottom of the window if you want to create it using your Google or Facebook account

Now that you are logged in with your Unity ID, you can activate a license. You can do that in the License Management window (Figure 1-6) in two ways:
  • Manual Activation: This is useful if you don’t have an active connection to the Internet. You can create a license request file and upload it later to https://license.unity3d.com/manual.

  • Guided Activation: You need to follow some steps and answer some questions to automatically activate a license on your Unity ID.

../images/491049_1_En_1_Chapter/491049_1_En_1_Fig6_HTML.jpg
Figure 1-6

Now that you logged in your Unity ID, you can request a license with one of the two blue buttons in the top right of the window

1.2.2.1 Manual Activation

If you want to activate your license manually, click the Manual Activation button in the License Management section (Figure 1-6). You will be prompted with a small window asking you to generate and save a license request file to be later uploaded on https://license.unity3d.com/manual (Figure 1-7). Click the “SAVE LICENSE REQUEST” button and save the license request file on your computer (Figure 1-8).
../images/491049_1_En_1_Chapter/491049_1_En_1_Fig7_HTML.jpg
Figure 1-7

Manual Activation view. Here, you can request your license to be later uploaded on https://license.unity3d.com/manual

../images/491049_1_En_1_Chapter/491049_1_En_1_Fig8_HTML.jpg
Figure 1-8

Save your license request file and upload it on https://license.unity3d.com/manual

After saving the file on your computer, you need to go to https://license.unity3d.com/manual and upload the file you just saved (Figure 1-9) to the uploading form and then press the Next button.
../images/491049_1_En_1_Chapter/491049_1_En_1_Fig9_HTML.jpg
Figure 1-9

Uploading the license request file on https://license.unity3d.com/manual

Uploading your license request will activate your account automatically for your Unity ID in Unity Hub, allowing you to download Unity on your PC and start using it.

1.2.2.2 Guided Activation

The guided activation needs you to have a working Internet connection, but it’s faster and easier.

You need to click “Activate New License” in the window shown in Figure 1-6; then you will be presented with a new window asking you what kind of license you want to activate (Figure 1-10) and if you plan to use Unity for commercial or learning purposes (Figure 1-11). Make your choice based on your use case and then click “Done.”
../images/491049_1_En_1_Chapter/491049_1_En_1_Fig10_HTML.jpg
Figure 1-10

You can choose between two classes of licenses: Unity Personal (made for individuals and amateurs) and Unity Plus or Pro (made for professionals)

../images/491049_1_En_1_Chapter/491049_1_En_1_Fig11_HTML.jpg
Figure 1-11

You can use Unity for free, if you don’t plan to sell your game, but there are also interesting plans if your company earns less than a certain amount

After completing all the steps, the License Management window will contain the details of your new active license, meaning you are all set up (Figure 1-12).
../images/491049_1_En_1_Chapter/491049_1_En_1_Fig12_HTML.jpg
Figure 1-12

A new license was activated, and Unity Hub is now ready to be used!

1.2.2.3 Installing Unity

After activating your Unity ID and license, you need to download a version of Unity from Unity Hub.

Click Installs in the left side of the window, and you will be prompted to the Installs view which should list all the available versions of Unity installed on the computer (Figure 1-13).
../images/491049_1_En_1_Chapter/491049_1_En_1_Fig13_HTML.jpg
Figure 1-13

This is the Installs view. Here, you can add or delete different versions of Unity

From the Installs view , click the Add button to select a new version of Unity to install and a pop-up listing all the available versions of Unity will be shown as in Figure 1-14.
../images/491049_1_En_1_Chapter/491049_1_En_1_Fig14_HTML.jpg
Figure 1-14

The first step to install Unity asks you to pick a version of the engine

To follow this book, you will not be forced to use a specific version of Unity, since we are not going to use any specific feature of the software; anyway, the UI of the engine may differ from version to version, so I suggest you to not use a version older than the LTS version of 2018. I will be using the LTS version from 2019 for this book.

Once you select the right version of Unity, click the Next button.

Once you selected the right version and clicked Next, you will be asked to choose which modules to install (Figure 1-15). Those modules will allow you to export your project to several different platforms, from desktop to mobile and the Web and others a bit more specific like tvOS and Vuforia.
../images/491049_1_En_1_Chapter/491049_1_En_1_Fig15_HTML.jpg
Figure 1-15

The second step to install Unity asks you to choose which modules you want to install. Those modules will allow you to export your project to many different platforms

For this book, we will only export on desktop, so select the module related to the desktop operating system (OS) you are using. For example, if you are using a Mac, you may want to install the Mac Build Support module; if you’re using Linux, install Linux Build Support; and if you’re on Windows (as myself), install Windows Build Support. I also suggest you to install the Documentation module which is always useful to have locally on your PC.

Finally, click the Done button to conclude the process and start downloading and installing the version of Unity you just selected.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.137.240