Game Development Reference
In-Depth Information
State managers
Following on from the Animation tutorial in Chapter 2 , Building a Character , we have
seen that Unity has a very powerful state machine system built in it already using Mecan-
im. We have only used it for animation so far, but like AnimationCurves, we can use this
to build a nice graphical system that is easier to maintain.
Note
Although the state machine is very powerful for controlling what states are available and
how they transition between states, it can't actually implement actions (other than anima-
tion). There are triggers built into the state system, but these are not fully supported on all
platforms. So if you use them, keep it limited.
To achieve this properly, you need to separate out the responsibilities for what does what
within the state system into the following parameters:
Inputs : What factors will be fed into the state system to affect change
The decision engine : The core logic that drives the state machine
Outputs : What the game will do based on the current state
The previous diagram shows an example of how you would componentize your state ma-
chine; this pattern is very extensible because it means you can apply separate scripts for
each of the inputs, which also means many areas of the game can have an input to the
state system. The outputs/reactions to states or state changes can also be componentized
(but don't have to be) so that you can swap and change AI behaviors to the different states
based on what you are implementing them on. Enemy 1 may be very brave and just act,
and Enemy 2 might be a bit more cautious and require other enemies close by before at-
tacking.
Implementing this in Mecanim Animation controllers is very simple since at its heart it is
a state machine itself, as shown in the following screenshot:
Search WWH ::




Custom Search