- Home /
Basic AI finite state machine: where to put decision logic?
I'm trying to convert my enemy AI to an FSM, and am having trouble wrapping my head around where in my script to put the actual decision logic. I built this basic template:
UPDATE: I updated the code to illustrate a solution I came up with this morning, works pretty well so far. Feel free to crib it.
var target : Transform;
//wandering vars public var wanderSpeed = 2.0; public var wanderRotSpeed = 5.0; public var wanderRadius = 10.0; public var wanderRayDistance = 5.0; public var wanderPauseMin = 2.0; public var wanderPauseMax = 6.0; private var basePosition : Vector3; private var currentDestination : Vector3;
//chase vars var chaseDistance : float = 10.0; var chaseSpeed : float = 3.0; var chaseRotSpeed : float = 5.0;
//attack vars var attackDistance : float = 3.0; var attackRate : float = 0.25;
//state setup enum aiState{ wandering, chasing, attacking } var state : aiState;
InvokeRepeating("StateLogic", 0.0, 0.05);
function Start(){ if(target == null) target = GameObject.FindWithTag("Player").transform; ChooseNextDestination(); yield StateMachine(); }
function StateLogic(){ var distanceToTarget = (target.position - transform.position).sqrMagnitude; if(distanceToTarget <= attackDistance*attackDistance) state = aiState.attacking; else if(distanceToTarget <= chaseDistance*chaseDistance) state = aiState.chasing; else state = aiState.wandering; }
function StateMachine(){ while(true){ switch(state){ case aiState.wandering: yield Wander(); break; case aiState.chasing: Chase(); break; case aiState.attacking: yield Attack(); break; } yield; } }
function Wander(){ RotateToward(currentDestination, wanderRotSpeed); MoveForward(wanderSpeed); //BroadcastMessage("PlayAnimation", "walk"); var destPosZeroY = currentDestination; var currentPosZeroY = transform.position; destPosZeroY.y = 0; currentPosZeroY.y = 0; if((destPosZeroY - currentPosZeroY).magnitude < 1.0){ yield WaitForSeconds(Random.Range(wanderPauseMin, wanderPauseMax)); ChooseNextDestination(); } }
function ChooseNextDestination(){ var randOffset : Vector2 = Random.insideUnitCircle * wanderRadius; currentDestination = basePosition + new Vector3(randOffset.x, transform.position.y, randOffset.y); Debug.DrawLine(transform.position, currentDestination, Color.white); }
function Chase(){ RotateToward(target.position, chaseRotSpeed); MoveForward(chaseSpeed); }
function Attack(){ target.GetComponent(PlayerStatus).TakeDamage(20.0); yield WaitForSeconds(attackRate); }
function RotateToward(targetPos : Vector3, rotSpeed : float){ targetPos.y = transform.position.y; var rotation = Quaternion.LookRotation(targetPos - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotSpeed);
}
function MoveForward(moveSpeed : float){ transform.Translate(Vector3.forward*Time.deltaTime*moveSpeed); }
But I don't know where to put my actual decision-making logic. Should I just call MakeDecision once in Start, and do a check inside each of the coroutines for my states, making a quick decision to see if the condition that I required to be satisfied for this state is still satisfied, and if not, return?
Is there a way I could separate the decision making logic from the coroutines for each state, so I don't have to pepper every function with checks to make sure whatever condition got me there is still satisfied?
Answer by Jessy · Feb 01, 2011 at 10:52 PM
I'm actually not 100% clear on your problem. However, I'm going to offer a suggestion, that hopefully will help you. Unity 3 added function types in JavaScript. That means you can have nice, compact code that switches functions, and you can run the appropriate functions in whatever coroutine you want. Here, I'll make the decision every frame, to decide what to do:
enum AIState {Wandering, Chasing, Attacking} var aiState : AIState;
private var act : function ();
function Update () { MakeDecision(); act(); }
function MakeDecision () { switch (aiState) { case AIState.Wandering : act = Wander; break; case AIState.Chasing : act = Chase; break; case AIState.Attacking : act = Attack; break; } }
The standard for the enum list is to use PascalCase, not camelCase, by the way. If you use these "function types", aka delegates, you may not need to have the enum in the first place, though. I'd like to hear more about whatever coroutines you're using.
The docs don't mention if you can declare an array of functions in JavaScript, but I doubt it; I can't figure a method for it, anyway. Here's how you might do it in C#:
enum AIState {Wandering, Chasing, Attacking} [SerializeField] AIState aiState;
delegate void Action (); Action[] actions;
void Awake () { actions = new Action[]{Wander, Chase, Attack}; }
void Update () { actions[(int) aiState](); }
I think this way might be faster, but I'm not sure. I think it's better because it's slightly more clear to me, though:
Dictionary<AIState, Action> actions;
void Awake () { actions = new Dictionary<AIState, Action>() { {AIState.Wandering, Wander}, {AIState.Chasing, Chase}, {AIState.Attacking, Attack}, }; }
void Update () { actions[aiState](); }
I highly recommend switching to C# at this point, given your apparent level of scripting knowledge, and desires.
Jessy, see my updated code for a little more insight into my intentions.
I don't see any reason to have those functions in their own coroutines, if you're running them every frame. Without seeing more code, I'd just tell you to take out the enum altogether, and $$anonymous$$akeDecision(), and just run the appropriate function based on the distance check. I would not check distance, though. You should compare the squares ins$$anonymous$$d, which is way faster. Is there some other FS$$anonymous$$ that you're trying to emulate?
No, not really. $$anonymous$$y goal was basically to take create an AI very similar to the robot AI in the FPS tutorial assets off Unity3d.com, but convert it from a procedurally coded, sloppy mess into a cleaner looking state switch, while using Update() as little as possible. I guess it was more of a challenge to myself than anything.
I cannot help you along this road. Update() is amazing. I love it because every script's code can be joyfully asynchronous with every other, and you can listen to events, and respond by "enabling" or "disabling" the script. Excellent for pausing. If you need something to happen every frame, and you don't use Update, I can't support you. I tried using custom coroutines myself, for months, and co$$anonymous$$g back to Update was like losing an obese demon hanging on my back. You just need to try it out with the event model (not simple in JavaScript, so I understand why you'd do what you did).
Hahaha I know, right? I blame Eric Haines for instilling the fear of Update() in me. I guess I'll just use Update() and write some Time.deltaTime wait functions. As for switching to C#, I just don't have it in me I don't think. It took me a year and a half to get as good with UnityScript as I am (and I'm not clai$$anonymous$$g that is too good, either!), and I had very little oop program$$anonymous$$g experience before Unity was some basic $$anonymous$$VC integration with PHP. If the documentation was in C#, maybe I'd do it. I would love to, but I just don't have the free time to devote to it otherwise :(
Answer by Bunny83 · Feb 01, 2011 at 10:36 PM
If your statemachine is selfcontrolled you want to call MakeDecision either in Update() every frame, or in a coroutine loop. But if the transitions should be triggered from outdide you have to call it when a decision is required and that's up to you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613102324im_/https://answers.unity.com/themes/thub/images/avi.jpg)