Game Development Reference
In-Depth Information
//Define possible states for enemy
public enum AI_ENEMY_STATE {IDLE = 2081823275,
PATROL=207038023,
CHASE= 1463555229,
ATTACK=1080829965,
SEEKHEALTH=-833380208};
More information can be found online at
http://docs.unity3d.com/
On the basis of the enumeration
AI_ENEMY_STATE
, the
AI_Enemy
class will maintain
a public variable
CurrentState
, which expresses the active state of the enemy object
right now. This variable will change over time as the states change, as shown in the
following code:
//Current state of enemy
public AI_ENEMY_STATE CurrentState = AI_ENEMY_STATE.IDLE;
Like most objects, the class
AI_Enemy
features an
Awake
function to retrieve cached
component references to other components, including the
NavMeshAgent
and the
local
Transform
, as well as to other objects in the scene, such as the
Player
object.
These references will be used elsewhere in the script, as shown in the following
code sample 7-2:
//Get Animator
ThisAnimator = GetComponent<Animator>();
//Get Navigation Mesh Agent
ThisAgent = GetComponent<NavMeshAgent>();
//Get Transform Component
ThisTransform = transform;
//Get Player Transform
PlayerTransform =
GameObject.FindGameObjectWithTag("Player").transform;
//Get Collider
ThisCollider = GetComponent<BoxCollider>();