|Examples of state machines used in Unity||Post Reply|
|Feb 26 2018 Anchor|
Hello! I'm looking for some examples of state machines used in Unity to take care of game states. In fact, what I'd really like to find is somewhere I can get a complete game, even if I have to pay for the source code. Just a full game project that I can dissect to see how everything works together. I'm working on a game with a friend and we plan to use a state machine, but we aren't entirely certain we are going about it the right way. Thanks for your help.
|Mar 10 2018 Anchor|
There is nothing special about state machines in Unity. You can just look up any project in general and see how they did it. Not sure what exactly you're looking for here. You simply create an enum with different states you need and some kind of controller, which switches through them depending on your actions.
Just as an example. Whether your Controller is a MonoBehaviour on a GameObject in the scene or not is depending on your overall structure of the project.
Edited by: iQew
|Mar 10 2018 Anchor|
I made a small example for you: MEGA
I hope you will find it useful, have a nice day!
|Mar 11 2018 Anchor|
Actually any game in existence uses a state machine in one way or the other. State machine is a concept more than an implementation.
Using enums is one way to do a state machine (I call this "state variable based" state machine). Other possibilities are using classes based on a state interface (I call this "class based" state machine). Last but not least you can use state machine definition tools which work with some kind of state machine language (I call this "definition based" state machine). And of course you can mix and match this (I use both "class based" and "state variable based" mixed together for various kinds of state machines).
Basically you just want your scripts to store the current state (a variable containing an enumeration value or a pointer containing a class instance) and use code which alters behaviour depending on this state:
In pseudo-code this boils down to this here:
for state based state machines:
for class based state machines:
If you see the concept the rest is just doing it the way your engine of choice allows to.
Edited by: Dragonlord
Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.