Game Development Reference
In-Depth Information
Getting to the state manager in the code
Now that we have our state manager running in Mecanim, we just need to be able to ac-
cess it from the code. However, at first glance, there is a barrier to achieving this. The
reason being that the Mecanim system uses hashes (integer ID keys for objects) not
strings to identify states within its engine (still not clear why, but for performance reasons
probably). To access the states in Mecanim, Unity provides a hashing algorithm to help
you, which is fine for one-off checks but a bit of an overhead when you need per-frame
access.
A simple solution to this is to generate and cache all the state hashes when we start and
then use the cache to talk to the Mecanim engine.
First, let's remove the placeholder code from Chapter 7 , Encountering Enemies and Run-
ning Away , for the old enum state machine, so remove the following code from the top of
the BattleManager script:
enum BattlePhase
{
PlayerAttack,
EnemyAttack
}
private BattlePhase phase;
Also, remove the following line from the Start method:
phase = BattlePhase.PlayerAttack;
There is still a reference in the OnGUI method, but we will replace that shortly; feel free
to remove it as well now if you wish.
Now, to begin working with our new state machine, we need a replica of the available
states we have defined in our Mecanim state machine. For this, we just need an enumera-
tion using the same names (you can create this either as a new C# script or simply place it
in the BattleManager class), as follows:
public enum BattleState
{
Begin_Battle,
Search WWH ::




Custom Search