Different ways of dealing with states

In the Third Person Combat and Traversal course, Nathan implemented this without any interfaces. He used abstract classes and methods that had to be implemented in each state. That way each state had 3 mandatory methods: Enter, Tick & Exit.

But, in Sam’s implementation, every state implementing LocomotionState has to have the 4 functions: Jump, Fall, Land & Crouch. I got the point why that’s being done. But what happens when we want to add other states? Like Attacking or Dodging? Do we add them to the list of methods in LocomotionState? Or do we add another interface for this?

And also, where can I learn more about this?

Thank you

Sam’s implementation is just a quick sample, and to be honest, I would hesitate to build a State Machine in this manner, precisely because of the lock in of these methods…

I strongly recommend Nathan’s implementation of the StateMachine. It’s generally the way these things are done. Probably the only difference I would have is that in Nathan’s course, all of the references are bound to the State Machine and are assigned in the inspector, while I tend towards a [RequireComponent(typeof(WhatClassIWant))] along with automatically assigning these properties when they exist in the OnValidate() method of the StateMachine. Nathan’s overall state machine structure is excellent.

1 Like

I haven’t done the Third Person Combat and Traversal course yet, so I can’t really speak for the state machine used, although from what I’ve seen it looks familiar.

I have 2 types of state machines I tend to use often: one with transitions that I got from Jason Weimann and (very) slightly adapted to suit my own style (see this Unity Bots with State Machines - Extensible State Machine / FSM - YouTube) and a stack based one that I use a little more often because it’s quick to set up. I am personally not the biggest fan of switch/enum type state machines, so I changed the states we implemented in the turn-based strategy course to use the stack based fsm in my own implementation.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms