Game Development Reference
In-Depth Information
Creating the Seek-Health (or flee) state
The
Seek-Health
state occurs when the enemy runs low on health and can restore it
by collecting a medikit. This state is unlike most others, in that, it can be reached or
entered from any state. The entering of this state doesn't depend on its relationship
to others, but only on the player's health. Specifically, this state should be entered
when the enemy's health is reduced beyond a minimum floor. As a result of this
configuration, be sure to hook up the
Seek-Health
animation state in the Mecanim
graph to the
Any State
node that allows the run animation to be triggered in any
state, as shown here:
The Seek-Health state can be accessed from Any State
Each enemy maintains a
if(Health
variable; this variable is adjusted either up or down,
depending on whether the enemy finds a medikit or is attacked. The change occurs
inside the method
ChangeHealth
, and this is where we can determine whether a
SeekHealth
state must be initiated. The
ChangeHealth
function is public; it allows
SendMessage
and
BroadcastMessage
to trigger it as an event, if required, as shown
in the following code sample 7-11:
//Event called health changed
public void ChangeHealth(float Amount)
{
//Reduce health
Health += Amount;
//Should we die?
if(Health <= 0)