Lance Harrington - Milestone One Part One: Enemy AI


                  Lance here with my first dev-log update. This update I will be talking about my first milestone 1 goal and where I’m at with it. There were two main goals that I wanted to accomplish with this milestone, with the first being setting up some basic enemy AI. The Second milestone goal was to put in an audio system that anyone can use to add audio to their own developments. With past experience and references, I went into this milestone with confidence.

                 For the enemy AI, I went with a state machine, just like our player. The StateMachine.cs script inherits from Monobehavior and is an abstract class with an Update method and a SwitchState method. There is another class called State that is a generic C# abstract class and has three main methods being Enter, Tick, and Exit. StateMachine has one attribute being a State named currenctState. The currentState calls its Tick method in the state machines update method, essentially updating the state ever frame. The child of the state machine can call the SwitchState method to change states. The SwithchState method then calls three lines of code in this order where our next state called newState is passed in as a parameter:

  1. currentState?.Exit();
  2. currentState = newState;
  3. cruuentState?.Enter();

This will check if currentState is null and then call its Exit method. Next, it will change our currentState to the newState. Finally, it will check if the currentstate is not null and call the new states Enter method. As long as I have access to the state machine, I can call SwitchState and simple pass in a new instance of any state I make. Below is a UML diagram to better show the layout of the state machine:


EnemyStateMachine UML

A UML Class diagram of a state machine for an enemy


                The child class EnemyStateMachine is where everything involving the enemy is stored for access, including various stats and components. I currently have it so that when the player gets in range of the enemy, they switch from EnemyIdleState to EnemyChaceState. In EnemyChaceState the enemy moves toward the player using a navmesh. If the enemy gets withing attackrange of the player, they will switch to EnemyAttackState and perform an attack animation. Once the animation completes, the state will switch back to EnemyChaceState, rinse and repeat.

                The EnemyStateMachine that was built for this can seem very complex, but when broken down into little parts, it becomes easier to understand. When set up in this way, it allows for all the logic to be done within each state and allows for you to switch between states with ease. As for checking this off my milestone of adding basic enemy AI, I would say that I have done that with this state machine and will be able to expand upon it in the future by creating new states as they become needed. This will be further done on milestone three which I will be adding more complexity to the AI. Next dev-log post will be on my second goal for milestone 1, adding an audio system. Be sure to check it out and thank you for reading!

Get Iron Echos: Zenith's Uprising

Leave a comment

Log in with itch.io to leave a comment.