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

You're reading from  Practical Game AI Programming

Product type Book
Published in Jun 2017
Publisher Packt
ISBN-13 9781787122819
Pages 348 pages
Edition 1st Edition
Languages
Author (1):
Micael DaGraça Micael DaGraça
Profile icon Micael DaGraça

Defining the states


Let's define what triggers each state and how the AI should choose the correct state in different scenarios. The PASSIVE state will be the default state, and the game will start in that position until the player comes across our character. The DEFENSIVE state will be used in two different situations if the player comes from the right side and if he has already confronted the player and has low HP. Finally, the AGGRESSIVE state will be activated if the player comes from the left side or has already arrived at the middle area:

public class Enemy : MonoBehaviour {
private int Health = 100;
private bool statePassive; 
private bool stateAggressive; 
private bool stateDefensive; 
private bool triggerL; 
private bool triggerR; 
private bool triggerM;
// Use this for initialisation 
void Start () { 
 statePassive = true; 
}
// Update is called once per frame 
void Update () {
 // The AI will remain passive until an interaction with the player occurs 
 if(Health == 100 &&...
lock icon The rest of the chapter is locked
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}