Game Development Reference
In-Depth Information
Intro,
Player_Move,
Player_Attack,
Change_Control,
Enemy_Attack,
Battle_Result,
Battle_End
}
Note
It may seem strange to have a duplicate of your states in the state machine and in the
code; however, at the time of writing, it is necessary. Mecanim does not expose the names
of the states outside of the engine other than through using hashes. You can either use this
approach and make it dynamic, or extract the state hashes and store them in a dictionary
for use.
Mecanim makes the managing of state machines very simple under the hood, and it is ex-
tremely powerful, much better than trawling through code every time you want to update
the state machine.
Next, we need a location to cache the hashes the state machine needs and a property to
keep the current state so we don't constantly query the engine for a hash. So, add a new
using
statement to the beginning of the
BattleManager
class, as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Then, add the following variables to the top of the
BattleManager
class:
private Dictionary<int, BattleState> battleStateHash = new
Dictionary<int, BattleState>();
private BattleState currentBattleState;
Finally, we just need to interrogate the animator state machine we have created. So create
a new
GetAnimationStates
method in the
BattleManager
class as follows:
void GetAnimationStates()
{