Game Development Reference
In-Depth Information
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'schildrenstartwhen
playerAttack
isvalidandwearenotalreadyhidingfrom
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't attack
node and add it below the
timer so that the enemy won't attack as it's running to hide.
Then, add another
expression
node to set
isHiding
to true; its expression value
should be
isHiding = true
. The behavior tree should be like the following screenshot:
Finally, we need to have hiding spots to go to. These are often predefined; in shooting
games, hiding spots are defined based on paths the player is expected to take. To
do this, create a few navigation targets by going to
RAIN
|
Create Navigation Target
and add them to some good cover spots for the enemy. Here's how they can be ar-
ranged: