- Home /
[AI] Best way to Code a State Machine for Enemy
I have done some google research in different ways to code state machines such as through: Inheritance, Coroutines, Delegates. I have made basic state machines before using a combo of enums and switch statements. But i wish to make mine tidy/clean and expandable(add more states easily).
I am wondering what the best way to code one would be for a Patrolling Enemy AI which will have the following states(or more): Idle, Patrol, Attack, Investigate(lost sight of target) and Dead.
Ps. Any state machine tips are welcome as well(would love some) :)
Thanks!
Edit: I have just found out about Behaviour Tree's, i dunno if they will be relevent but it seems they are overtaking state machines? Are they a possibility?
Answer by stuart6854 · Dec 29, 2014 at 09:53 AM
Thanks Thom and Bored Mormon for you help.
I think i will go with what i know best and use Inheritance and Classes(the way it was done in BoredMormon's link at the end) as that way seems more straight forward to me.
Hi! You should include the links in this answer, just to help others like me looking for the info you were searching for (I would do it by myself but I cant edit yet).
Answer by Thom Denick · Dec 28, 2014 at 11:04 PM
Behavior Trees are fairly complicated to code, but there are a ton of Behavior Tree solutions in the app store. Behavior Trees are better for complicated AI behavior. State Machines are just fine for an enemy that say Runs/Walks/Shoots, but if you want him to display different behaviors in different situations, Behavior Trees are better.
Here's a good Youtube video about creating a Finite State Machine in Unity: https://www.youtube.com/watch?v=lo80BOomaU4
Answer by winxalex · Aug 31, 2018 at 12:11 PM
There are several way how you do it. I you have no codding experience best way is to use visual finite state machines.
And when you learn the basics you can create Zombie AI with FSM. I'm codder and I'll advice you to skip all that hard codding Interfaces and stuff. In 21st you need to have everything visual which state you are, what is executed and what are the values. Switching isn't a problem. The main problem are transitions and how to break big Delagates/Events chains. Transitions should start at the end of frame of there will be glitch if transition is too long. For example you call delegate that to other state then that state call other state and chain is done. You can use Event but events in C# are multi-threaded . You can break chains with coroutines like wait end of frame or waittime. There is another problem and that is how you will sync everything with animations a.k.a Mecanim and you are split in two. I've been able to tweak Mecanim and use as ordinery FSM by use of triggers. About behaviours, behaviours doesn't take over from statemachines but they work together. Like you are in some state where some behaviour tree is executed....
Your answer
Follow this Question
Related Questions
Problem with replays on the state machine's transitions. 0 Answers
State machine callbacks not being called on mobile iOS? 1 Answer
DIsabling a Box Collider Trigger after cut scene 2 Answers
What is the proper way to implement a 180 turn while running (Plant and turn) animation? 1 Answer
Managing GUI.depth for drawing lines in EditorWindow 1 Answer