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

The abstract FSM class

Next, we implement a generic abstract class to define the methods that our enemy tank AI class has to implement.

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

using UnityEngine; 
using System.Collections; 
 
public class FSM : MonoBehaviour  
{ 
    //Player Transform 
    protected Transform playerTransform; 
 
    //Next destination position of the NPC Tank 
    protected Vector3 destPos; 
 
    //List of points for patrolling 
    protected GameObject[] pointList; 
 
    //Bullet shooting rate 
    protected float shootRate; 
    protected float elapsedTime; 
 
    //Tank Turret 
    public Transform turret { get; set; } 
    public Transform bulletSpawnPoint { get; set; } 
 
    protected virtual void Initialize() { } 
    protected virtual void FSMUpdate() { } 
    protected virtual void FSMFixedUpdate() { } 
 
    // Use this for initialization 
    void Start ()  
    { 
      Initialize(); 
    } 
     
    // Update is called once per frame 
    void Update ()  
    { 
      FSMUpdate(); 
    } 
 
    void FixedUpdate() 
    { 
      FSMFixedUpdate(); 
    } 
} 

The enemy tanks need only to know the position of the player's tank, their next destination point, and the list of waypoints to choose from while they're patrolling. Once the player tank is in range, they rotate their turret object and then start shooting from the bullet spawn point at their fire rate.

The inherited classes will also need to implement the three methods: Initialize, FSMUpdate, and FSMFixedUpdate.

主站蜘蛛池模板: 蓬安县| 新宁县| 怀化市| 洱源县| 天全县| 龙南县| 大港区| 潍坊市| 望奎县| 岳西县| 玛纳斯县| 永春县| 天津市| 太保市| 化德县| 蒙山县| 莒南县| 通化市| 景宁| 乌拉特后旗| 东港市| 和政县| 清徐县| 会宁县| 正定县| 阳城县| 措勤县| 忻州市| 壤塘县| 靖西县| 榆林市| 海晏县| 六枝特区| 石柱| 宜丰县| 梁河县| 上虞市| 平南县| 偃师市| 张家口市| 苗栗县|