Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PrimeDerektive · Feb 01, 2011 at 09:46 PM · aicoroutinelogicfinite-state-machine

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PrimeDerektive · Feb 02, 2011 at 02:28 AM 0
Share

Jessy, see my updated code for a little more insight into my intentions.

avatar image Jessy · Feb 02, 2011 at 07:31 AM 0
Share

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?

avatar image PrimeDerektive · Feb 02, 2011 at 12:59 PM 0
Share

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.

avatar image Jessy · Feb 02, 2011 at 01:43 PM 0
Share

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).

avatar image PrimeDerektive · Feb 02, 2011 at 02:50 PM 0
Share

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 :(

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

[Unity Multiplayer Networking] About NPCs/AIs logic (movement, interaction,..) in multiplayer networking 0 Answers

Getting my object to rotate and move at random 1 Answer

Update SetDestination only every 0.2s with coroutines ? 5 Answers

AI script making unity no longer respond. 1 Answer

AI Coroutine or ScriptableObject ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges