官术网_书友最值得收藏!

The enemy tank AI

Let's look at the real code for our AI tanks. Let's create a new class, called SimpleFSM, which inherits from our FSM abstract class.

The code in the SimpleFSM.cs file is as follows:

using UnityEngine; 
using System.Collections; 
 
public class SimpleFSM : FSM  
{ 
 
    public enum FSMState 
    { 
      None, 
      Patrol, 
      Chase, 
      Attack, 
      Dead, 
    } 
 
    //Current state that the NPC is reaching 
    public FSMState curState; 
 
    //Speed of the tank 
    private float curSpeed; 
 
    //Tank Rotation Speed 
    private float curRotSpeed; 
 
    //Bullet 
[SerializeField] private GameObject Bullet; //Whether the NPC is destroyed or not private bool bDead; private int health;

// We overwrite the deprecated built-in `rigidbody` variable.
new private Rigidbody rigidbody;

Here, we declare a few variables. Our tank AI will have four different states: Patrol, Chase, Attack, and Dead. Basically, we are implementing the FSM that we described as an example in Chapter 1, Introduction to AI:

The enemy tank AI's FSM

In our Initialize method, we set up our AI tank's properties with default values. Then we store the positions of waypoints in our local variable. We got those waypoints from our scene using the FindGameObjectsWithTag method, trying to find those objects with the WandarPoint tag:

    //Initialize the Finite state machine for the NPC tank 
    protected override void Initialize ()  
    { 
      curState = FSMState.Patrol; 
      curSpeed = 150.0f; 
      curRotSpeed = 2.0f; 
      bDead = false; 
      elapsedTime = 0.0f; 
      shootRate = 3.0f; 
      health = 100; 
 
      //Get the list of points 
      pointList =  
      GameObject.FindGameObjectsWithTag("WandarPoint"); 
 
      //Set Random destination point first 
      FindNextPoint(); 
 
      //Get the target enemy(Player) 
      GameObject objPlayer =  
      GameObject.FindGameObjectWithTag("Player"); 

// Get the rigidbody
rigidbody = GetComponent<Rigidbody>(); playerTransform = objPlayer.transform; if (!playerTransform) print("Player doesn't exist.. Please add one "+ "with Tag named 'Player'"); //Get the turret of the tank turret = gameObject.transform.GetChild(0).transform; bulletSpawnPoint = turret.GetChild(0).transform; }

The Update method that gets called every frame looks as follows:

    //Update each frame 
    protected override void FSMUpdate() 
    { 
      switch (curState) 
      { 
        case FSMState.Patrol: UpdatePatrolState(); break; 
        case FSMState.Chase: UpdateChaseState(); break; 
        case FSMState.Attack: UpdateAttackState(); break; 
        case FSMState.Dead: UpdateDeadState(); break; 
      } 
 
     //Update the time 
     elapsedTime += Time.deltaTime; 
 
     //Go to dead state is no health left 
     if (health <= 0) 
      curState = FSMState.Dead; 
    } 

We check the current state and then call the appropriate state method. Once the health object has a value of zero or less, we set the tank to the Dead state.

主站蜘蛛池模板: 江北区| 正镶白旗| 大石桥市| 娄烦县| 林周县| 皋兰县| 蚌埠市| 冕宁县| 张家口市| 化州市| 山阴县| 原阳县| 绥宁县| 沐川县| 雅江县| 玉树县| 清水河县| 聊城市| 大冶市| 临朐县| 临夏市| 海淀区| 观塘区| 渭源县| 武冈市| 汶上县| 海盐县| 哈巴河县| 祁连县| 老河口市| 横峰县| 吴桥县| 广元市| 德化县| 兴安盟| 辽阳市| 礼泉县| 泸溪县| 炎陵县| 理塘县| 环江|