This is a project I worked on for a class where we learned about AI. This was part of a group project the whole class had to work on. The task of this project was to create 2 puzzles the AI can solve on its own. These puzzles we made involved the AI looking for a key to open a door and looking for coins to be able to cross a bridge.
In this project, I worked on programming the states for the AI. I did help program on all the states of the AI. In our project we made 5 states for the AI. Here is a run down of what the AI does. The state the AI starts is called FindDoorState. In this state the AI will head towards whatever is set as the door. Upon arriving at it it will check if it has a key for it. If it does not then it will switch to a state called FindKeyState and will try to find a key in the scene and pick it up. Upon picking up the key it will go back to the FindDoorState. Since it now has a key, upon arriving at the door, the door will turn off and the AI will switch to a state called FindBridgeState. In this state it will heard towards whatever is set as the bridge. Once it reaches the bridge it will check if it has enough coins for it. If it does not then it will switch to a state called FindCoinState and will try to find set number of coins in the scene and pick them up. Once it picks up the required amount of coins it will switch its state back to FindBridgeState. A barrier blocking the barrier will disapear once the AI has collected the required amount of coins. Since it now has the required amount of coins, once the AI reaches the bridge it will switch to a final state called FindWinAreaState. In this state it will simply head towards whatever area is marked as the win area. Once it reaches this area, text will up saying something along the lines of, "Yay you win."
Here is the code for two of the states and a script for the object that interacts with one of the states. The states have a interface script placed in them. Due to this they don't have to be placed on an game object and instead can be placed inside a script. The states were made with the help of a frame work someone from our team provided for switching between states and being able to call methods from the interface inside each state with a state machine.