Bad Thoughts - Dev Log #2 - State Machines and Visual Debugging

Implemented a finite state machine to handle the AI behaviors. Even though there won’t be too many states, having a bunch of if/else statements will get unwieldy pretty quick. I’m happy with how it turned out. Now all that’s needed to add a new state is to create a class for the state that inherits from BaseState and add one extra line to BadThoughtStateMachine.

I almost added state changes within the states themselves, for example, when RunningAway is finished it could auto change back to the Following state. Instead, I opted to keep all state changes in the BadThought script, which is also where all the Bad Thoughts' settings are located. That way if I need to find out why it’s not reaching a certain state, I don’t have to dig through various state scripts and only need to look in one place.


So far I’ve been diligent with adding visual debugging to everything I’m implementing. It adds a bit of time, but I’ve worked on projects in the past that didn’t do this and it can make things more complicated than they need to be further down the road.

I’ve been using ALINE from the Unity Asset Store to create the visual debugging shapes. It’s much easier to use rather than Unity’s built-in Debug.Draw and Gizmo drawing functionality. Here’s an example of the arrow beneath the cube and the text to show which state it’s in:

Greg Hendrix @greghendrix