GDD 011 : Beginner’s Guide To Enemy AI

By April 7, 2014 Podcast No Comments
Game Design Dojo Podcast Episode

Crash course in how to set up AI for your game, complete with pro tips from our resident AI programming expert, Ike.

GDC 2014 has come and gone, we give a little recap of our highlights of the conference. The most exciting part was after Brian’s speech at the Unity Booth fans from this podcast came up and talked to us. So just wanted to give a shout out to those that approached us, Thank you!

In other news, Brian’s company Fenix Fire will launch a Kickstarter campaign for “Source” which will be available for Xbox 1, PS4, and Steam Greenlight for PC. We want to be as transparent as possible so you can learn from this experience. We’ll be showing and discussing the approach, marketing, press, social media, numbers and much more. This is an exciting an unmarked territory challenge and we’re looking forward to sharing it with you.

Enemy AI

This podcast is a beginner’s guide on how to set up a basic enemy character and how to organize basic functionality. Ike uses his 15 years of experience and gives some black belt examples and ideas while Brian keeps it on a simpler level so wherever your skill level is this will be a helpful podcast for your game development.

3 Key Elements of Enemy AI

  1. Goals
  2. Movement
  3. Responding to player

Goals

First split up behavior between unconscious decisions and conscious decisions. Separate your actions, behaviors and animations into which category it’s going to be in and understand the unconscious decisions take priority over the conscious decisions.

A good exercise when starting out is to write down all the conscious and unconscious actions that you’re Enemy AI will do. Keep it at a high level.

Conscious Decisions

  • Patrol – trying to find the player, but not necessarily seeking them out
  • Idle – waiting for something to happen
  • Moving – to a specific location
  • Attack – aggressively of lightly

Give a sense of urgency  to each goal this allows you to swap in different animations later and when you have the character respond the same for starters it also gives flexibility later on. However, different states of urgency is more of an advanced feature, the next layer so to speak but it’s one that has to be developed early on because it’s hard to add later and it creates a more life like complex character for human behavior.

Unconscious Decisions:

  • Hit reaction – hit by bullet
  • Falling – fell off a ledge
  • Thrown on the ground
  • Death Sequence
  • The character doesn’t think about what’s happening to them, they’re a victim of the environment

Step by Step in Unity

Next, we go though a detailed explanation of diving in and creating an example using Unity.

Here are some definitions and explanations of terms we’ve used:

Pathfinding: Basically the study of how do you get from one location to the other when the direct path is unavailable.

Character Controller: Certain kind of entity that you can put on to an object and it all of a sudden assumes and absorbs a lot of the nice functionality for moving characters around.

Ray Cast: Imaging looking through binoculars or a telescope and you’re looking down a very pinpoint vision, a cone of vision but can see very very far and anything that is interrupting that can be sensed because you’re using a ray cast. Basically you pass in a starting positions and an ending position in 3 dimensional space and the first thing that it comes in contact with from the starting position you can get data on it.

Switch Case Functions: A little bit different than an if then statement in programmer talk. Brian gives a detailed example of how he uses this with his Enemy AI

Helpful Advice

We’re big believers in tracking and recording the state changes of your NPC. Create a change state function and have all the state changes go through there then put it out to your debug window to always have it around. Because one thing with AI is it’s really hard to reproduce the same situation over again sometimes and it’s a really good idea to have that paper trail with the game time since it gives you a little sense of what’s happening to evaluate it later. Again it’s really hard to recreate a fluke thing over and over again.

When you have multiple enemies on screen at the same time, you can put a debug widget that floats  above their heads. Color is a huge thing, can match the color with an action. Take a little time and put all that in so you can read what you’re enemy is doing because you’ll end up saving a ton of time in the long run.

Help us Help you

We only scratched the surface with Enemy AI, but for the future we’d like to hear from you about what you’d like to hear. Some examples are:

  • Pathfinding
  • Obstacle Avoidance
  • Separating Enemies Out
  • Flocking
  • Top Down Space Shooter

So, let us know what you think via Facebook and Twitter @GameDesignDojo and depending on the request we’ll either include it in the newsletter or make a podcast. You can sign up for our newsletter on our website. Thank you and have a great day!

Listen now to Game Design Dojo Episode #011

 

 

 

 

 

 

 

About Deanna McRae