游戏场景——敌人——固定不动的敌人
预制体制作
将一个炮塔模型拖拽至场景当中,然后为其添加一个旋转脚本。
写代码
创建一个MosterTower脚本,它继承自坦克基类。
类字段
开火方法
实现间隔开火
处理受伤逻辑
让子弹在击中敌方坦克后销毁
在子弹脚本中添加逻辑
其实就是敌人射玩家和玩家射敌人
再添加一个tag
再玩家被敌人击中后,让玩家受伤
最后将坦克的参数设置好即可
直到当前MonsterTower的代码为
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonsterTower : TankBaseObj
{
//炮塔的发射间隔时间
public float fireOffsetTime = 0.5f;
//当前时间
public float nowTime = 0;
//发射点
public Transform[] shootPos;
//关联的子弹
public GameObject bullet;
public override void Fire()
{
//有多少个炮口就创建多少个子弹
for (int i = 0; i < shootPos.Length; i++)
{
GameObject obj = Instantiate(bullet, shootPos[i].transform.position, shootPos[i].transform.rotation);
//控制子弹做什么
BulletObj bulletObj = obj.GetComponent<BulletObj>();
bulletObj.SetFather(this);
}
}
//控制炮台开火
void Update()
{
nowTime += Time.deltaTime;
if (nowTime > fireOffsetTime)
{
Fire();
nowTime = 0;
}
}
public override void Wound(TankBaseObj other)
{
//什么逻辑都不写,就能让炮台无法受到伤害
}
}