Daily progress: Optimizing our State machine with switch statements.

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:



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store