How To Make An Enemy AI

I have been wondering this for a while and now the time has come that I need one. Currently I am trying to make a 3D game with turn based strategy like “Fire Emblem”. I would like some help getting started. The primary parts that I need is for the enemy to know how to get the optimal amount of damage on the player (attack are calculated like this: [SOLVED]Detecting what side of s box collider is hit through ray casts - Unity Engine - Unity Discussions), when to pursue a character/retreat from character, field of view only in front of the enemy (maybe raycasts), and finally being able to coordinate with other enemy’s. I assume a lot of the eventing can be done with an Enum, but a good starting idea for the structure would be very helpful.

Probably there are more books about this but here are same links

I did not read them but htis kind of books are usually a grate tool.
Also try in YouTube channel AI tutorial series

This is a pretty deep topic. Alan is right, some books might be a good investment.

Like just about anything else with programming, I advise you to take this big hairy problem and break it up into a series of smaller, more manageable problems. Start with a dumb AI that always attacks directly in front, for example, whether there’s anything there to attack or not. Then add sensing (raycasts or whatever to tell what’s within view). Then add some code to calculate what attack to use based on the target. Then add more code to decide to retreat under certain circumstances.

You’ll be using an ad-hoc approach to AI which we call “heuristics” when we want to dress it up and make it sound fancy. :slight_smile: But it just means, hand-coding some logic that makes sense. There are a whole lot of more sophisticated ways to approach agent AI, but I think simple heuristics are a fine way to begin (and in many cases, good enough).

I see that you are using raycasting and such to get data from your game. I would advice to avoid that and make a model of your game first, so that graphics and logics are separated. The model would be an entity storing all the data, the rules of the game, possible moves… etc which would make it easier to query data. Then the A.I. could use this model to obtain information in an efficient way to make decision.

As a starting point, a well known algorithm for turn based games is MiniMax, which basically models two rational players making decisions where each player is trying to maximizing its gain while minimizing the gain of the opponent.

This is what I have so far. Constructive criticism would be helpful.

using UnityEngine;
using System.Collections;

public class EnemyMainCombatAI : MonoBehaviour {

    public bool thisEnemysTurn = false;

    public enum StratType{
        DEFENSIVE,//neutral untill within eyesight
        OFFENSIVE,//Agressive attack
        STATIONARY,//Stays in one spot (sniper tower)
        ROUTE//Follows along a path of waypoints
    }
    public StratType stratType;

    private enum PrimaryActions{
        PURSUE,//IF deffensive the only if character is in rage // If offensive then only if Enemy has advantage over character//IF whithin sound range of route
        RETREAT,//IF deffensive health is low // IF offensive is at dissadvantage//NEVER Route
       
        GROUPUP,//all enemys within sound range (2x sight range) // NEVER ROUTE

        FINDPATH//Route only
    }
    private PrimaryActions primaryActions;

    private enum SecondaryActions{
        ATTACK,//Deffensive always attacks at advantage and will always attack if outnumbered // offensive always attacks at advantage retreat if outnumbered
        ATTACKRANGE,//STATIONARY ONLY
        HEAL, //heals enemy
        WAIT //Skips secondary action for this turn
    }
    private SecondaryActions secondaryActions;

    private enum AIInteraction{
        UNAWARE,//Sleeping, knocked out, etc
        NEUTRAL,//Normal
        SUSPICIOUS,//Aware of enemy in sound range
        CONTROLLED//Being controlled by player to attack other enemys(makes enemy vulnerable to being attacked or crit)
    }
   
    // Update is called once per frame
    void Update () {
        if (thisEnemysTurn == true) {
            TurnActions();
        }
    }
    void TurnActions(){
        if (stratType == StratType.DEFENSIVE) {
            //do stuff
        }else if(stratType == StratType.OFFENSIVE){
            //do stuff
        }else if(stratType == StratType.STATIONARY){
            //do stuff
        }else if(stratType == StratType.ROUTE){
            //do stuff
        }
    }
}

This looks well structured.

What you are doing is a Finite State Machine (FSM), it’s a good tool to learn about since it is often used in video games. Though it can be hard to work with if you want to do complex stuffs or if you need to modify/prototype your logics often.

An excellent alternative to FSM is Behaviour Tree, which is more flexible and extensible, and particularly suited to make AIs.

I’ve made a tool based on Behaviour Tree available here:
http://www.pandabehaviour.com/

If you are interested to learn more about BT, here is a selection of online resources:
http://www.pandabehaviour.com/?page_id=48

And an excellent alternative to a Behavior Tree is a utility system, as explained here.

There are a couple of good-looking assets for this in the asset store, like this and this. Or, it’s not too hard to implement yourself, which is what I’ve done.