Game Development Reference
In-Depth Information
The Chase and Attack States
Having seen the fundamentals of creating an FSM through previous sections, let's now put it all
together. Here we'll finalize the enemy Drone character, complete with Patrol, Chase, and Attack
states. Consider Listings 7-9 and 7-10 for the complete Enemy class and the relevant changes to the
Enemy_Drone classes. Get ready for some longer code listings! Comments on critical code additions
and changes follow.
Listing 7-9. Final Enemy.cs Class with a Completed FSM
001 //Sets up FSM for enemy AI
002 //------------------------------------------------
003 using UnityEngine;
004 using System.Collections;
005 using System.Collections.Generic;
006 //------------------------------------------------
007 public class Enemy : MonoBehaviour
008 {
009 //Enemy types
010 public enum ENEMY_TYPE {Drone = 0, ToughGuy = 1, Boss=2};
011
012 //Type of this enemy
013 public ENEMY_TYPE Type = ENEMY_TYPE.Drone;
014
015 //Custom ID of this enemy
016 public int EnemyID = 0;
017
018 //Current health of this enemy
019 public int Health = 100;
020
021 //Attack Damage - amount of damage this enemy deals to player when attacking
022 public int AttackDamage = 10;
023
024 //Recovery delay in seconds after launching an attack
025 public float RecoveryDelay = 1.0f;
026
027 //Enemy cached transform
028 protected Transform ThisTransform = null;
029
030 //------------------------------------------------
031 //AI Properties
032
033 //Reference to NavMesh Agent component
034 protected NavMeshAgent Agent = null;
035
036 //Reference to active PlayerController component for player
037 protected PlayerController PC = null;
038
039 //Reference to Player Transform
040 protected Transform PlayerTransform = null;
041
 
Search WWH ::




Custom Search