Game Development Reference
In-Depth Information
09 //Current state of enemy - default is patrol
10 public ENEMY_STATE ActiveState = ENEMY_STATE.PATROL;
11
12 //Current health of this enemy
13 public int Health = 100;
14 }
Changing Between States
Each of the three states for the Enemy will be implemented as separate coroutines, with each
respective coroutine repeating every frame for as long as the state is active. To achieve this, we'll
need three coroutines, one for each state, and a function to manage and change between states.
The Enemy class be updated as shown in Listing 7-6; comments follow.
Listing 7-6. Moving Further with FSMs: Defining State Relationships with Coroutines
001 using UnityEngine;
002 using System.Collections;
003
004 public class Enemy : MonoBehaviour
005 {
006 //Enum of states for FSM
007 public enum ENEMY_STATE {PATROL = 0, CHASE = 1, ATTACK=2};
008
009 //Current state of enemy - default is patrol
010 public ENEMY_STATE ActiveState = ENEMY_STATE.PATROL;
011
012 //Current health of this enemy
013 public int Health = 100;
014
015 //Reference to active PlayerController component for player
016 protected PlayerController PC = null;
017
018 //Enemy cached transform
019 protected Transform ThisTransform = null;
020
021 //Reference to Player Transform
022 protected Transform PlayerTransform = null;
023
024 //Total distance enemy must be from player, in Unity Units, before chasing them
(entering chase state)
025 public float ChaseDistance = 10.0f;
026
027 //Total distance enemy must be from player before attacking them
028 public float AttackDistance = 0.1f;
029
030 //------------------------------------------------
 
Search WWH ::




Custom Search