Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Unity 2017 Game AI Programming - Third Edition - Third Edition

You're reading from  Unity 2017 Game AI Programming - Third Edition - Third Edition

Product type Book
Published in Jan 2018
Publisher Packt
ISBN-13 9781788477901
Pages 254 pages
Edition 3rd Edition
Languages

Table of Contents (10) Chapters

Preface 1. The Basics of AI in Games 2. Finite State Machines and You 3. Implementing Sensors 4. Finding Your Way 5. Flocks and Crowds 6. Behavior Trees 7. Using Fuzzy Logic to Make Your AI Seem Alive 8. How It All Comes Together 9. Other Books You May Enjoy

Using Fuzzy Logic to Make Your AI Seem Alive

Fuzzy logic is a fantastic way to represent the rules of your game in a more nuanced way. Perhaps more so than other concepts in this book, fuzzy logic is a very math-heavy topic. Most of the information can be represented purely in mathematical functions. For the sake of teaching the important concepts as they apply to Unity, most of the math has been simplified and implemented using Unity's built-in features. Of course, if you are the type who loves math, this is a somewhat deep topic in that regard, so feel free to take the concepts covered in this book and run with them! In this chapter, we'll learn the following:

  • What fuzzy logic is
  • Where fuzzy logic is used
  • How to implement fuzzy logic controllers
  • What the other creative uses for fuzzy logic concepts are

Technical Requirements

Defining fuzzy logic

The simplest way to define fuzzy logic is by comparison to binary logic. In the previous chapters, we looked at transition rules as true or false or 0 or 1 values. Is something visible? Is it at least a certain distance away? Even in instances where multiple values were being evaluated, all of the values had exactly two outcomes; thus, they were binary. In contrast, fuzzy values represent a much richer range of possibilities, where each value is represented as a float rather than an integer. We stop looking at values as 0 or 1, and we start looking at them as 0 to 1.

A common example used to describe fuzzy logic is temperature. Fuzzy logic allows us to make decisions based on non-specific data. I can step outside on a sunny Californian summer's day and ascertain that it is warm, without knowing the temperature precisely. Conversely, if I were to find...

Using fuzzy logic

Once you understand the simple concepts behind fuzzy logic, it's easy to start thinking of the many, many ways in which it can be useful. In reality, it's just another tool in our belt, and each job requires different tools.

Fuzzy logic is great at taking some data, evaluating it in a similar way to how a human would (albeit in a much simpler way), and then translating the data back to information that is usable by the system.

Fuzzy logic controllers have several real-world use cases. Some are more obvious than others, and while these are by no means one-to-one comparisons to our usage in game AI, they serve to illustrate a point:

  • Heating ventilation and air conditioning (HVAC) systems: The temperature example when talking about fuzzy logic is not only a good theoretical approach to explaining fuzzy logic, but also a very common real-world example...

Using the resulting crisp data

The data output from a fuzzy controller can then be plugged into a behavior tree or a finite state machine. Of course, we can also combine multiple controllers' output to make decisions. In fact, we can take a whole bunch of them to achieve the most realistic or interesting result (as realistic as a magic-using wizard can be, anyway).

The following figure illustrates a potential set of fuzzy logic controllers it can be used to determine whether or not to cast the heal spell:

We've looked at the health question already, but what about the rest? We have another set of questions that really don't mean much to our agent on their own:

Do you have enough mana? Well, you can have a little bit of mana, some mana, or a lot of mana. It would not be uncommon for a human player to ask this question as they choose to cast a magic spell in a game...

The morality meter example

The faction/morality meter example for this chapter covers a slightly different approach to implementing fuzzy logic via Unity. We build upon the implementation we covered in the basic fuzzy logic example.

In this example, we create a simple dialogue sequence, where the player is presented a series of scenarios, or questions, that they can then answer according to their morality. For simplicity's sake, we've included a "good," "neutral," and "evil" answer for each question. Let's take a look at the code to understand this a bit better.

The question and answer classes

The Question and Answer classes are very simple, and are used as data containers. Let...

Finding other uses for fuzzy logic

Fuzzy data is very peculiar and interesting in that it can be used in tandem with all of the major concepts we have introduced in this book. We saw how a series of fuzzy logic controllers can easily fit into a behavior tree structure, and it's not terribly difficult to imagine how it could be used with an FSM.

Merging with other concepts

Sensory systems also tend to make use of fuzzy logic. While seeing something can be a binary condition, in low-light or low-contrast environments, we can suddenly see how fuzzy the condition can become. You've probably experienced it at night: seeing an odd shape, dark in the distance, in the shadows, thinking "is that a cat?". It then...

Summary

I'm glad to see that you've made it to the end of the chapter. Fuzzy logic tends to become far less fuzzy once you understand the basic concepts. Being one of the more pure math concepts in the book, it can be a little daunting if you're not familiar with the lingo, but when presented in a familiar context, the mystery fades away, and you're left with a very powerful tool to use in your game.

We learned how fuzzy logic is used in the real world, and how it can help illustrate vague concepts in a way that binary systems cannot. We also learned how to implement our own fuzzy logic controllers using the concepts of member functions, degrees of membership, and fuzzy sets. In addition to this, we also played around with a faction/morality system to further illustrate the concept of fuzzy logic in the context of a choose-your-own-adventure-style interaction...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Unity 2017 Game AI Programming - Third Edition - Third Edition
Published in: Jan 2018 Publisher: Packt ISBN-13: 9781788477901
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}