Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Unity AI Programming Essentials

You're reading from  Unity AI Programming Essentials

Product type Book
Published in Dec 2014
Publisher
ISBN-13 9781783553556
Pages 162 pages
Edition 1st Edition
Languages
Author (1):
Curtis Bennett Curtis Bennett
Profile icon Curtis Bennett

Chapter 7. Adaptation

Having good AI for our characters is more than just giving them simple tasks to perform; we'd like to have our characters realistically react to the game environment. Game events such as seeing new objects appear or having a bomb go off in a scene should cause a reaction in the AI. Having the AI adapt to the environment is a huge topic, but we will focus on the basic ways to have AI adapt to the environment. In this chapter, we will look at taking AI skills we learned in previous chapters and combining them to create AI characters that adapt to the game environment in a realistic way, changing their tasks based on game events.

In this chapter, you will be:

  • Creating AI characters that react and adapt to multiple game events

  • Setting up more complex AI characters in RAIN

  • Getting to know the importance of creating larger AI scenes with REACT AI

An overview


In previous chapters, we looked at how to do different specific AI tasks. We learned how to make characters patrol a path, have them wander an environment, change state with behavior trees, and sense objects in the game environment. These are all important, but it's more important to understand how we can combine these different elements to make AI that works well in a large game environment. We will need characters that can navigate an environment to perform tasks but then change based on game events that occur. To do this, the game needs to be designed at a high level, defining what the different AI character's main goals and actions are. These high-level goals are things such as wanting an enemy to patrol an area until it sees the player and then start to chase and attack him. From there, the different aspects of sensing need to be designed for the level, deciding what objects need to be tagged, so they can be used by the AI system. The characters then need sensors defined...

RAIN's demo


The basic start of the demo will be similar to our others, a ground with several walls around for our ships to travel. This is how the basic starting point of our demo should look:

The basic starting point of our demo

One of the first things we will need is the ability to query a random location in the scene to spawn and find points to travel to. Create a class called Ground and add it to the ground plane. This class will be used to provide higher-level information about the level, the first of which is being able to find a random position in the level. Here is the Ground class with the random position chooser method:

In the preceding code, we are able to ask for a random position at anytime from anywhere in the game. In the Start method for the Ground class, we store the max and min positions for it, and as we don't want positions on the very edge of the level, it is scaled to 90 percent by multiplying by 0.9f. The min and max positions are static, so we can add a static method...

The React AI


We have been using RAIN for our adaption so far, but there is no reason you cannot create a demo like the one we just did with React. The basic behavior tree and node logic can stay the same. The main difference is that React doesn't use a built-in sensor system; instead, users define sensing based on what they think is the best. This can be done through Unity's built-in ray casting methods to query the scene. The following is a method adapted from React's sample that can be used with React to determine the visibility of a target. This code takes in a target and first does a simple test to see whether the target is within the field of view by finding the vector of the target from the player and comparing the angle of it and the forward direction of the AI character.

This is a simple and quick test that does a basic check, in terms of collision detection, and this is called the broad phase. Then, the Unity physics system is used to ray cast from the character to the target; this...

Summary


In this chapter, we looked at how we can make our AI adapt to events in the game. This was done using methods we learned in the previous chapters, and we also took a look at RAIN's motor system to allow our adaptions to be more customizable. Our demos in this chapter have been pretty straightforward, but there is no reason why this demo couldn't be extended to have more events to send and more reactions defined in the character behavior trees. However, our demos have been missing one important thing, which is yet to be discussed: the player. In the next chapter, we will discuss how AI characters attack by adding a player to our scene and having our characters react and attack. We will discuss how to create enemies for the player and have them attack the player.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Unity AI Programming Essentials
Published in: Dec 2014 Publisher: ISBN-13: 9781783553556
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}