Reader small image

You're reading from  Unity AI Programming Essentials

Product typeBook
Published inDec 2014
Publisher
ISBN-139781783553556
Edition1st Edition
Tools
Right arrow
Author (1)
Curtis Bennett
Curtis Bennett
author image
Curtis Bennett

Curtis Bennett has been a developer in the games and computer graphics industry for several years. He has worked on developing immersive virtual environments, published research in visual simulation, taught college courses in game development, and worked for various game studios, and he was also an engineer on early versions of the RAIN AI plugin for Unity. Currently, he is the Technical Director for Creative Services at Ideum, which focuses on creating interactive media projects.
Read more about Curtis Bennett

Right arrow

Chapter 8. Attacking

Fighting is an important part of a game's AI. For many games, fighting with the player is the main game mechanic and the most noticeable AI in the game. We will discuss the common methods for attack AI, how to make an enemy character chase and attack the player, and then have the enemy character take cover and hide from the player.

In this chapter, you will learn about:

  • Designing attack AI in RAIN 2.1.4

  • Creating basic chase attack AI

  • Creating and covering attack AI

  • Having AI attack in groups

An overview of attack AI


Attack AI is a large and much studied subject. When you start dealing with things such as different attack moves based on different player actions or having enemies coordinate attacks, the AI can become quite complex. However, designing good AI that attacks is the same as designing for other AI scenarios we have looked at so far in this book. First, we need sensors for our AI characters to perceive game events and to create aspects in the game world, tagging what they can sense. Then, we define behavior trees for the characters, directing them to change actions based on sensor response or other game states, such as running out of ammo. Defining different behaviors is the main part of setting up attack AI.

We'll look at two foundational AI attack behaviors in our demos in this chapter. The first will use multiple sensors on the AI to determine when to chase and when to stop and attack. The second behavior we will look at is the duck and cover type, where the enemy...

The attack demo


Like our previous demos, we will start with a basic scene with a ground and walls. The demos here will involve an enemy ship attacking a player, so add a ship to the scene, name it player, and add simple controls to move the ship around. Also, tint the color of the material to make the player ship stand out from the enemy ship that we'll add in a moment. Of course, the player ship isn't an AI, so it doesn't need a RAIN AIRig, but it does need to have a RAIN Entity component. With the player selected, go to RAIN | Create Entity. Next, it needs a visual aspect for the AI enemies to see it; from the Add Aspect dropdown, select Visual Aspect and rename the aspect to player. This provides a base for our attack demo. This is how the RAIN attack demos will look with a player ship:

Next, we need an enemy for the attack. The enemies will also be ship models, and as we are focusing on just the AI, we won't worry about the actual game mechanics of attacking, such as having the ship fire...

The chase and attack demo


In the first demo, we will build an enemy ship that senses for the player, and if it sees the player, it starts moving toward it and then attacks it. A simple version of this would be to have the enemy wander with a visual sensor to detect the player, and if it sees the player, the enemy will move toward it and attack it. This would work but it really wouldn't be any different from the demo from Chapter 7, Adaptation, where the ship had to search for and collect gold. To make it a little different, we'll use a two-sensor approach. We will have one larger sensor on the enemy that detects the player, and if the enemy senses the player aspect, it will start chasing the player. Then, there is a second smaller sensor that attacks the player, that is, if it senses the player, then the enemy stops chasing and it instead attacks. This gives the effect of chasing the player but when the enemy gets closer, it stops and starts attacking, instead of just chasing and attacking...

Creating cover AI


Our AI enemy will just keep attacking the player as long as it is close enough to the ship. However, this isn't very realistic; we'd like the enemy ship to attack for a little bit but then duck and head for cover. We could have this hiding behavior be based on a response to the player fighting back, but for this demo, we will make it a constant value of 5 seconds; after attacking the player for 5 seconds, it will hide.

To set this up, first we'll add an isHidingbool variable to our behavior tree that is set to true after 5 seconds of attacking. Create a new constraint node under the root parallel node with the playerAttack != null && isHiding == false expression. This node's children start when playerAttack is valid and we are not already hiding from the player. Add a sequencer node under this constraint so it will go through all of its children. The first child needs to be a new timer node with the Seconds value of 5 and Returns set to Success. Next, copy the don...

Group attacks


We spent Chapter 4, Crowd Chaos, and Chapter 5, Crowd Control, looking at group behaviors, and we won't go through a full demo of attacking in groups here, but we should discuss a few main points. With the demo in this chapter, we can add more ships and they will attack in a fairly believable manner. However, there are ways to make it better by considering other enemy positions.

When the enemy ships choose a cover position, a simple method for a group is to track each position if an enemy is already there. Then, when selecting a cover position, each enemy won't go to one that is occupied, making the enemies more diverse in their attacks.

Similarly, when attacking the player, instead of just going as close as possible, the attack pattern can be coordinated. Instead of just going directly to the player, a set of points can be defined radially around the player, so enemies surround and attack it. The key to these group behaviors is enemies taking into account the behavior of other...

Summary


In this chapter, we looked at attack AI, focusing on how to have enemies chase and attack a player and then how to evade. These are basic attack behaviors and can be extended to more complex and game-specific behaviors, and we discussed how to do this when creating groups of enemies.

In the next chapter, we will look at another special AI case, which is driving and cars. However, instead of using a general-purpose AI system such as RAIN or React AI, we will use an AI plugin specifically designed for cars that takes into account physics to create realistic driving.

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

Author (1)

author image
Curtis Bennett

Curtis Bennett has been a developer in the games and computer graphics industry for several years. He has worked on developing immersive virtual environments, published research in visual simulation, taught college courses in game development, and worked for various game studios, and he was also an engineer on early versions of the RAIN AI plugin for Unity. Currently, he is the Technical Director for Creative Services at Ideum, which focuses on creating interactive media projects.
Read more about Curtis Bennett