ELI5! - FSM's and AI

There isn't really anything Unity specific about constructing an FSM, or C# depending on how comfortable you are writing with the language.

is the movement code stored in the current active state, for example, when I need to call the movement state, or is it held in a seperate 'movement' script and called accordingly? Is it all one long code file?

Every state would be in its own class. In general, classes should be kept as small as possible. Every class should only be responsible for one thing. This keeps code manageable, gives sense to their names, and allows isolation of behavior for easy maintainability and easy inheritance. You will not have to dig through dozens of lines of code to find most problems, because the behavior in error will be right in the class file name. Keep your functions as small as possible and use descriptive names for them as well for the same reason. You've definitely encountered what happens when you don't this in tutorials, which often have zero readability and arbitrary naming.

Delegating between states would be done by a controller class which encapsulates your states. Any object wanting to know anything about the state should have to ask this controller or its interface. In turn, the states should have to ask the controller/interface for any information they need from outside the machine. Any changes to how these communications should behave then only need to be changed in this class. Again, this is about isolating behaviors so you only need to go one place to change the rules.

If you find yourself passing or using similar combinations of information around, bundle them into a structure or their own class. For instance, if you're constantly passing the position vector, an integer, and two boolean values between the controller and the states, or between the controller and other objects, bundle them in a struct. You'll find that bundling like this makes everything less complicated when you're using them in many different functions.

These are mostly general programming suggestions, but I find that when you know your language and keep everything clean and simple like this, the underlying logic you are trying to implement is far easier to deal with even if it is a new concept. Nothing makes learning a new algorithm or whatever harder than trying to debug it when it is scattered about hundreds of LOC in one big class.

/r/Unity2D Thread