坦克对战游戏 AI 设计
从商店下载游戏:“Kawaii” Tank 或 其他坦克模型,构建 AI 对战坦克。具体要求
- 使用“感知-思考-行为”模型,建模 AI 坦克
- 场景中要放置一些障碍阻挡对手视线
- 坦克需要放置一个矩阵包围盒触发器,以保证 AI 坦克能使用射线探测对手方位
- AI 坦克必须在有目标条件下使用导航,并能绕过障碍。(失去目标时策略自己思考)
- 实现人机对战
成品图
具体实现
场景设计
利用官方教程Tanks! Tutorial里面的模型构建了自己的游戏场景。
坦克基类Tank
定义坦克的血量,射击方式
public class Tank : MonoBehaviour {
private float hp =500.0f;
public Tank()
{
hp = 500.0f;
}
public float getHP()
{
return hp;
}
public void setHP(float hp)
{
this.hp = hp;
}
public void shoot(TankType type)
{
GameObject bullet = Singleton<MyFactory>.Instance.getBullets(type);
bullet.transform.position = new Vector3(transform.position.x, 1.5f, transform.position.z) + transform.forward * 1.5f;
bullet.transform.forward = transform.forward; //方向
bullet.GetComponent<Rigidbody>().AddForce(bullet.transform.forward * 20, ForceMode.Impulse);
}
}
玩家player类
玩家类继承于坦克类,在player里定义移动和转向
public class Player : Tank{
public delegate void DestroyPlayer();
public static event DestroyPlayer destroyEvent;
void Start () {
setHP(500);
}
void Update () {
if(getHP() <= 0)
{
this.gameObject.SetActive(false);
destroyEvent();
}
}
public void moveForward()
{
gameObject.GetComponent<Rigidbody>().velocity = gameObject.transform.forward * 30;
}
public void moveBackWard()
{
gameObject.GetComponent<Rigidbody>().velocity = gameObject.transform.forward * -30;
}
public void turn(float offsetX)
{
float x = gameObject.transform.localEulerAngles.x;
float y = gameObject.transform.localEulerAngles.y +