State machines can be a very useful thing to look into when you want to create a game. They can be used for many different things inside your game that will make it a lot easier for you to create, use and edit in the future.
State machines can be used for different things within your game. I will be talking about 2 ways you can use a state machine within a game. Both are similar in the way that they work but are used in different ways to achieve slightly different outcomes.
First off I will be talking about using state machines with animations. Many games that are released these days are normally filled with animations. These animations can be used to make a main character more lively, NPC’s in your game look more believable or even make collectibles in your game more desirable to players. There are few games out there that are made without using animations that do well. The animation aspect of many games is what a lot of players enjoy. If a game has fluent and fascinating animations the player is sure to be more attracted to your game. On the other hand if your game does not contain animations you will need to have some pretty solid mechanics to keep the player coming back.
Using a state machine to keep track of all your animations is very effective and simple once you get the hang of it. The Animation State machine is used to keep track of your animations. It helps you link your different animations together and and even gives you a visual to help you better link different animations together. It can also be use to restrict the access of some animations in case you only want a certain animation to play after another animation has been used. An example of this is if your character has to maybe charge up an attack or a jump before it is used, you can make a condition by connecting certain animations up the way you wish them to play out. If you would like to take a further look at the Animation State Machine then the Official Unity Manual has some interesting things you can look at. There is also an old Video from Unity that helps explains how to use them a little better.
After you have studied and create a couple of games for yourself, you begin to realize that there is sometimes a common pattern or a set way that your game plays out. For example, you could start with a Menu that can take you to the Game Screen and then to maybe a Credits Screen when the player completes the game. Using a State machine allows you to gather these events and order them as you would like then only allowing them to be accessed once they are activated by the player. It is sort of like a big IF statement but a lot more organised, allowing you to move in and out of certain “modes” easily. Within different modes/states you are able to run different parts of a script that will allow you to take actions that are specific to that state/mode. If you would like to look into this method of State Machines then please check out this Video that I Found useful during my research. It is a very simple yet easy to follow guide on the basics of State Machines within your game.
After looking into State Machines, I have realized that they would have come in handy in my most recent project EGONE. EGONE was a group project that would have benefited well from a state machine when considering I was the only programmer on a team with 2 other designers. Having a state machine would have allowed for easy editing and made our code a lot simpler/cleaner. One major spot that we could have had a state machine would have been inside our individual levels for when the player begun to play the game. It would have 3 different states: Playing, Death and Win states. While the state machine was in the “Playing” state it would allow the player to move freely throughout the level. The Death sate would have screen appear that showed the player had Died and prompt the player to restart or go to the main menu. Finally the Win state would be largely the same as the Death state but it would raise the Win screen and prompt the player to start the next level. This style of state machine would allow for easy level navigation between all levels making it a lot easier on the designers when it comes to putting together a level.