State machines aren’t optimal when used with if statements. To get the best performance, you’ll want to be using them with switch statements.
So in Update, we’ll lay down the foundations of our Switch statement:
and for each case, we’ll use the different states:
For our Attack state, we should modularize our code a little more and put the attack code in its own method:
And now call it under the Attack case.
And as you can guess, CalculateMovement goes under Chase:
And now we are left with much cleaner, more readable code!
Here’s the updated EnemyAI script: